I have the following, simplified data frame.
obj <- data.frame (id = c(1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2),
Date = c("1990-01", "1990-02", "1990-03", "1990-04", "1990-01", "1990-02", "1990-03", "1990-04", "1991-01", "1991-02", "1991-03", "1991-04", "1991-01", "1991-02", "1991-03", "1991-04"),
degree = c(1, 1, 1, 2, 1, 1, 0, 0, 1, 2, 2, 2, 1, 1, 2, 1)
)
Note that my real ID's are named something like 2.01811e+13
What I would like to do is the following:
- I would like to create a new column which should be 1 for a date where
degree
turns to 2 and the previous observation is 1 for the same ID. - For every
degree
= 1 the new column should be zero. - For any
degree
= 0 the new column should take NA. - For any other
degree
= 2 (so if the previous degree of the same id observation was not 1) the new column should be NA as well.
Note that consecutive id observations might overlap across years in the real sample, e.g. an ID ranging from 1990-11, 1990-12, 1991-01, 1991-02.
it should look like this,
id date new_col
1 1990-01 0
1 1990-02 0
1 1990-03 0
1 1990-04 1
2 1990-01 0
2 1990-02 0
2 1990-03 NA
2 1990-04 NA
1 1991-01 0
1 1991-02 1
1 1991-03 NA
1 1991-04 NA
2 1991-01 0
2 1991-02 0
2 1991-03 1
2 1991-04 0
Many thanks in advance!