View source: R/quest_functions.R
| agg | R Documentation |
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).
agg(x, grp, rep = TRUE, rtn.grp = !rep, sep = "_", fun, ...)
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 |
If rep = TRUE, then agg calls ave; if rep =
FALSE, then agg calls aggregate.
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:
then the return
object is a data.frame with nrow = nrow(data) where the first
columns are grp and the last column is the result of fun. If
grp is not a list with names, then its colnames will be "Group.1",
"Group.2", "Group.3" etc. similar to aggregate's return object. The
colname for the result of fun will be "x".
then the return
object is an atomic vector with length = length(x) where the values
are the result of fun and the names = names(x).
then the return
object is a data.frame with nrow =
length(levels(interaction(grp)))
where the first columns are the unique group combinations in grp and
the last column is the result of fun. If grp is not a list
with names, then its colnames will be "Group.1", "Group.2", "Group.3" etc.
similar to aggregate's return object. The colname for the result of
fun will be "x".
then the return
object is an atomic vector with length
length(levels(interaction(grp))) where the values are the result of
fun and the names are each group value pasted together by sep
if there are multiple grouping variables within grp (i.e.,
is.list(grp) && length(grp) > 2).
aggs,
agg_dfm,
ave,
aggregate,
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.