dplyr按值组进行采样

时间:2017-06-26 11:32:31

标签: r dplyr

我想根据dplyr的分组值制作样本:

我尝试了什么:

 id <- c(1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8)

 id <-  as.data.frame(id)

 sample <- id %>%
   group_by(id) %>%
   sample_n(2, replace = FALSE) %>%
   ungroup(id)

sample

预期结果(n样本= 2):

1, 1, 1, 2

1, 1, 1, 3, 3

5, 5, 5, 6, 6

我有一个错误:

Error: `size` must be less or equal than 1 (size of data), set `replace` = TRUE to use sampling with replacement

1 个答案:

答案 0 :(得分:2)

也许这有帮助

id %>% 
  distinct(id) %>% 
  sample_n(2, replace = FALSE) %>% 
  inner_join(id, .)