I have a data.table my.data.table and a character vector i (length one) corresponding a to a colname of my.data.table. Using i, I would like to extract the corresponding column of my.data.table as a vector that is not of class data.table or data.frame.
How would I do this?
> my.data.table <- data.table(a=1:2,b=2:3)
> i <- "a"
> class(my.data.table[,i,with=FALSE])
[1] "data.table" "data.frame"
> as.vector(my.data.table[,i,with=FALSE]) ##does not work
a
1: 1
2: 2
> is.vector(as.vector(my.data.table[,i,with=FALSE])) ##strange behavior
[1] FALSE
>
I assume there’s a way to do this using with=FALSE and eval(i, <env>) in j but I can’t figure it out.
Either one of these will work in your example, but the second of the two is more generally useful.
..indata.tablemeans to “look up one level” (in this case for the variablei)