<span style="font-family:arial, sans-serif;font-size:11px;border-collapse:collapse;color:rgb(34, 34, 34)">Dear data.table friends and maintainers,<div><br></div><div>First, thanks to the authors for this excellent package: it really fills a void in the R world. However, I have a question: I'm looking to have an efficient conversion of a data table object to a vector (of the correct type) when querying a single column whose name is stored in a variable. As per the vignette and the FAQ, I use the syntax</div>
<div><br></div><div> my.data.table[, colname, with=FALSE]</div><div><br></div><div>(where colname is a variable containing my desired column name) but this returns another data table, not a vector. Morever, the eval syntax suggested in the FAQ simply does not work:</div>
<div><br></div><div> my.data.table[, eval(colname)]</div><div><br></div><div>See example below. I could use as.matrix on the result, but this carries out undesirable type conversion in the case of columns containing dates: see below.</div>
<div><br></div><div>Here is an example to reproduce this problem:</div><div><br></div><div><div>> require(data.table)</div><div>Loading required package: data.table</div></div><div><div>> a <- data.table(x=seq(1, 2, by=0.1), y=seq(as.POSIXct("2010-01-01"), as.POSIXct("2010-01-11"), length.out=11))</div>
<div>> a</div><div> x y</div><div> [1,] 1.0 2010-01-01</div><div> [2,] 1.1 2010-01-02</div><div> [3,] 1.2 2010-01-03</div><div> [4,] 1.3 2010-01-04</div><div> [5,] 1.4 2010-01-05</div><div> [6,] 1.5 2010-01-06</div>
<div> [7,] 1.6 2010-01-07</div><div> [8,] 1.7 2010-01-08</div><div> [9,] 1.8 2010-01-09</div><div>[10,] 1.9 2010-01-10</div><div>[11,] 2.0 2010-01-11</div><div>> colname <- "y"</div><div><br></div><div>## The following returns a data table. How can I get a vector, and still preserve type information?</div>
<div>> a[, colname, with=FALSE]</div><div> y</div><div> [1,] 2010-01-01</div><div> [2,] 2010-01-02</div><div> [3,] 2010-01-03</div><div> [4,] 2010-01-04</div><div> [5,] 2010-01-05</div><div> [6,] 2010-01-06</div>
<div> [7,] 2010-01-07</div><div> [8,] 2010-01-08</div><div> [9,] 2010-01-09</div><div>[10,] 2010-01-10</div><div>[11,] 2010-01-11</div><div><br></div><div>## The eval recipe suggested in the FAQ does not work.</div><div>
> a[, eval(colname)]</div>
<div>[1] "y"</div></div><div><br></div><div>## as.vector does not convert away from data.table</div><div><div>> as.vector(a[, colname, with=FALSE])</div><div> y</div><div> [1,] 2010-01-01</div>
<div>
[2,] 2010-01-02</div><div> [3,] 2010-01-03</div><div> [4,] 2010-01-04</div><div> [5,] 2010-01-05</div><div> [6,] 2010-01-06</div><div> [7,] 2010-01-07</div><div> [8,] 2010-01-08</div><div> [9,] 2010-01-09</div><div>[10,] 2010-01-10</div>
<div>[11,] 2010-01-11</div><div>> class(as.vector(a[, colname, with=FALSE]))</div><div>[1] "data.table"</div></div><div><br></div><div>## as.matrix loses type information (NOTE: in my case it is not acceptable to</div>
<div>## convert this character vector back to a POSIXct, due to the loss of important</div><div>## timezone information. Furthermore, this would be very inefficient.)</div><div><div>> as.matrix(a[, colname, with=FALSE])</div>
<div>
y </div><div> [1,] "2010-01-01"</div><div> [2,] "2010-01-02"</div><div> [3,] "2010-01-03"</div><div> [4,] "2010-01-04"</div><div> [5,] "2010-01-05"</div><div>
[6,] "2010-01-06"</div><div> [7,] "2010-01-07"</div><div> [8,] "2010-01-08"</div><div> [9,] "2010-01-09"</div><div>[10,] "2010-01-10"</div><div>[11,] "2010-01-11"</div>
<div>> mode(as.matrix(a[, colname, with=FALSE]))</div><div>[1] "character"</div></div><div><br></div><div>## Finally, one could go through a data.frame, but this is inefficient</div><div>## and it sorts of defeats the purpose of using data.table...</div>
<div><div>> as.data.frame(a[, colname, with=FALSE])[, colname]</div><div> [1] "2010-01-01 EST" "2010-01-02 EST" "2010-01-03 EST" "2010-01-04 EST"</div><div> [5] "2010-01-05 EST" "2010-01-06 EST" "2010-01-07 EST" "2010-01-08 EST"</div>
<div> [9] "2010-01-09 EST" "2010-01-10 EST" "2010-01-11 EST"</div></div><div><br></div><div><br></div><div>So at this point, my imagination is running out and I'm turning to this list for suggestions. This should seem to be a fairly frequent use-case, and I'm surprised it does not appear to have previously been addressed.</div>
<div><br></div><div>For the record, here is my sessionInfo()</div><div><br></div><div><div>> sessionInfo()</div><div>R version 2.9.2 (2009-08-24) </div><div>x86_64-pc-linux-gnu </div><div><br></div><div>locale:</div><div>
C</div><div><br></div><div>attached base packages:</div><div>[1] stats graphics grDevices utils datasets methods base </div><div><br></div><div>other attached packages:</div><div>[1] data.table_1.4.1</div>
</div><div><br></div><div><br></div><div>Thanks in advance for any help!</div><div>+ Nicolas Chapados</div></span>