0

I have a data.table looking like this:

dd <- data.table(a= 1:3, `1990` = 4:6, `1991` = 7:9, `1992` = 10:12)

I want to select columns that essentially correspond to integers.

The following code would work if dd is a data.frame but fails if dd is a data.table

dd[,as.character(1990:1992)]

The following does not work either:

dd[,.(1990:1992)]

Is there a solution that mimics the data.frame solution mentioned?

user436994
  • 601
  • 5
  • 15
  • 1
    You need to use `.SDcols`, i.e. `cols <- as.character(1990:1992); dd[, .SD, .SDcols = cols]` – Sotos Aug 22 '23 at 09:15
  • To select columns whose name consist only of numbers: `dd[, .SD, .SDcols = patterns("^[0-9]+$")] ` – s_baldur Aug 22 '23 at 09:52

0 Answers0