I would like to turn several columns after a certain one to NA, depending on the value of another column.
Here the initial dataset:
x.1 x.2 x.3 x.4 x.5 x.6 x.7 t
1 a b d a a d b 4
2 b d d a b a b 3
3 b a d a a c a 4
4 c b c a a c a 1
reproducible with:
data <- data.frame(x.1 = c("a","b","b","c"), x.2 = c("b","d","a","b"), x.3 = c("d","d","d","c"), x.4 = c("a","a","a","a"), x.5 = c("a","b","a","a"),x.6 = c("d","a","c","c"), x.7 = c("b","b","a","a"), t = c(4,3,4,1))
Here the dataset I would like to obtain, with NA for each x-column, after the corresponding t. I.e. in both rows 1 and 3, t is equal to 4, so nothing will change in x.1, x.2, x.3, x.4, and all the x after that point (x.5, x.6, x.7) become NA. In row 2 t is 3, so from x.4 columns will turn into NA, etc.
x.1 x.2 x.3 x.4 x.5 x.6 x.7 t
1 a b d a NA NA NA 4
2 b d d NA NA NA NA 3
3 b a d a NA NA NA 4
4 c NA NA NA NA NA NA 1
I will have a large dataset with 156 x-columns, so I'd a quick way to do it.