Divide into groups respecting relative proportions
Divide into groups respecting relative proportions
Divides a data frame randomly, but respecting the relative proportions of levels of a factor in the original data frame. Each subset has roughly the same number of individuals, and the same relative proportions in respect to levels of the given factor.
splitf(set, fac, k)
Arguments
set: a data frame containing values to be divided into groups.
fac: a reference factor giving the relative proportions to be respected in each subset of set.
k: an integer giving the number of subsets to be generated.
data(iris)iris2 <- iris[c(1:50,51:80,101:120),]# Proportions to be respectedtable(iris2$Species)/nrow(iris2)# Splittingresult <- splitf(iris2,iris2$Species,3)# All subsets have the same sizelapply(result,nrow)# And respect the initial proportionslapply(result,function(x) table(x$Species)/nrow(x))