I have binary data as below:
ID <- c("A", "B", "C", "D", "E", "F")
Q0 <- c(0, 0, 0, 0, 0, 0)
Q1 <- c(0, 1, 0, 0, NA, 1)
Q2 <- c(0, NA, 1, 0, NA, 1)
Q3 <- c(0, NA, NA, 1, NA, 1)
Q4 <- c(0, NA, NA, 1, NA, 1)
dta <- data.frame(ID, Q0, Q1, Q2, Q3, Q4)
If there is 1 for a row in one of the columns, all the subsequent columns should be 1 as well. If there is 0 or NA, the next column should stay as is.
Stated differently, how can I change the value of multiple columns based conditionally on the value of a column in a relative position?
The intended output for the above data frame is:
ID Q0 Q1 Q2 Q3 Q4
A 0 0 0 0 0
B 0 1 1 1 1
C 0 0 1 1 1
D 0 0 0 1 1
E 0 NA NA NA NA
F 0 1 1 1 1
How can I do this? Perhaps using apply
or a for
loop?