学习笔记,仅供参考,有错必纠


准备

df <- data.frame(grade = c("A", "A", "A", "B", "B", "C"), 
a = c(1:6),
b = c(6:1),
c = c(10:15))
> df
grade a b c
1 A 1 6 10
2 A 2 5 11
3 A 3 4 12
4 B 4 3 13
5 B 5 2 14
6 C 6 1 15

distinct()

Select only unique/distinct rows from a data frame. This is similar to ​​unique.data.frame()​​ but considerably faster.

library(dplyr)
df.0 <- distinct(df, grade, .keep_all = T)
> df.0
grade a b c
1 A 1 6 10
2 B 4 3 13
3 C 6 1 15

aggregate()

Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form.

df.1 <- aggregate(df[,-1], list(df$grade), FUN=sum)
> df.1
Group.1 a b c
1 A 6 15 33
2 B 9 5 27
3 C 6 1 15