aggregate()
In this page:
aggregate()
aggregate(value ~ group, data = df, FUN = mean) groups a data frame by one column and computes a summary statistic (like mean or sum) per group, similar to SQL's GROUP BY. This is the standard R base function for group-wise summarization before more specialized packages are introduced. The formula syntax, value ~ group, reads as "value, grouped by group".
Note: The formula syntax value ~ group reads naturally as "summarize value, grouped by group".
Example: aggregate()
df <- data.frame(
team = c("A", "A", "B", "B"),
score = c(10, 20, 30, 40)
)
result <- aggregate(score ~ team, data = df, FUN = mean)
print(result)
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: