I'm trying to subset a dataset by selecting some columns from a data.table. However, my code does not work with some variations.
Here is a sample data.table
library(data.table)
DT <- data.table( ID = 1:50,
Capacity = sample(100:1000, size = 50, replace = F),
Code = sample(LETTERS[1:4], 50, replace = T),
State = rep(c("Alabama","Indiana","Texas","Nevada"), 50))
Here is a working subset code, where a numeric sequence of columns is specified using :
:
DT[ , 1:2]
However, specifying the same sequence of columns using seq
does not work:
DT[ , seq(1:2)]
Note that this works with a dataframe but not with a data.table.
I need something along the lines of the second format because I'm subsetting based on the output of grep()
and it gives the same output as the second format. What am I doing incorrectly?
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…