Learn R Programming

taRifx (version 1.0.6.2)

splitc: Split data over columns

Description

Split data column-wise on data.frame, matrix and array or element-wise on a list.

Usage

splitc(X, INDEX, FUN = NULL, ...)

Arguments

X

A data.frame, matrix, array or a list.

INDEX

A factor of length(X) (number of columns or list elements). If not a factor, it will be coerced into one.

FUN

A function to be applied to individual subset of data (each factor level). If not provided (NULL), raw (split) data is returned.

Additional arguments to FUN.

Value

A list of the same length as there are factor levels in INDEX.

Details

Function splits a data.frame, matrix and array column-wise according to INDEX and list is sliced according to INDEX. Output is returned as a list of the same length as the number of levels in INDEX.

See Also

tapply, by, aggregate, apply, split

Examples

Run this code
# NOT RUN {
my.list <- list(a = runif(5), b = runif(5), c = runif(5), d = runif(5), e = runif(10),
		f = runif(10), g = runif(10), h = runif(10), i = runif(10), j = runif(10))
my.df <- as.data.frame(my.list)
my.matrix <- as.matrix(my.df)

ind <- factor(c(1,1,1,1, 2,3, 4,4,4,4))
ind2 <- factor(c(1,1,1,1, 2,3, 4,4,4,4), levels = 1:5)

# Applies mean to each, you must use \code{colMeans},
#   as \code{mean} is deprecated for \code{data.frame}s
splitc(X = my.df, INDEX = ind, FUN = colMeans)
splitc(X = my.matrix, INDEX = ind2) # level 5 empty because not populated
splitc(X = my.list, INDEX = ind, FUN = sum) # applied to elements INDEX-wise
# }

Run the code above in your browser using DataLab