R dplyr Summarize: Group By & Aggregate Data

In R language, you can use the summarize() function in the dplyr package to perform data summarization. Here is a simple example:

If you have a dataset df that contains information such as students’ names, subjects, and grades, and you want to calculate the average grade of each student for each subject, you can use the following code.

library(dplyr)

df %>%
  group_by(姓名, 科目) %>%
  summarize(平均成绩 = mean(成绩))

In the code above, the group_by() function is used to group by name and subject, then the summarize() function calculates the average grade for each group, ultimately obtaining the average grade for each student in each subject.

In addition to the summarize() function, the dplyr package also offers other functions for data manipulation, such as mutate() and filter(). By combining these functions, more complex data manipulation operations can be achieved.

bannerAds