Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
R DocumentationR InterfaceData Input in RData Management in RStatistics in RGraphs in R

Aggregating Data in R

It is relatively easy to collapse data in R using one or more BY variables and a defined function.

# aggregate data frame mtcars by cyl and vs, returning means
# for numeric variables
attach(mtcars)
aggdata <-aggregate(mtcars, by=list(cyl,vs),
  FUN=mean, na.rm=TRUE)
print(aggdata)
detach(mtcars)

When using the aggregate() function, the by variables must be in a list (even if there is only one). The function can be built-in or user provided.

See also:

Going Further

To practice aggregate() and other functions, try the exercises in this manipulating data tutorial.