1

Let say I have below data.table

library(data.table)
DT <- data.table(x=sample(letters, 1e6, TRUE), y=rnorm(1e6), v=runif(1e6))

Now I want to subset DT based on the 1st column value equals to letters[1:2]

If the chose column by name, then this is straighforward -

DT[x %in% letters[1:2]]

However I want to select column by placement i.e. first column or 4th column etc

Below code doesnt work

DT[1 %in% letters[1:2]]

Any pointer on what will be right syntax to select column based on placement, would be helpful

Bogaso
  • 2,838
  • 3
  • 24
  • 54
  • Related: [Select multiple columns in data.table by their numeric indices](https://stackoverflow.com/questions/13383840/select-multiple-columns-in-data-table-by-their-numeric-indices) – markus Jul 05 '19 at 18:47

1 Answers1

3

You can do this :

DT[DT[[1]] %in% letters[1:2]]
fmarm
  • 4,209
  • 1
  • 17
  • 29