Lets say I have the following data:
library(tidyverse)
cf <- data.frame(x = c("a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "c", "c", "d", "d", "e", "e"),
y = c("free", "with", "sus", "sus", "free", "with", "free", "sus", "sus", "with", "free", "sus", "free", "free", "with", "sus"))
> cf
x y
1 a free
2 a with
3 a sus
4 a sus
5 b free
6 b with
7 b free
8 b sus
9 c sus
10 c with
11 c free
12 c sus
13 d free
14 d free
15 e with
16 e sus
I want to obtain an indicator variable equal to 1 if the last value of y
by group is equal to sus
and the last value before sus
was with
. There can be a multiple sus
before with
occurs as in group a
. If free
occurs last first before with
then its zero.
Desired output:
x y sus_with
1 a free 0
2 a with 0
3 a sus 0
4 a sus 1
5 b free 0
6 b with 0
7 b free 0
8 b sus 0
9 c sus 0
10 c with 0
11 c free 0
12 c sus 0
13 d free 0
14 d free 0
15 e with 0
16 e sus 1
I was thinking something like this:
cf %>%
group_by(x) %>%
mutate(sus_with = if_else(row_number() == n() & y == "sus" & last(y == "with" & y != "free"), 1, 0))
refs:
How to find the date after the last occurrence of a certain observation in R?
How to find the last occurrence of a certain observation in grouped data in R?
any suggestions please?