Home > Net >  Summarize all column from a dataset on another column without using their column names
Summarize all column from a dataset on another column without using their column names

Time:01-31

I have the dataframe below and I want to sum every column based on the activity column and this is how I do it below. The issue is that because my dataset is goin to be dynamic I will have diffrent column names every time so I need a way to sum without using the column names (except from the activity).

vol<-structure(list(activity = c("RAAMELK", "Separering", "Sweetmilk Pasteurizer 8332", 
"9005 - T51 kartong 70x70", "RAAMELK", "Separering", "Sweetmilk Pasteurizer 8331", 
"9004 - T42 kartong 70x70", "9006 - T61 BIB", "9004 - T41 kartong 70x70"
), qty.in = c(0, 19976.92, 17590.92, 17480, 0, 31, 31, 6, 3, 
28), qty_scrap.in = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), qty.out = c(19976.92, 
2386, 17481, 17694, 31, 9, 31, 6, 3, 28), qty_scrap.out = c(0, 
0, 109.92, -214, 0, 0, -270.64, 524, 260, 0)), class = "data.frame", row.names = c(1L, 
3L, 5L, 7L, 9L, 11L, 13L, 15L, 17L, 19L))

library(dplyr)
vol<-vol %>% group_by(activity) %>%
      summarize(qty.in = sum(qty.in),
                qty_scrap.in = sum(qty_scrap.in),
                qty.out = sum(qty.out),
                qty_scrap.out=sum(qty_scrap.out))

CodePudding user response:

vol |> group_by(activity) |>
  summarise(across(.cols=everything(),
                   .fns=sum))
  •  Tags:  
  • r
  • Related