1

How do I apply / set the result of a group_by + summarize as a new variable for all observations of that group?

Let's say I have a tibble like follows:

foo <- tribble(
    ~x, ~y, 
     1, 1, 
     1, 5, 
     1, 2,  
     2, 1, 
     2, 7, 
     2, 3)

x signifying the group, y a variable.
I know I can get the max-y of each group via foo %>% group_by(x) %>% summarize(max(y)).

How do I set this result now though as a new column for the entire group?
I.e. resulting in

~x, ~y, ~max-y
 1,  1,  5
 1,  5,  5
 1,  2,  5
 2,  1,  7
 2,  7,  7
 2,  3,  7
Dan
  • 515
  • 6
  • 20
TommyF
  • 6,660
  • 8
  • 37
  • 61

1 Answers1

2

One can simply use mutate after group by and specify the function:

foo %>%
  group_by(x) %>%
  mutate(max_y = max(y))
#output:
# A tibble: 6 x 3
# Groups:   x [2]
      x     y max_y
  <dbl> <dbl> <dbl>
1     1     1     5
2     1     5     5
3     1     2     5
4     2     1     7
5     2     7     7
6     2     3     7
missuse
  • 19,056
  • 3
  • 25
  • 47