0

Hello everyone I'm looking for help in order to aggregate sum of columns within df$Names

Here is the df

Names COL1 COL2 COL3 COL4 
A     2    2    0    1
A     3    1    1    1
A     3    2    0    1
A     4    0    4    0 
B     1    1    0    0
B     3    1    1    1

The expected output is :

Names COL1 COL2 COL3 COL4 
A     12   5    5    3
B     4    2    1    1

Here are the data :

structure(list(Names = structure(c(1L, 1L, 1L, 1L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), COL1 = c(2L, 3L, 3L, 4L, 1L, 3L), COL2 = c(2L, 
1L, 2L, 0L, 1L, 1L), COL3 = c(0L, 1L, 0L, 4L, 0L, 1L), COL4 = c(1L, 
1L, 1L, 0L, 0L, 1L)), class = "data.frame", row.names = c(NA, 
-6L))

I tried:

aggregate(cbind(COL1,COL2,COL3,COL4) ~ Names, data = df, sum, na.rm = TRUE)
chippycentra
  • 1,719
  • 3
  • 10
  • 2
    What is the issue you are facing? Your code works and gives expected output. You can shorten it to `aggregate(. ~ Names, data = df, sum, na.rm = TRUE)` – Ronak Shah Mar 10 '21 at 10:14

1 Answers1

1

Does this work:

library(dplyr)
df %>% group_by(Names) %>% summarise(across(starts_with('COL'), ~ sum(.)))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 2 x 5
  Names  COL1  COL2  COL3  COL4
  <chr> <dbl> <dbl> <dbl> <dbl>
1 A        12     5     5     3
2 B         4     2     1     1
Karthik S
  • 7,798
  • 2
  • 6
  • 20