A colleague was working with data describing time series within individuals and was looking for a way to set up a variable that would switch "on" at the first instance of a flag variable value within each individual.
Her initial question was "Is there a way to do loops with the tidyverse?". Well, yes, but... fortunately no loops are required for the solution...
Here's the reprex of the solution:
Creating a reprex dataset
Subject 1 discontinues at TIME=3
Subject 2 discontinues but has no further observations
Subject 3 does not discontinue
library(tidyverse)
myData <- tribble(~ID, ~TIME, ~DV, ~FLAG,
1, 1, 0.1, 194,
1, 2, 0.2, 194,
1, 3, 0.3, 5000,
1, 4, 0.1, 194,
2, 1, 0.1, 194,
2, 2, 0.2, 5000,
3, 1, 0.1, 194,
3, 2, 0.2, 194,
3, 3, 0.3, 194,
3, 4, 0.4, 194)
Want to set the discontinuation (DISC) variable to
have value 0 until we see the first instance of FLAG == 5000
within each individual.
myData %>%
group_by(ID) %>%
mutate(DISC = case_when(row_number()==1 ~ 0,
FLAG == 5000 ~ 1)) %>%
fill(DISC)
#> # A tibble: 10 x 5
#> # Groups: ID [3]
#> ID TIME DV FLAG DISC
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0.1 194 0
#> 2 1 2 0.2 194 0
#> 3 1 3 0.3 5000 1
#> 4 1 4 0.1 194 1
#> 5 2 1 0.1 194 0
#> 6 2 2 0.2 5000 1
#> 7 3 1 0.1 194 0
#> 8 3 2 0.2 194 0
#> 9 3 3 0.3 194 0
#> 10 3 4 0.4 194 0
Created on 2019-06-27 by the reprex package (v0.2.1)