Learn R Programming

quest (version 0.2.0)

tapply2: Apply a Function to a (Atomic) Vector by Group

Description

tapply2 applies a function to a (atomic) vector by group and is an alternative to the base R function tapply. The function is apart of the split-apply-combine type of function discussed in the plyr R package and is somewhat similar to dlply. It splits up one (atomic) vector .xinto a (atomic) vector for each group in .grp, applies a function .fun to each (atomic) vector, and then returns the results as a list with names equal to the group values unique(interaction(.grp.nm, sep = .sep)). tapply2 is simply split.default + lapply. Similar to dlply, The arguments all start with . so that they do not conflict with arguments from the function .fun. If you want to apply a function a data.frame rather than a (atomic) vector, then use by2.

Usage

tapply2(.x, .grp, .sep = ".", .fun, ...)

Value

list of objects containing the return object of .fun for each group. The names are the unique combinations of the grouping variables (i.e., unique(interaction(.grp, sep = .sep))).

Arguments

.x

atomic vector

.grp

list of atomic vector(s) and/or factor(s) (e.g., data.frame) containing the groups. They should each have same length as .x. It can also be an atomic vector or factor, which will then be made the first element of a list internally.

.sep

character vector of length 1 specifying the string to combine the group values together with. .sep is only used if there are multiple grouping variables (i.e., .grp is a list with multiple elements).

.fun

function to apply to .x for each group.

...

additional named arguments to pass to .fun.

See Also

Examples

Run this code

# one grouping variable
tapply2(mtcars$"cyl", .grp = mtcars$"vs", .fun = median, na.rm = TRUE)

# two grouping variables
grp_nm <- c("vs","am") # Roxygen runs the whole script if I put a c() in a []
x <- tapply2(mtcars$"cyl", .grp = mtcars[grp_nm], .fun = median, na.rm = TRUE)
print(x)
str(x)

# compare to tapply
grp_nm <- c("vs","am") # Roxygen runs the whole script if I put a c() in a []
y <- tapply(mtcars$"cyl", INDEX = mtcars[grp_nm],
   FUN = median, na.rm = TRUE, simplify = FALSE)
print(y)
str(y) # has dimnames rather than names

Run the code above in your browser using DataLab