Subset of my data:
library(tidyverse)
df <- structure(list(Score = c(18, 18, 20, 20, 19, 19, 19, 19, 19,
19, 19, 19, 19, 17, 17, 17, 17, 17, 19, 19)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
I want to create a column that will track the Score when it changes from being < 19 to >= 19 or vice versa.
I'm currently trying this:
df %>%
mutate(temp = as.numeric(Score < 19))
Score temp
18 1
18 1
20 0
20 0
19 0
19 0
19 0
19 0
19 0
19 0
19 0
19 0
19 0
17 1
17 1
17 1
17 1
17 1
19 0
19 0
However, I want something that looks like this:
Score temp
18 1
18 1
20 2
20 2
19 2
19 2
19 2
19 2
19 2
19 2
19 2
19 2
19 2
17 3
17 3
17 3
17 3
17 3
19 4
19 4
Any help is greatly appreciated.
Edit: ended up using this post.