I need to calculate when a value switched between 0 and 1, values are distributed across columns, the switch is not given, and NAs are present.
I attempted with mutate
and rowSums
with little results.
Example:
df <- data.frame(entry = c(1:5),
year_1 = c(NA, NA, NA, 1, NA),
year_2 = c(NA, NA, 0, 0, 1),
year_3 = c(NA, 1, 1, 0, 1))
Desired result:
switch = c(NA, NA, "year_2", NA, NA)