| agg {quest} | R Documentation |
Aggregate an Atomic Vector by Group
Description
agg evaluates a function separately for each group and combines the
results back together into an atomic vector of data.frame that is returned.
Depending on the argument rep, the results of fun are repeated
for each element of x in the group (TRUE) or only once for each group
(FALSE). Depending on the argument rtn.grp, the return object is a
data.frame and the groups within grp are included in the data.frame as
columns (TRUE) or the return object is an atomic vector and the groups are
the names (FALSE).
Usage
agg(x, grp, rep = TRUE, rtn.grp = !rep, sep = "_", fun, ...)
Arguments
x |
atomic vector. |
grp |
atomic vector or list of atomic vectors (e.g., data.frame)
specifying the groups. The atomic vector(s) must be the length of |
rep |
logical vector of length 1 specifying whether the result of
|
rtn.grp |
logical vector of length 1 specifying whether the groups
(i.e., |
sep |
character vector of length 1 specifying what string should
separate different group values when naming the return object. This
argument is only used if |
fun |
function to use for aggregation. This function is expected to return an atomic vector of length 1. |
... |
additional named arguments to |
Details
If rep = TRUE, then agg calls ave; if rep =
FALSE, then agg calls aggregate.
Value
result of fun applied to x for each group
within grp. The structure of the return object depends on the
arguments rep and rtn.grp:
- If rep = TRUE and rtn.grp = TRUE:
then the return object is a data.frame with nrow =
nrow(data)where the first columns aregrpand the last column is the result offun. Ifgrpis not a list with names, then its colnames will be "Group.1", "Group.2", "Group.3" etc. similar toaggregate's return object. The colname for the result offunwill be "x".- If rep = TRUE and rtn.grp = FALSE:
then the return object is an atomic vector with length =
length(x)where the values are the result offunand the names =names(x).- If rep = FALSE and rtn.grp = TRUE:
then the return object is a data.frame with nrow =
length(levels(interaction(grp)))where the first columns are the unique group combinations ingrpand the last column is the result offun. Ifgrpis not a list with names, then its colnames will be "Group.1", "Group.2", "Group.3" etc. similar toaggregate's return object. The colname for the result offunwill be "x".- If rep = FALSE and rtn.grp = FALSE:
then the return object is an atomic vector with length
length(levels(interaction(grp)))where the values are the result offunand the names are each group value pasted together bysepif there are multiple grouping variables withingrp(i.e.,is.list(grp) && length(grp) > 2).
See Also
aggs,
agg_dfm,
ave,
aggregate,
Examples
# one grouping variable
agg(x = airquality$"Solar.R", grp = airquality$"Month", fun = mean)
agg(x = airquality$"Solar.R", grp = airquality$"Month", fun = mean,
na.rm = TRUE) # ignoring missing values
agg(x = setNames(airquality$"Solar.R", nm = row.names(airquality)), grp = airquality$"Month",
fun = mean, na.rm = TRUE) # keeps the names in the return object
agg(x = airquality$"Solar.R", grp = airquality$"Month", rep = FALSE,
fun = mean, na.rm = TRUE) # do NOT repeat aggregated values
agg(x = airquality$"Solar.R", grp = airquality$"Month", rep = FALSE, rtn.grp = FALSE,
fun = mean, na.rm = TRUE) # groups are the names of the returned atomic vector
# two grouping variables
tmp_nm <- c("vs","am") # Roxygen2 doesn't like a c() within a []
agg(x = mtcars$"mpg", grp = mtcars[tmp_nm], rep = TRUE, fun = sd)
agg(x = mtcars$"mpg", grp = mtcars[tmp_nm], rep = FALSE,
fun = sd) # do NOT repeat aggregated values
agg(x = mtcars$"mpg", grp = mtcars[tmp_nm], rep = FALSE, rtn.grp = FALSE,
fun = sd) # groups are the names of the returned atomic vector
agg(x = mtcars$"mpg", grp = mtcars[tmp_nm], rep = FALSE, rtn.grp = FALSE,
sep = ".", fun = sd) # change the separater for naming
# error messages
## Not run:
agg(x = airquality$"Solar.R", grp = mtcars[tmp_nm]) # error returned
# b/c atomic vectors within \code{grp} not having the same length as \code{x}
## End(Not run)