Learn R Programming

mosaic (version 0.10.0)

maggregate: Aggregate for mosaic

Description

Compute function on subsets of a variable in a data frame.

Usage

maggregate(formula, data = parent.frame(), FUN, subset,
  overall = mosaic.par.get("aggregate.overall"), .format = c("default",
  "table", "flat"), drop = FALSE, .multiple = FALSE, groups = NULL,
  .name = deparse(substitute(FUN)), ...)

Arguments

formula
a formula. Left side provides variable to be summarized. Right side and condition describe subsets. If the left side is empty, right side and condition are shifted over as a convenience.
data
a data frame. Note that the default is data=parent.frame(). This makes it convenient to use this function interactively by treating the working envionment as if it were a data frame. But this may not be appropriate for programming uses. Whe
FUN
a function to apply to each subset
subset
a logical indicating a subset of data to be processed.
overall
currently unused
.format
format used for aggregation. "default" and "flat" are equivalent.
drop
a logical indicating whether unused levels should be dropped.
.multiple
a logical indicating whether FUN returns multiple values
groups
grouping variable that will be folded into the formula (if there is room for it). This offers some additional flexibility in how formulas can be specified.
.name
a name used for the resulting object
...
additional arguments passed to FUN

Value

  • a vector

Examples

Run this code
if (require(mosaicData)) {
maggregate( cesd ~ sex, HELPrct, FUN=mean )
# using groups instead
maggregate( ~ cesd, groups = sex, HELPrct, FUN=sd )
# the next four all do the same thing
maggregate( cesd ~ sex & homeless, HELPrct, FUN=mean )
maggregate( cesd ~ sex | homeless, HELPrct, FUN=sd )
maggregate( ~ cesd | sex , groups= homeless, HELPrct, FUN=sd )
maggregate( cesd ~ sex, groups = homeless, HELPrct, FUN=sd )
# this is unusual, but also works.
maggregate( cesd ~ NULL , groups = sex, HELPrct, FUN=sd )
}

Run the code above in your browser using DataLab