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
It splits up one (atomic) vector
.xinto a (atomic) vector for each
.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
lapply. Similar to
dlply, The arguments
all start with
. so that they do not conflict with arguments from the
.fun. If you want to apply a function a data.frame rather
than a (atomic) vector, then use
list of atomic vector(s) and/or factor(s) (e.g., data.frame)
containing the groups. They should each have same length as
character vector of length 1 specifying the string to combine the
group values together with.
function to apply to
additional named arguments to pass to
list of objects containing the return object of
.fun for each
group. The names are the unique combinations of the grouping variables
unique(interaction(.grp, sep = .sep))).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.