分组按量抽样
df <- data.frame(id = 1:15, grp = rep(1:3,each = 5), frq = rep(c(3,2,4), each = 5)) set.seed(22) df %>% group_by(grp) %>% # for every group summarise(d = list(data.frame(id=id)), # create a data frame of ids frq = unique(frq)) %>% # get the unique frq value mutate(v = map2(d, frq, ~sample_n(.x, .y))) %>% # sample using data frame of ids and frq value unnest(v) %>% # unnest sampled values select(-frq) # remove frq column (if needed)