Summing column in a dataframe

Hi,

I have an easy question. I have dataframe:

df <- data.frame(
  stringsAsFactors = FALSE,
  a= c(1,1,1,1,1,2,2,2,2,3,3),
  b = c("a","a","b","b","c","a","a","a","b","a","a"),
  c= c(11,22,33,44,55,66,77,88,99,12,14)

)

and I would like to sum values in 'c' for groups of rows with the same values in 'a' and 'b' and add these sums as a new column 'd' to this dafaframe for each group of rows (sharing the same values in 'a' and 'b').

Many thanks

This is a job for group_by() and summarise().

library(dplyr, warn.conflicts = FALSE)

df <- data.frame(
  stringsAsFactors = FALSE,
  a = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3),
  b = c("a", "a", "b", "b", "c", "a", "a", "a", "b", "a", "a"),
  c = c(11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 14)
)

df %>% 
  group_by(a, b) %>% 
  summarise(d = sum(c), .groups = "drop")
#> # A tibble: 6 x 3
#>       a b         d
#>   <dbl> <chr> <dbl>
#> 1     1 a        33
#> 2     1 b        77
#> 3     1 c        55
#> 4     2 a       231
#> 5     2 b        99
#> 6     3 a        26

Created on 2020-06-10 by the reprex package (v0.3.0)

1 Like

If your issue has been resolved, please mark the relevant post as a solution.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Actually, I wanted to keep all the rows and just add a new column. Could you help me with that?

i.e.

a b c d
1 a 11 33
1 a 22 33
1 b 33 77
1 b 44 77
....

Many thanks
....

Use mutate() instead.

library(dplyr, warn.conflicts = FALSE)

df <- data.frame(
  stringsAsFactors = FALSE,
  a = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3),
  b = c("a", "a", "b", "b", "c", "a", "a", "a", "b", "a", "a"),
  c = c(11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 14)
)

df %>% 
  group_by(a, b) %>% 
  mutate(d = sum(c))
#> # A tibble: 11 x 4
#> # Groups:   a, b [6]
#>        a b         c     d
#>    <dbl> <chr> <dbl> <dbl>
#>  1     1 a        11    33
#>  2     1 a        22    33
#>  3     1 b        33    77
#>  4     1 b        44    77
#>  5     1 c        55    55
#>  6     2 a        66   231
#>  7     2 a        77   231
#>  8     2 a        88   231
#>  9     2 b        99    99
#> 10     3 a        12    26
#> 11     3 a        14    26

Created on 2020-06-11 by the reprex package (v0.3.0)

1 Like