For 循环遍历 Lapply(或使用 map2)
For Loop over Lapply (or using map2)
你好,我正在尝试遍历数据帧列表并从数据帧中提取不同大小的样本。例如,对于 df1,我想要一个大小为 10 的样本,df2 想要一个大小为 8 的样本,等等。我与 Melissa Key 合作解决了我之前提出的问题,以开发以下代码:
sampler <- function(df, n,...) {
return(df[sample(x=nrow(df),n),])
}
#for(i in 1:totalstratum){
sample_list<-lapply(population_list,sampler,n=stratum_sizes[i,1])
#}
#or
library(purrr)
sample_list<-map2(population_list, stratum_sizes,sampler)
其中stratum_sizes是向量{4,5,3,2,10,10,8},totalstratum=nrow(stratum_sizes),也等于元素个数在列表 population_list 中。
到目前为止,我能够获得一个样本,但从来没有获得正确数量的观察结果。有任何想法吗?预先感谢您的帮助!
我假设您想从存储在 list
.
中的 data.frame
中抽取一定数量的 行
如何使用 map2
:
# Generate sample data
# Here: A list of three data.frames
set.seed(2017);
lst <- lapply(1:3, function(x) data.frame(val1 = runif(20), val2 = runif(20)))
# Define the sample sizes for every data.frame in the list
ssize <- c(10, 5, 3)
# Sample ssize entries from every data.frame in the list
map2(ssize, lst, ~ .y[sample(nrow(.y), .x), ])
#[[1]]
# val1 val2
#16 0.38868193 0.6500038
#8 0.43490560 0.3191046
#11 0.67433148 0.8838444
#7 0.03932234 0.6204450
#2 0.53717641 0.3798674
#3 0.46919565 0.9420740
#19 0.94099988 0.1771317
#5 0.77008816 0.2276118
#10 0.27383312 0.2608393
#14 0.43207779 0.2117630
#
#[[2]]
# val1 val2
#12 0.8835366 0.6904628
#4 0.0791699 0.7512366
#6 0.5096950 0.4699963
#19 0.5393251 0.4123170
#20 0.9229542 0.9327490
#
#[[3]]
# val1 val2
#4 0.9204118 0.1926415
#15 0.8373573 0.9309950
#8 0.1653395 0.5895154
你好,我正在尝试遍历数据帧列表并从数据帧中提取不同大小的样本。例如,对于 df1,我想要一个大小为 10 的样本,df2 想要一个大小为 8 的样本,等等。我与 Melissa Key 合作解决了我之前提出的问题,以开发以下代码:
sampler <- function(df, n,...) {
return(df[sample(x=nrow(df),n),])
}
#for(i in 1:totalstratum){
sample_list<-lapply(population_list,sampler,n=stratum_sizes[i,1])
#}
#or
library(purrr)
sample_list<-map2(population_list, stratum_sizes,sampler)
其中stratum_sizes是向量{4,5,3,2,10,10,8},totalstratum=nrow(stratum_sizes),也等于元素个数在列表 population_list 中。
到目前为止,我能够获得一个样本,但从来没有获得正确数量的观察结果。有任何想法吗?预先感谢您的帮助!
我假设您想从存储在 list
.
data.frame
中抽取一定数量的 行
如何使用 map2
:
# Generate sample data
# Here: A list of three data.frames
set.seed(2017);
lst <- lapply(1:3, function(x) data.frame(val1 = runif(20), val2 = runif(20)))
# Define the sample sizes for every data.frame in the list
ssize <- c(10, 5, 3)
# Sample ssize entries from every data.frame in the list
map2(ssize, lst, ~ .y[sample(nrow(.y), .x), ])
#[[1]]
# val1 val2
#16 0.38868193 0.6500038
#8 0.43490560 0.3191046
#11 0.67433148 0.8838444
#7 0.03932234 0.6204450
#2 0.53717641 0.3798674
#3 0.46919565 0.9420740
#19 0.94099988 0.1771317
#5 0.77008816 0.2276118
#10 0.27383312 0.2608393
#14 0.43207779 0.2117630
#
#[[2]]
# val1 val2
#12 0.8835366 0.6904628
#4 0.0791699 0.7512366
#6 0.5096950 0.4699963
#19 0.5393251 0.4123170
#20 0.9229542 0.9327490
#
#[[3]]
# val1 val2
#4 0.9204118 0.1926415
#15 0.8373573 0.9309950
#8 0.1653395 0.5895154