Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

vol |> group_by(activity) |>
  summarise(across(.cols=everything(),
                   .fns=sum))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading