splitc: Split data over columns

Description Usage Arguments Details Value Note Author(s) See Also Examples

View source: R/Contributed.R

Description

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

Usage

1
  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.

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.

Value

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

Note

Simplification sensu tapply is not yet implemented.

Author(s)

Roman Lustrik roman.lustrik@biolitika.si

See Also

tapply, by, aggregate, apply, split

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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

taRifx documentation built on April 14, 2020, 6:27 p.m.

Related to splitc in taRifx...