Section 11 Function split

11.1 Usage

split(x, f, drop = FALSE, ...)

11.2 Arguments

x is a vector or data frame containing values to be divided into groups.

f is a ‘factor’ in the sense that as.factor(f) defines the grouping, or a list of such factors in which case their interaction is used for the grouping.

drop is logical indicating if levels that do not occur should be dropped (if f is a factor or a list).

... Other optional arguments

11.3 Create data

set.seed(1234)
id <- paste0('S',1:20)
age <- sample(x = 8:10, size = length(id), replace = TRUE)
wt <- sample(30:40, size = length(id), replace = TRUE)
sex <- sample(x = c('Male','Female'), size = length(id), replace = TRUE)
vac <- sample(c(T,F), size = length(id), replace = TRUE)

DF <- data.frame(ID=id, Age=age, Wt=wt, 
                 Sex=sex, Vac=vac, stringsAsFactors = TRUE)

DF$Age[5] <- NA
DF$Wt[10] <- NA

str(DF)

11.4 split with one factor

str(DF)

sDF <- split(x = DF, f = DF$Sex)
str(sDF)

sapply(sDF, function(x) colMeans(x[, c("Age", "Wt")]))
sapply(sDF, function(x) colMeans(x[, c("Age", "Wt")], na.rm = TRUE))

11.5 split with two factors

sDF <- split(x = DF, f = list(DF$Sex, DF$Vac))
str(sDF)

sapply(sDF, function(x) colMeans(x[, c("Age", "Wt")], na.rm = TRUE))