使用 {dplyr} 和 {purrr} 我想计算以“eff”开头的每个数字列的总和。
library(dplyr)
library(purrr)
mydf <- tribble(
~categ_21, ~categ_22, ~eff_21, ~eff_22,
"a", "b", 1, 5,
"b", "b", 2, 6,
"c", "c", 3, 7,
"c", "a", 4, 8
)
Run Code Online (Sandbox Code Playgroud)
我想要的是 :
result <- tribble(
~categ, ~eff_21, ~eff_22,
"a", 1, 8,
"b", 2, 11,
"c", 7, 7
)
Run Code Online (Sandbox Code Playgroud)
我尝试过,但它创建了多个 data.frames 并且很长,这就是我想使用 {purrr} 的原因,因为在我真正的工作 data.frame 中,我有比 "21" 和 "22" 更多的列:
mydf %>%
group_by(categ_21) %>%
summarise(total_21 = sum(eff_21))
mydf %>%
group_by(categ_22) %>%
summarise(total_22 = sum(eff_22))
Run Code Online (Sandbox Code Playgroud)
谢谢!
在这种特殊情况下,您可能会发现先旋转长轴,然后再返回宽轴很方便:
library(dplyr)
library(tidyr)
mydf %>%
pivot_longer(everything(),names_to = c(".value", "cat"), names_pattern="(.*)_(.*)") %>%
pivot_wider(categ,names_from = cat,values_from = eff, values_fn = sum,names_prefix = "eff_")
Run Code Online (Sandbox Code Playgroud)
输出:
categ eff_21 eff_22
<chr> <dbl> <dbl>
1 a 1 8
2 b 2 11
3 c 7 7
Run Code Online (Sandbox Code Playgroud)