Is it possible to select a column of a data.table and get back a vector? In base R, the argument drop=TRUE would do the trick. For example,
library(data.table)
dat <- as.data.table(iris)
dat[,"Species"] # returns data.table
dat[,"Species", drop=TRUE] # same
iris[, "Species", drop=TRUE] # a factor, wanted result
Is there a way to do this with data.table?
EDIT: the dat[,Species] method is fine, however I need a method where I can pass the column name in a variable:
x <- "Species"
dat[,x, drop=TRUE]
data.tableversions you can just saydat[, Species]. You get afactorhere because you started with a factor from theirisdata set. (I didn't downvote.)dat[,get(x), drop=TRUE](not my downvote either)colnam <- "Species"followed bydat[, ..colnam][[1]]. All this is, methinks, in thedata.tableFAQ ...