gpt4 book ai didi

r - 使用 dplyr 进行汇总并保持相同的变量名

转载 作者:行者123 更新时间:2023-12-02 06:50:01 25 4
gpt4 key购买 nike

我发现 data.table 和 dplyr 在尝试做同样的事情时有不同的结果。我想使用 dplyr 语法,但让它以 data.table 的方式进行计算。用例是我想将小计添加到表中。为此,我需要对每个变量进行一些聚合,但然后保留相同的变量名称(在转换后的版本中)。 Data.table 允许我对变量执行一些聚合并保持相同的名称。然后使用同一变量进行另一个聚合。它将继续使用未转换的版本。但是,Dplyr 将使用转换后的版本。

总结文档中,它说:

# Note that with data frames, newly created summaries immediately
# overwrite existing variables
mtcars %>%
group_by(cyl) %>%
summarise(disp = mean(disp), sd = sd(disp))

这基本上是我遇到的问题,但我想知道是否有一个好的解决方法。我发现的一件事是将转换后的变量命名为其他名称,然后在最后重命名它,但这对我来说看起来不太好。如果有一种很好的方法来进行小计,那么也很高兴知道。我浏览了这个网站,没有看到讨论的具体情况。任何帮助将不胜感激!

这里我做了一个简单的例子,一次使用 data.table 的结果,一次使用 dplyr 的结果。我想采用这个简单的表格并附加一个小计行,它是感兴趣的列(总计)的加权平均值。

library(data.table)
library(dplyr)

dt <- data.table(Group = LETTERS[1:5],
Count = c(1000, 1500, 1200, 2000, 5000),
Total = c(50, 300, 600, 400, 1000))
dt[, Count_Dist := Count/sum(Count)]
dt[, .(Count_Dist = sum(Count_Dist), Weighted_Total = sum(Count_Dist*Total))]

dt <- rbind(dt[, .(Group, Count_Dist, Total)],
dt[, .(Group = "All", Count_Dist = sum(Count_Dist), Total = sum(Count_Dist*Total))])
setnames(dt, "Total", "Weighted_Avg_Total")

dt

df <- data.frame(Group = LETTERS[1:5],
Count = c(1000, 1500, 1200, 2000, 5000),
Total = c(50, 300, 600, 400, 1000))

df %>%
mutate(Count_Dist = Count/sum(Count)) %>%
summarize(Count_Dist = sum(Count_Dist),
Weighted_Total = sum(Count_Dist*Total))

df %>%
mutate(Count_Dist = Count/sum(Count)) %>%
select(Group, Count_Dist, Total) %>%
rbind(df %>%
mutate(Count_Dist = Count/sum(Count)) %>%
summarize(Group = "All",
Count_Dist = sum(Count_Dist),
Total = sum(Count_Dist*Total))) %>%
rename(Weighted_Avg_Total = Total)

再次感谢您的帮助!

最佳答案

一个可能的解决方案是跳过 mutate 步骤并使用 transmute 进行第一个 mutate/select -步骤并直接从原始变量计算所需的变量,而无需为第二个 mutate 步骤创建中间变量:

df %>% 
transmute(Group, Count_Dist = Count/sum(Count), Weighted_Avg_Total = Total) %>%
bind_rows(df %>%
summarize(Group = "All",
Count_Dist = sum(Count/sum(Count)),
Weighted_Avg_Total = sum((Count/sum(Count))*Total)))

给出:

  Group Count_Dist Weighted_Avg_Total
1 A 0.09345794 50.0000
2 B 0.14018692 300.0000
3 C 0.11214953 600.0000
4 D 0.18691589 400.0000
5 E 0.46728972 1000.0000
6 All 1.00000000 656.0748
<小时/>

另一种可能的解决方案是更改在 dplyr 中计算新变量的顺序,然后使用 select 将列顺序恢复为您最初想要的值:

df %>% 
mutate(Count_Dist = Count/sum(Count)) %>%
select(Group, Count_Dist, Weighted_Avg_Total = Total) %>%
bind_rows(df %>%
mutate(Count_Dist = Count/sum(Count)) %>%
summarize(Group = "All",
Weighted_Avg_Total = sum(Count_Dist*Total),
Count_Dist = sum(Count_Dist)) %>%
select(Group, Count_Dist, Weighted_Avg_Total))
<小时/>

如果您还想包含 Count 列,您可以这样做(基于我下面的评论):

df %>% 
transmute(Group = Group, Count_Dist = Count/sum(Count), Weighted_Avg_Total = Total, Count) %>%
bind_rows(df %>%
summarize(Group = "All",
Count_Dist = sum(Count/sum(Count)),
Weighted_Avg_Total = sum((Count/sum(Count))*Total),
Count = sum(Count)))

关于r - 使用 dplyr 进行汇总并保持相同的变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357867/

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