如何在R中按组计算总和?
在本文中,我们将了解如何在 R 编程语言中按组计算总和。
演示数据
R
# creating data frame
df <- data.frame(Sub = c('Math', 'Math', 'Phy', 'Phy',
'Phy', 'Che', 'Che'),
Marks = c(8, 2, 4, 9, 9, 7, 1),
Add_on = c(3, 1, 9, 4, 7, 8, 2))
# view dataframe
df
R
# creating data frame
df <- data.frame(Sub = c('Math', 'Math', 'Phy', 'Phy',
'Phy', 'Che', 'Che'),
Marks = c(8, 2, 4, 9, 9, 7, 1),
Add_on = c(3, 1, 9, 4, 7, 8, 2))
aggregate(df$Marks, list(df$Sub), FUN=sum)
aggregate(df$Add_on, list(df$Sub), FUN=sum)
R
library(dplyr)
df %>%
group_by(Sub) %>%
summarise_at(vars(Marks),
list(name = sum))
R
library(data.table)
# convert data frame to data table
setDT(df)
# find sum of points scored by sub
df[ ,list(sum=sum(Marks)), by=Sub]
输出:
Sub Marks Add_on
Math 8 3
Math 2 1
Phy 4 9
Phy 9 4
Phy 9 7
Che 7 8
Che 1 2
方法 1:在 Base R 中使用 aggregate() 方法
aggregate()函数用于按组获取数据的汇总统计信息。统计数据包括平均值、最小值、总和。最大等
Syntax: aggregate(dataframe$aggregate_column, list(dataframe$group_column), FUN)
where
- dataframe is the input dataframe.
- aggregate_column is the column to be aggregated in the dataframe.
- group_column is the column to be grouped with FUN.
- FUN represents sum/mean/min/ max.
R
# creating data frame
df <- data.frame(Sub = c('Math', 'Math', 'Phy', 'Phy',
'Phy', 'Che', 'Che'),
Marks = c(8, 2, 4, 9, 9, 7, 1),
Add_on = c(3, 1, 9, 4, 7, 8, 2))
aggregate(df$Marks, list(df$Sub), FUN=sum)
aggregate(df$Add_on, list(df$Sub), FUN=sum)
输出:
Group.1 x
Che 8
Math 10
Phy 22
Group.1 x
Che 10
Math 4
Phy 20
方法二:使用 dplyr() 包
group_by()函数后跟summarise()函数,并执行适当的操作。
R
library(dplyr)
df %>%
group_by(Sub) %>%
summarise_at(vars(Marks),
list(name = sum))
输出:
Sub name
Che 8
Math 10
Phy 22
方法3:使用data.table
data.table 包,用于计算团队得分的总和。
R
library(data.table)
# convert data frame to data table
setDT(df)
# find sum of points scored by sub
df[ ,list(sum=sum(Marks)), by=Sub]
输出:
Sub sum
Math 10
Phy 22
Che 8