Learn R Programming

doBy (version 0.4)

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, drop=FALSE, prefix=NULL, ...)

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'.
prefix
An optional vector of prefixes for the names of the output variables, see 'examples'.
drop
Logical indicating if levels that do not occur should be dropped
...
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, 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,
  prefix=c("m","v"))

Run the code above in your browser using DataLab