How to calculate sum with dplyr and purrr?

With {dplyr} and {purrr} I would like to calculate the sum of each numerical column that begins with "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
)

What I want :

result <- tribble(
  ~categ, ~eff_21, ~eff_22,
  "a",  1,   8,
  "b",  2,   11,
  "c",  7,   7
) 

I tried but it creates several data.frames and it is long, that’s why I want to use {purrr} because in my real working data.frame I have more columns than "21" and "22" :

mydf %>% 
  group_by(categ_21) %>% 
  summarise(total_21 = sum(eff_21))

mydf %>% 
  group_by(categ_22) %>% 
  summarise(total_22 = sum(eff_22))

Thanks!

>Solution :

In this particular case, you may find it convenient to pivot long, and then back to wide:

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_")

Output:

  categ eff_21 eff_22
  <chr>  <dbl>  <dbl>
1 a          1      8
2 b          2     11
3 c          7      7

Leave a Reply