DT = data.table(
id = 1:5,
a = c(0,1,0,2,5),
b = c(1,0,2,4,4),
c = c(1,2,0,0,5))
# id a b c
# 1: 1 0 1 1
# 2: 2 1 0 2
# 3: 3 0 2 0
# 4: 4 2 4 0
# 5: 5 5 4 5
I want to identify the first column from the left which has 0, and put the column index in idx
.
# id a b c idx
# 1: 1 0 1 1 2
# 2: 2 1 0 2 3
# 3: 3 0 2 0 2
# 4: 4 2 4 0 4
# 5: 5 5 4 5 NA
(Non-data.table
solutions, e.g., with dplyr
are also welcome)