如何在R中按组对变量求和?
在本文中,让我们讨论如何在 R 编程语言中按组查找变量的总和。
使用中的数据集:
预期输出:
方法
- 创建数据框
- 将值设置为所需参数
- 传递给正在使用的函数
- 显示数据框
方法一:使用聚合函数
在这种方法中,我们将借助聚合函数按组求和。聚合函数将数据拆分为子集,计算每个子集的汇总统计信息,并以方便的形式返回结果。
句法:
aggregate(x = dataset_Name , by = group_list, FUN = any_function) # Basic R syntax of aggregate function
例子:
R
GFG <- data.frame(
Category = c ("A","B","C","B","C","A","C","A","B"),
Frequency= c(9,5,0,2,7,8,1,3,7)
)
aggregate(x= GFG$Frequency,
by = list(GFG$Category),
FUN = sum)
R
library("dplyr")
GFG <- data.frame(
Category = c ("A","B","C","B","C","A","C","A","B"),
Frequency= c(9,5,0,2,7,8,1,3,7)
)
GFG%>%
group_by(Category) %>%
summarise_at(vars(Frequency),
list(name = sum))
输出:
方法二:使用dplyr
dplyr 是一个包,它提供了一组工具来有效地操作 R 中的数据集
Methods in dplyr package
- mutate() adds new variables that are functions of existing variables
- select() picks variables based on their names.
- filter() picks cases based on their values.
- summarise() reduces multiple values down to a single summary.
- arrange() changes the ordering of the rows.
在使用这个包之前,你必须安装它
程序:
电阻
library("dplyr")
GFG <- data.frame(
Category = c ("A","B","C","B","C","A","C","A","B"),
Frequency= c(9,5,0,2,7,8,1,3,7)
)
GFG%>%
group_by(Category) %>%
summarise_at(vars(Frequency),
list(name = sum))
输出: