0

I want to make a new column that is the rowwise total of subgroups of a different variable. For example, if my dataset looks like this:

df <- tribble(
  ~name, ~point, ~time,
  "A", 1, 1,
  "B", 1, 1,
  "W", 2, 1,
  "A", 3, 2,
  "B", 1, 2,
  "W", 4, 2,
)

# A tibble: 6 x 3
  name  point  time
  <chr> <dbl> <dbl>
1 A         1     1
2 B         1     1
3 W         2     1
4 A         3     2
5 B         1     2
6 W         4     2

I want it to look like this after (totaling point by time):

df <- tribble(
  ~name, ~point, ~time, ~total,
  "A", 1, 1, 4,
  "B", 1, 1, 4,
  "W", 2, 1, 4,
  "A", 3, 2, 8,
  "B", 1, 2, 8,
  "W", 4, 2, 8,
)

# A tibble: 6 x 4
  name  point  time total
  <chr> <dbl> <dbl> <dbl>
1 A         1     1     4
2 B         1     1     4
3 W         2     1     4
4 A         3     2     8
5 B         1     2     8
6 W         4     2     8

I wanted to use dplyr but I can't figure out how to properly group these items and then put them back into a column. I got a vectorized result by using aggregate() but I don't know how to map that back onto the main dataframe. :(

Thank you!

1 Answers1

2

Use mutate to create a sum by group and keep all the rows.

df %>% 
  group_by(time) %>% 
  mutate(total = sum(point))

# A tibble: 6 x 4
# Groups:   time [2]
  name  point  time total
  <chr> <dbl> <dbl> <dbl>
1 A         1     1     4
2 B         1     1     4
3 W         2     1     4
4 A         3     2     8
5 B         1     2     8
6 W         4     2     8
Maël
  • 45,206
  • 3
  • 29
  • 67