我正在使用下面的代码对一个人口进行分层随机抽样,之前根据性别和地区建立了目标。可以看到,相同的代码重复了三次(每次使用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]]))
2条答案
按热度按时间9w11ddsr1#
多亏了Harry Smith的帮助,我找到了答案,它包含了对他的代码的一个小调整:
gab6jxml2#
类似这样,它把dfs保存在一个列表中。