← Back to R Course | Chapter 8: Data Manipulation | Lesson 7 of 7

aggregate()

In this page:

  1. aggregate()

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()

markup
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)
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.