我试图将一个因子的每个级别的数字列的值求和,但还要得到结果数据帧中所有级别的总和。
例如:
# Type gender population
# A male 100
# B male 150
# A female 125
# B female 175
使用聚合函数,我可以获得:
aggregate(population ~ gender, df, sum)
# gender population
# male 250
# female 300
但是有没有办法得到同时将两个水平相加的输出?
# gender population
# all 550
# male 250
# female 300
这可以通过proc制表在SAS中轻松完成,希望也可以使用R来实现。
预先感谢
编辑
给出的两个答案都行得通,但是我试图找到一种不那么特别的解决方案。我正在寻找可以在多个变量上使用的东西,例如,对于更复杂的数据框,这样的输出:
# Type gender population
# all all 500
# all male 200
# all female 300
# A all 250
# A male 100
# A female 150
# B all 250
# B male 100
# B female 150
我很抱歉,如果还不够清楚的话。
答案 0 :(得分:4)
您可以rbind
,即
d1 <- aggregate(population ~ gender, df, sum)
rbind(data.frame(gender = 'total', population = sum(d1$population)), d1)
# gender population
#1 total 550
#2 female 300
#3 male 250
答案 1 :(得分:3)
我们可以将 xtabs 与 addmargins 结合使用以获得总计:
df1 <- read.table(text = "
Type gender population
A male 100
B male 150
A female 125
B female 175", header = TRUE, stringsAsFactors = FALSE)
df2 <- read.table(text = "
Type gender population
all all 500
all male 200
all female 300
A all 250
A male 100
A female 150
B all 250
B male 100
B female 150", header = TRUE, stringsAsFactors = FALSE)
data.frame(addmargins(xtabs(population ~ gender, df1)))
# gender Freq
# 1 female 300
# 2 male 250
# 3 Sum 550
data.frame(addmargins(xtabs(population ~ gender, df2)))
# gender Freq
# 1 all 1000
# 2 female 600
# 3 male 400
# 4 Sum 2000
答案 2 :(得分:2)
还带有软件包janitor
:
x <- aggregate(population ~ gender, d, FUN=sum)
library(janitor)
adorn_totals(x, "row")
#gender population
#female 300
# male 250
# Total 550