Reputation: 1104
Let DT
be a data.table:
DT <- data.table (col.1 = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3),
col.2 = c(0, 1, 1, 0, 0, 0, 1, 0, 1, 1),
#...
col.n = c(0, 2, 1, 0, 0, 2, 1, 0, 0, 1),
flag = 0)
DT
col.1 col.2 col.n flag
1: 1 0 0 0
2: 1 1 2 0
3: 1 1 1 0
4: 2 0 0 0
5: 2 0 0 0
6: 2 0 2 0
7: 3 1 1 0
8: 3 0 0 0
9: 3 1 0 0
10: 3 1 1 0
How to set flag = T
(or 1) where col.1 + col.2 + col.n > 4
, without using the column names?
Upvotes: 1
Views: 165
Reputation: 697
DT[ , flag := fifelse(rowSums(.SD)>4, 1, flag)), .SDcols = !='flag']
Upvotes: 0
Reputation: 887118
Another option is to use Reduce
DT[, flag := (Reduce(`+`, .SD)>4)+0L, .SDcols=-'flag'][]
Upvotes: 1