R 分层抽样
library(sampling)
mtcars_order <- mtcars %>% arrange(gear)
mtcars_order %>% group_by(gear) %>% summarise(n=n())
smn <- round(prop.table(table(mtcars_order$gear))*10)
stratID <- strata(mtcars_order,stratanames='gear',size = smn,method = 'srswor')
head(stratID)
strat<-getdata(mtcars_order,stratID)
library(doBy)
mtcars %>% group_by(am,gear) %>% summarise(n=n())
mtcars %>% group_by(am) %>% summarise(n=n())
sampleBy(formula = ~ iris$Species,frac=0.1,data=iris)
sample_by(mtcars,formula = ~am+gear,frac = 0.2)
评论
发表评论