Data Analysis
Aggregate
Grouped Totals
aggregate computes summaries by group. Formula syntax names the value column and grouping column.
Program
Play the script to sum amounts by region and read the east total.
aggregate_summary.R
sales <- data.frame(region = c("east", "west", "east"), amount = c(10, 7, 5))
summary <- aggregate(amount ~ region, sales, sum)
east_total <- summary$amount[summary$region == "east"]
cat(east_total, "\n", sep = "")
aggregate
`aggregate(amount ~ region, sales, sum)` groups by region and sums amounts.
formula
`amount ~ region` reads as summarize amount by region.
grouped summary
Grouped summaries reduce many rows to one row per group.