balancedataset: balance a data set according to some grouping factor(s)

Description Usage Arguments Details Value Author(s) Examples

View source: R/balancedataset.R

Description

balance a data set according to some grouping factor(s)

Usage

1
balancedataset(xdata, whattobalance, n = NULL)

Arguments

xdata

a data.frame

whattobalance

a character vector with column names. The corresponding columns typically are either factor or character.

n

integer, the number of cases to select for each factor level (or combination of factor levels)

Details

the function requires either one or two factors to be balanced over

if n is larger than the largest possible number, there will be a warning to that effect and n will be reset to the largest possible number, i.e. the function behaves as if n = NULL (the default)

Value

a list with 5 items

Author(s)

Christof Neumann

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
set.seed(1)
xdata <- data.frame(ID = sample(letters[1:4], 30, replace = TRUE),
context = sample(LETTERS[21:22], 30, replace = TRUE),
var1 = rnorm(30), var2 = rnorm(30))
table(xdata$ID, xdata$context)
balancedataset(xdata = xdata, whattobalance = c("context"), n = 2)$seldata
balancedataset(xdata = xdata, whattobalance = c("context"), n = 3)$seldata
balancedataset(xdata = xdata, whattobalance = c("context"))$seldata

# with two factors
balancedataset(xdata = xdata, whattobalance = c("context", "ID"), n = 1)$seldata

# a case where one combination occurs only once (hence n = 1): row 13 has to be in each data set
xdata2 <- xdata[-9, ]
table(xdata2$ID, xdata2$context)
x <- sapply(1:50, function(X){
  row.names(balancedataset(xdata = xdata2, whattobalance = c("context", "ID"))$seldata)
})
table(x)

gobbios/cfp documentation built on Sept. 25, 2018, 7:47 p.m.