我想根据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
答案 0 :(得分:2)
也许这有帮助
id %>%
distinct(id) %>%
sample_n(2, replace = FALSE) %>%
inner_join(id, .)