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