| aggs {quest} | R Documentation |
Aggregate Data by Group
Description
aggs evaluates a function separately for each group and combines the
results back together into a data.frame that is returned. Depending on
rep, the results of fun are repeated for each element of
data[vrb.nm] in the group (TRUE) or only once for each group (FALSE).
Note, aggs evaluates fun separately for each variable
vrb.nm within data. If instead, you want to evaluate fun
for variables as a set data[vrb.nm], then use agg_dfm.
Usage
aggs(
data,
vrb.nm,
grp.nm,
rep = TRUE,
rtn.grp = !rep,
sep = "_",
suffix = "_a",
fun,
...
)
Arguments
data |
data.frame of data. |
vrb.nm |
character vector of colnames from |
grp.nm |
character vector of colnames from |
rep |
logical vector of length 1 specifying whether the result of
|
rtn.grp |
logical vector of length 1 specifying whether the group
columns (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 |
suffix |
character vector of length 1 specifying the string to append to the end of the colnames in the return object. |
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
data.frame of aggregated values. If rep is TRUE, then nrow =
nrow(data). If rep = FALSE, then nrow =
length(levels(interaction(data[grp.nm]))). The names are specified
by paste0(vrb.nm, suffix). If rtn.grp = TRUE, then the group
columns are appended to the begining of the data.frame.
See Also
Examples
aggs(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month",
fun = mean, na.rm = TRUE)
aggs(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month",
rtn.grp = TRUE, fun = mean, na.rm = TRUE) # include the group columns
aggs(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month",
rep = FALSE, fun = mean, na.rm = TRUE) # do NOT repeat aggregated values
aggs(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"),
rep = FALSE, fun = mean, na.rm = TRUE) # with multiple group columns
aggs(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"),
rep = FALSE, rtn.grp = FALSE, fun = mean, na.rm = TRUE) # without returning groups