The data frame contains an identifier, an estimate, and several binary variables (0,1)
ID <- c(1,2,3,4,5,6,7,8,9,10)
grade <- c("a", "b", "e", "a", "d", "d", "a", "c", "c", "b")
b1 <- c(1,0,0,0,0,0,0,0,0,0)
b2 <- c(1,1,0,0,0,1,0,1,0,0)
b3 <- c(1,0,0,1,1,0,0,1,0,0)
b4 <- c(1,1,0,0,0,1,0,1,0,0)
b5 <- c(1,1,1,1,1,1,0,1,1,0)
b6 <- c(1,1,1,1,1,1,1,1,1,0)
df <- data.frame(ID, grade, b1, b2, b3, b4, b5, b6)
I need to create a new integer column (name it y) that has values from 1 to 6
Their way to calculating y is to return the position of the first 1 in (from b1 to b6), in which after that the values in the row are the only ones.
For instance:
for ID=1, y=1
ID=2, y=4
ID=3, y=5
However, if all values are zeros in b1-b6, then return no.
Also, the faster the code, the better.