Learn R Programming

RVAideMemoire (version 0.9-83-7)

splitf: Divide into groups respecting relative proportions

Description

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.

Usage

splitf(set, fac, k)

Value

A list of subsets of set.

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.

Author

Maxime HERVE <maxime.herve@univ-rennes1.fr>

See Also

Examples

Run this code
data(iris)
iris2 <- iris[c(1:50,51:80,101:120),]

# Proportions to be respected
table(iris2$Species)/nrow(iris2)

# Splitting
result <- splitf(iris2,iris2$Species,3)

# All subsets have the same size
lapply(result,nrow)

# And respect the initial proportions
lapply(result,function(x) table(x$Species)/nrow(x))

Run the code above in your browser using DataLab