[英]Take multiple k samples of varying size n by groups in R
I have a dataset that looks like this:我有一个看起来像这样的数据集:
group=rep(1:4,each=100)
values=round(runif(400,25,350),0)
data<-data.frame(values,group)
Each group is comprised by 100 observations (values).每组由 100 个观察值(值)组成。
For each group, I would take 20 random samples without replacement and varying sampling size starting from 10 and increasing by 5 up to 95.对于每组,我将随机抽取 20 个样本而无需替换,样本大小从 10 开始,从 5 增加到 95。
Thus for each group I want 20 samples with size=10, 20 samples with size=15....20 samples with size=95.因此,对于每组,我想要 20 个大小为 10 的样本,20 个样本大小为 15....20 个样本大小为 95。
Any idea on how to do it using some tidyverse solution?关于如何使用一些 tidyverse 解决方案做到这一点的任何想法?
At the moment I did this:目前我这样做了:
data %>%
group_by(group) %>%
nest() %>%
mutate(v=map(data,~rep_sample_n(.,size=10,replace=FALSE,reps=20))) %>%
unnest(v)
It seems correctly replicate 20 times a sample with size=10, but still I need to change the size...它似乎正确地复制了大小为 10 的样本 20 次,但我仍然需要更改大小...
Thanks.谢谢。
You could create a sequence of sample sizes, wrap your group_by/nest/etc dude into a For loop, then add each new sample to a list.您可以创建一系列样本大小,将 group_by/nest/etc 包到 For 循环中,然后将每个新样本添加到列表中。
Notice how the size argument in ~rep_sample_n
is now sizes[i]
rather than a fixed number.注意
~rep_sample_n
的 size 参数现在是 size sizes[i]
而不是固定数字。
sizes <- seq(10,95,by=5)
sample_list <- list()
for (i in 1:length(sizes)){
new_data <- data %>%
group_by(group) %>%
nest() %>%
mutate(v=map(data,~rep_sample_n(.,size=sizes[i],replace=FALSE,reps=20))) %>%
unnest(v)
sample_list[i] <- new_data
}
I am suggesting a for
loop instead of lapply()
, as it makes more sense to me and this application doesn't take much time anyway.我建议使用
for
循环而不是lapply()
,因为它对我来说更有意义,而且这个应用程序不会花费太多时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.