gpt4 book ai didi

r - 为因子的每个水平附加一行总和

转载 作者:行者123 更新时间:2023-12-01 23:07:11 27 4
gpt4 key购买 nike

我想像这样为每个 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

已编辑

我想要同时拥有 dplyrdata.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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com