我想像这样为每个Reg附加一行总和
Reg Res Pop
1 Total 1000915
2 A Urban 500414
3 A Rural 500501
4 Total 999938
5 B Urban 499922
6 B Rural 500016
7 Total 1000912
8 C Urban 501638
9 C Rural 499274
10 Total 999629
11 D Urban 499804
12 D Rural 499825
13 Total 1000303
14 E Urban 499917
15 E Rural 500386
MWE如下:
Reg <- rep(LETTERS[1:5], each = 2)
Res <- rep(c("Urban", "Rural"), times = 5)
set.seed(12345)
Pop <- rpois(n = 10, lambda = 500000)
df <- data.frame(Reg, Res, Pop)
df
Reg Res Pop
1 A Urban 500414
2 A Rural 500501
3 B Urban 499922
4 B Rural 500016
5 C Urban 501638
6 C Rural 499274
7 D Urban 499804
8 D Rural 499825
9 E Urban 499917
10 E Rural 500386
df %>%
group_by(Reg) %>%
summarise(Total = sum(Pop))
# A tibble: 5 x 2
Reg Total
<fctr> <int>
1 A 1000915
2 B 999938
3 C 1000912
4 D 999629
5 E 1000303
编辑
我想同时拥有
dplyr
和data.table
解决方案。 最佳答案
您可以在摘要中添加一个额外的Res列,然后使用原始数据框添加bind_rows
:
df %>%
group_by(Reg) %>%
summarise(Pop = sum(Pop), Res = 'Total') %>%
bind_rows(df) %>%
arrange(Reg)
# A tibble: 15 x 3
# Reg Pop Res
# <chr> <int> <chr>
# 1 A 1000915 Total
# 2 A 500414 Urban
# 3 A 500501 Rural
# 4 B 999938 Total
# 5 B 499922 Urban
# 6 B 500016 Rural
# 7 C 1000912 Total
# 8 C 501638 Urban
# 9 C 499274 Rural
#10 D 999629 Total
#11 D 499804 Urban
#12 D 499825 Rural
#13 E 1000303 Total
#14 E 499917 Urban
#15 E 500386 Rural
相应的
data.table
解决方案:dt <- setDT(df)
rbindlist(list(dt[, .(Pop = sum(Pop), Res = 'Total'), Reg], dt), use.names = TRUE)
关于r - 为因子的每个级别追加一行总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46122047/