Learn R Programming

doBy (version 1.5)

summaryBy: Function to calculate groupwise summary statistics

Description

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

Usage

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

Arguments

formula
A formula object, see examples below
data
A data frame
id
A formula specifying variables which data are not grouped by but which should appear in the output. See examples below.
FUN
A list of functions to be applied, see examples below.
keep.names
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'.
postfix
An optional vector of postfixes for the names of the output variables, see 'examples'.
p2d
Should parentheses in output variable names be replaced by dots?
order
Should the resulting dataframe be ordered according to the variables on the right hand side of the formula? (using orderBy
...
Additional arguments to FUN. This could for example be NA actions.

Value

  • A data frame

Details

NAs are removed before calculating summary statistics

See Also

orderBy, transformBy, splitBy, doby.xtabs,

Examples

Run this code
data(dietox)
dietox12    <- subset(dietox,dietox$Time==12)summaryBy(Weight+Feed~Evit+Cu,      data=dietox12, FUN=c(mean,var), na.rm=TRUE)  

summaryBy(Weight+Feed~Evit+Cu+Time, data=subset(dietox,Time>1),
FUN=c(mean,var), na.rm=TRUE)  

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

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

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

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

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

summaryBy(Ozone+Wind~Month, data=airquality,FUN=c(mean,var),na.rm=TRUE,
  postfix=c("m","v"))

summaryBy(Ozone+Wind~Month, data=airquality,
  FUN=c(function(x,...){c(mean(x,...),var(x,...))},function(x,...){median(x,...)}),
  postfix=list(c("m","v"),"med"), na.rm=TRUE)


summaryBy(conc ~ Plant, CO2)

do.call("summaryBy", list(.~ Treatment, CO2[3:5], FUN = function(x) mean(range(x)), postfix = "midrange"))

Run the code above in your browser using DataLab