Extract a column from a data.table as a vector, by position

A data.table inherits from class data.frame. Therefore it is a list (of column vectors) internally and can be treated as such.

is.list(DT)
#[1] TRUE

Fortunately, list subsetting, i.e. [[, is very fast and, in contrast to [, package data.table doesn’t define a method for it. Thus, you can simply use [[ to extract by an index:

DT[[2]]
#[1] 3 4

Leave a Comment