使用坐标随机抽样网格

时间:2017-01-12 18:42:42

标签: r

我有一个编号的网格(19 x 22)放在图像上,我需要随机选择418帧中的250个。有没有办法可以通过随机选择x和y坐标而不做任何重复来做到这一点?

由于

Example of grid over image (rows and columns are not labeled)

1 个答案:

答案 0 :(得分:0)

这是一种生成随机X和Y值的快速而肮脏的方法,无需在网格中重复。

library(tidyverse) # Gives us both %>% and filter_n

# Create a dataframe (technically a tibble) with one cell for each
# cell in your grid
combos <- expand.grid(x = seq(1, 19, 1), y = seq(1, 22, 1)) %>%
  tbl_df()

# Draw 250 random samples from the data without replacement
# If you post more information about the data you're using,
# I might be able to skip the creation of the combos data entirely
grid_sample <- sample_n(combos, size = 250, replace = FALSE)

如果您发布了要抽样的数据类型的代码示例,我可以简化。