R分层抽样中避免重复编码的循环函数

qc6wkl3g  于 2023-03-05  发布在  其他
关注(0)|答案(2)|浏览(153)

我正在使用下面的代码对一个人口进行分层随机抽样,之前根据性别和地区建立了目标。可以看到,相同的代码重复了三次(每次使用rbind)。有没有办法编写一个循环函数,可以重复这个过程,这样我就不必写几次了?

library(tidyverse)
population<- data.frame(region=rep(c("North", "South", "North", "South"),50000),
                        sex=rep(c("Male", "Female", "Female", "Male"),50000))

targets<- data.frame(region=c("North", "South", "North", "South"),
                     sex=c("Male", "Female", "Female", "Male"),
                     total=c(20,25,20,30)) 

n=0
n=n+1
sample<- population %>% filter(region==targets$region[[1]], 
                           sex==targets$sex[[1]]) %>% 
  slice_sample(n=targets$total[[1]])
n=n+1
sample<- sample %>%  rbind(population %>% filter(region==targets$region[[n]], 
                               sex==targets$sex[[n]]) %>% 
  slice_sample(n=targets$total[[n]]))
n=n+1
sample<- sample %>%  rbind(population %>% filter(region==targets$region[[n]], 
                               sex==targets$sex[[n]]) %>% 
  slice_sample(n=targets$total[[n]]))
n=n+1
sample<- sample %>%  rbind(population %>% filter(region==targets$region[[n]], 
                               sex==targets$sex[[n]]) %>% 
  slice_sample(n=targets$total[[n]]))
9w11ddsr

9w11ddsr1#

多亏了Harry Smith的帮助,我找到了答案,它包含了对他的代码的一个小调整:

n = 4  
sample_list <- list()
for(i in 1:n){
  sample<- population %>% filter(region==targets$region[[i]], 
                                                   sex==targets$sex[[i]]) %>% 
                               slice_sample(n=targets$total[[i]])
  sample_list[[i]] <- sample
}

a<-bind_rows(sample_list)
gab6jxml

gab6jxml2#

类似这样,它把dfs保存在一个列表中。

n = 4  
sample_list <- list()
for(i in 1:n){
sample<-rbind(population %>% filter(region==targets$region[[i]], 
                                                 sex==targets$sex[[i]]) %>% 
                             slice_sample(n=targets$total[[i]]))
sample_list[[i]] <- sample
}

相关问题