Hi Matthew,<div><br></div><div>Many thanks for your quick reply.</div><div><br></div><div>I had indeed not realized that a[[colname]] would work just fine. I also found a[, eval(<a href="http://as.name">as.name</a>(colname))] to work, albeit the syntax is messier than the double bracket.</div>
<div><br></div><div>Thanks again,</div><div>+ Nicolas<br><br></div><div><br><div class="gmail_quote">On Tue, Aug 31, 2010 at 3:35 AM, Matthew Dowle <span dir="ltr"><<a href="mailto:mdowle@mdowle.plus.com">mdowle@mdowle.plus.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
Nicolas,<br>
<br>
Welcome to the list.<br>
<br>
Where the documentation mentions 'quoted' it means the quote() function<br>
to create an expression, not as in a character string. Alternatively you<br>
can use [[ in the usual way since a data.table is a list.<br>
<br>
> colexp = quote(y) # rather than "y"<br>
> a[,eval(colexp)]<br>
[1] "2010-01-01 GMT" "2010-01-02 GMT" "2010-01-03 GMT" "2010-01-04 GMT"<br>
[5] "2010-01-05 GMT" "2010-01-06 GMT" "2010-01-07 GMT" "2010-01-08 GMT"<br>
[9] "2010-01-09 GMT" "2010-01-10 GMT" "2010-01-11 GMT"<br>
<br>
or<br>
<br>
> colname = "y"<br>
> a[[colname]]<br>
[1] "2010-01-01 GMT" "2010-01-02 GMT" "2010-01-03 GMT" "2010-01-04 GMT"<br>
[5] "2010-01-05 GMT" "2010-01-06 GMT" "2010-01-07 GMT" "2010-01-08 GMT"<br>
[9] "2010-01-09 GMT" "2010-01-10 GMT" "2010-01-11 GMT"<br>
><br>
<br>
A single column name is a special case of expressions so although this<br>
can create a steeper learner curve, it results in more power and<br>
flexibility later.<br>
<br>
Suggestions on how to improve documentation so that 'quoting' is clearer<br>
are very welcome. I've added an item to the list so we don't forget.<br>
<br>
Matthew<br>
<div><div></div><div class="h5"><br>
<br>
On Mon, 2010-08-30 at 23:59 -0400, Nicolas Chapados wrote:<br>
> Dear data.table friends and maintainers,<br>
><br>
><br>
> First, thanks to the authors for this excellent package: it really<br>
> fills a void in the R world. However, I have a question: I'm looking<br>
> to have an efficient conversion of a data table object to a vector (of<br>
> the correct type) when querying a single column whose name is stored<br>
> in a variable. As per the vignette and the FAQ, I use the syntax<br>
><br>
><br>
> my.data.table[, colname, with=FALSE]<br>
><br>
><br>
> (where colname is a variable containing my desired column name) but<br>
> this returns another data table, not a vector. Morever, the eval<br>
> syntax suggested in the FAQ simply does not work:<br>
><br>
><br>
> my.data.table[, eval(colname)]<br>
><br>
><br>
> See example below. I could use as.matrix on the result, but this<br>
> carries out undesirable type conversion in the case of columns<br>
> containing dates: see below.<br>
><br>
><br>
> Here is an example to reproduce this problem:<br>
><br>
><br>
> > require(data.table)<br>
> Loading required package: data.table<br>
> > a <- data.table(x=seq(1, 2, by=0.1), y=seq(as.POSIXct("2010-01-01"),<br>
> as.POSIXct("2010-01-11"), length.out=11))<br>
> > a<br>
> x y<br>
> [1,] 1.0 2010-01-01<br>
> [2,] 1.1 2010-01-02<br>
> [3,] 1.2 2010-01-03<br>
> [4,] 1.3 2010-01-04<br>
> [5,] 1.4 2010-01-05<br>
> [6,] 1.5 2010-01-06<br>
> [7,] 1.6 2010-01-07<br>
> [8,] 1.7 2010-01-08<br>
> [9,] 1.8 2010-01-09<br>
> [10,] 1.9 2010-01-10<br>
> [11,] 2.0 2010-01-11<br>
> > colname <- "y"<br>
><br>
><br>
> ## The following returns a data table. How can I get a vector, and<br>
> still preserve type information?<br>
> > a[, colname, with=FALSE]<br>
> y<br>
> [1,] 2010-01-01<br>
> [2,] 2010-01-02<br>
> [3,] 2010-01-03<br>
> [4,] 2010-01-04<br>
> [5,] 2010-01-05<br>
> [6,] 2010-01-06<br>
> [7,] 2010-01-07<br>
> [8,] 2010-01-08<br>
> [9,] 2010-01-09<br>
> [10,] 2010-01-10<br>
> [11,] 2010-01-11<br>
><br>
><br>
> ## The eval recipe suggested in the FAQ does not work.<br>
> > a[, eval(colname)]<br>
> [1] "y"<br>
><br>
><br>
> ## as.vector does not convert away from data.table<br>
> > as.vector(a[, colname, with=FALSE])<br>
> y<br>
> [1,] 2010-01-01<br>
> [2,] 2010-01-02<br>
> [3,] 2010-01-03<br>
> [4,] 2010-01-04<br>
> [5,] 2010-01-05<br>
> [6,] 2010-01-06<br>
> [7,] 2010-01-07<br>
> [8,] 2010-01-08<br>
> [9,] 2010-01-09<br>
> [10,] 2010-01-10<br>
> [11,] 2010-01-11<br>
> > class(as.vector(a[, colname, with=FALSE]))<br>
> [1] "data.table"<br>
><br>
><br>
> ## as.matrix loses type information (NOTE: in my case it is not<br>
> acceptable to<br>
> ## convert this character vector back to a POSIXct, due to the loss of<br>
> important<br>
> ## timezone information. Furthermore, this would be very inefficient.)<br>
> > as.matrix(a[, colname, with=FALSE])<br>
> y<br>
> [1,] "2010-01-01"<br>
> [2,] "2010-01-02"<br>
> [3,] "2010-01-03"<br>
> [4,] "2010-01-04"<br>
> [5,] "2010-01-05"<br>
> [6,] "2010-01-06"<br>
> [7,] "2010-01-07"<br>
> [8,] "2010-01-08"<br>
> [9,] "2010-01-09"<br>
> [10,] "2010-01-10"<br>
> [11,] "2010-01-11"<br>
> > mode(as.matrix(a[, colname, with=FALSE]))<br>
> [1] "character"<br>
><br>
><br>
> ## Finally, one could go through a data.frame, but this is inefficient<br>
> ## and it sorts of defeats the purpose of using data.table...<br>
> > as.data.frame(a[, colname, with=FALSE])[, colname]<br>
> [1] "2010-01-01 EST" "2010-01-02 EST" "2010-01-03 EST" "2010-01-04<br>
> EST"<br>
> [5] "2010-01-05 EST" "2010-01-06 EST" "2010-01-07 EST" "2010-01-08<br>
> EST"<br>
> [9] "2010-01-09 EST" "2010-01-10 EST" "2010-01-11 EST"<br>
><br>
><br>
><br>
><br>
> So at this point, my imagination is running out and I'm turning to<br>
> this list for suggestions. This should seem to be a fairly frequent<br>
> use-case, and I'm surprised it does not appear to have previously been<br>
> addressed.<br>
><br>
><br>
> For the record, here is my sessionInfo()<br>
><br>
><br>
> > sessionInfo()<br>
> R version 2.9.2 (2009-08-24)<br>
> x86_64-pc-linux-gnu<br>
><br>
><br>
> locale:<br>
> C<br>
><br>
><br>
> attached base packages:<br>
> [1] stats graphics grDevices utils datasets methods base<br>
><br>
><br>
><br>
> other attached packages:<br>
> [1] data.table_1.4.1<br>
><br>
><br>
><br>
><br>
> Thanks in advance for any help!<br>
> + Nicolas Chapados<br>
</div></div>> _______________________________________________<br>
> datatable-help mailing list<br>
> <a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br>
<br>
<br>
</blockquote></div><br></div>