doBy (version 4.5-15)

summaryBy: Function to calculate groupwise summary statistics


Function to calculate groupwise summary statistics, much like the summary procedure of SAS


summaryBy(formula, data = parent.frame(), id = NULL, FUN = mean, keep.names=FALSE, p2d=FALSE, order=TRUE, full.dimension=FALSE, var.names=NULL, fun.names=NULL, ...)


A formula object, see examples below
A data frame
A formula specifying variables which data are not grouped by but which should appear in the output. See examples below.
A list of functions to be applied, see examples below.
If TRUE and if there is only ONE function in FUN, then the variables in the output will have the same name as the variables in the input, see 'examples'.
Should parentheses in output variable names be replaced by dots?
Should the resulting dataframe be ordered according to the variables on the right hand side of the formula? (using orderBy
If TRUE then rows of summary statistics are repeated such that the result will have the same number of rows as the input dataset.
Option for user to specify the names of the variables on the left hand side.
Option for user to specify function names to apply to the variables on the left hand side.
Additional arguments to FUN. This could for example be NA actions.


A data frame


Extra arguments ('...') are passed onto the functions in FUN. Hence care must be taken that all functions in FUN accept these arguments - OR one can explicitly write a functions which get around this. This can particularly be an issue in connection with handling NAs. See examples below.

Some code for this function has been suggested by Jim Robison-Cox.

See Also

ave, descStat, lapplyBy, orderBy, scaleBy, splitBy, transformBy,


Run this code

dietox12    <- subset(dietox,Time==12)

summaryBy(Weight+Feed~Evit+Cu, data=dietox12,

summaryBy(list(c("Weight","Feed"), c("Evit","Cu")), data=dietox12,

summaryBy(Weight+Feed~Evit+Cu+Time, data=subset(dietox,Time>1),

## Calculations on transformed data:

summaryBy(log(Weight)+Feed~Evit+Cu, data=dietox12)

## Calculations on all numerical variables (not mentioned elsewhere):

summaryBy(.~Evit+Cu,                data=dietox12,
   id=~Litter, FUN=mean)

## There are missing values in the 'airquality' data, so we remove these
## before calculating mean and variance with 'na.rm=TRUE'. However the
## length function does not accept any such argument. Hence we get
## around this by defining our own summary function in which length is
## not supplied with this argument while mean and var are:

sumfun <- function(x, ...){
  c(m=mean(x, ...), v=var(x, ...), l=length(x))
summaryBy(Ozone+Solar.R~Month, data=airquality, FUN=sumfun, na.rm=TRUE)

## Using '.' on the right hand side of a formula means to stratify by
## all variables not used elsewhere:

summaryBy(breaks ~ wool+tension, warpbreaks)
summaryBy(breaks ~., warpbreaks)
summaryBy(.~ wool+tension, warpbreaks)

## Keep the names of the variables (works only if FUN only returns one
## value):

summaryBy(Ozone+Wind~Month, data=airquality,FUN=c(mean),na.rm=TRUE,

## Using full.dimension=TRUE

## Consider:
summaryBy(breaks~wool, data=warpbreaks)
## Rows of result are replicated below
summaryBy(breaks~wool, data=warpbreaks, full.dimension=TRUE)
## Notice: Previous result is effectively the same as
with(warpbreaks, ave(breaks, wool))
## A possible application of full.dimension=TRUE is if we want to
## standardize (center and scale) data within groups:
ss <- summaryBy(breaks~wool, data=warpbreaks, full.dimension=TRUE, FUN=c(mean,sd))

Run the code above in your browser using DataLab