I have a data frame (df) below, and I want to add an extra column result
using dplyr, which will take the value 1, if z == "gone"
and where x
is the maximum value for the group y
.
y x z
1 a 3 gone
2 a 5 gone
3 a 8 gone
4 a 9 gone
5 a 10 gone
6 b 1
7 b 2
8 b 4
9 b 6
10 b 7
If I just chose the maximum for each group, this would be:
df %>%
group_by(y) %>%
slice(which.max(x))
which will return:
y x z
1 a 10 gone
2 b 7
This is not what I want. I need to use the maximum value x
for each group in y
, checking if there is z == "gone"
, and if TRUE
1 otherwise, 0. It will look like this:
y x z result
1 a 3 gone 0
2 a 5 gone 0
3 a 8 gone 0
4 a 9 gone 0
5 a 10 gone 1
6 b 1 0
7 b 2 0
8 b 4 0
9 b 6 0
10 b 7 0
I suppose I would use the conditional operator in mutate()
, but I cannot find an example. Please inform.
source
share