Orion
Orion

Reputation: 1104

Using column index in data.table condition

dt <- data.table(id=c(8,5,4,9,2,7), col1=c(2,1,2,2,3,1), col2=c(1,1,1,2,3,1))

   id col1 col2
1:  8    2    1
2:  5    1    1
3:  4    2    1
4:  9    2    2
5:  2    3    3
6:  7    1    1

I want to subset dt to get only rows that contain the value 2 in any of col1 or col2 , using column indices rather than names.

Edit: To be clear, I'm interested in situations where only column indices are known (names are unknown).

Upvotes: 0

Views: 1265

Answers (3)

promagma
promagma

Reputation: 116

This works

> dt <- data.table(id=c(8,5,4,9,2,7), col1=c(2,1,2,2,3,1), col2=c(1,1,1,2,3,1))
> dt[dt[[2]] == 2 | dt[[3]] == 2]
   id col1 col2
1:  8    2    1
2:  4    2    1
3:  9    2    2

Upvotes: 1

Colonel Beauvel
Colonel Beauvel

Reputation: 31161

dt[rowSums(dt[,grep('col',names(dt)), with=F]==2)!=0,]
   id col1 col2
1:  8    2    1
2:  4    2    1
3:  9    2    2

With column indices:

dt[rowSums(dt[,c(2,3), with=F]==2)!=0,]

Upvotes: 2

lukeA
lukeA

Reputation: 54237

You could use the .SD and .SDcols:

dt[dt[, Reduce("|", lapply(.SD, function(x) x==2)), .SDcols=2:3]]

Upvotes: 4

Related Questions