[R] data.table evaluating columns

Matthew Dowle mdowle at mdowle.plus.com
Wed Mar 3 13:05:54 CET 2010

I'd go a bit further and remind that the r-help posting guide is clear :

  " For questions about functions in standard packages distributed with R 
(see the FAQ Add-on packages in R), ask questions on R-help.
If the question relates to a contributed package , e.g., one downloaded from 
CRAN, try contacting the package maintainer first. You can also use 
find("functionname") and packageDescription("packagename") to find this 
information. ONLY send such questions to R-help or R-devel if you get no 
reply or need further assistance. This applies to both requests for help and 
to bug reports. "

The "ONLY" is in bold in the posting guide. I changed the bold to capitals 
above for people reading this in text only.

Since Tom and I are friendly and responsive, users of data.table don't 
usually make it to r-help. We'll follow up this one off-list.  Please note 
that Rob's question is very good by the rest of the posting guide, so no 
complaints there, only that it was sent to the wrong place.  Please keep the 
questions coming, but send them to us, not r-help.

You do sometimes see messages to r-help starting something like "I have 
contacted the authors/maintainers but didn't hear back,  does anyone know 
...".   To not state that they had would be an implicit request for further 
work by the community (for free) to ask if they had. So its not enough to 
contact the maintainer first, but you also have to say that you have as 
well, and perhaps how long ago too would be helpful.  For r-forge projects I 
usually send any question to everyone on the project (easy to find) or if 
they have a list then to that.


"Tom Short" <tshort.rlists at gmail.com> wrote in message 
news:fd27013a1003021718w409acb32r1281dfeca559314a at mail.gmail.com...
On Tue, Mar 2, 2010 at 7:09 PM, Rob Forler <rforler at uchicago.edu> wrote:
> Hi everyone,
> I have the following code that works in data frames taht I would like tow
> ork in data.tables . However, I'm not really sure how to go about it.
> I basically have the following
> names = c("data1", "data2")
> frame = data.frame(list(key1=as.integer(c(1,2,3,4,5,6)),
> key2=as.integer(c(1,2,3,2,5,6)),data1 = c(3,3,2,3,5,2), data2=
> c(3,3,2,3,5,2)))
> for(i in 1:length(names)){
> frame[, paste(names[i], "flag")] = frame[,names[i]] < 3
> }
> Now I try with data.table code:
> names = c("data1", "data2")
> frame = data.table(list(key1=as.integer(c(1,2,3,4,5,6)),
> key2=as.integer(c(1,2,3,2,5,6)),data1 = c(3,3,2,3,5,2), data2=
> c(3,3,2,3,5,2)))
> for(i in 1:length(names)){
> frame[, paste(names[i], "flag"), with=F] = as.matrix(frame[,names[i],
> with=F] )< 3
> }

Rob, this type of question is better for the package maintainer(s)
directly rather than R-help. That said, one answer is to use list

for(i in 1:length(names)){
    frame[[paste(names[i], "flag")]] = frame[[names[i]]] < 3

Another option is to manipulate frame as a data frame and convert to
data.table when you need that functionality (conversion is quick).

In the data table version, frame[,names[i], with=F] is the same as
frame[,names[i], drop=FALSE] (the answer is a list, not a vector).
Normally, it's easier to use [[]] or $ indexing to get this. Also,
fname[i,j] <- something assignment is still a bit buggy for

- Tom

Tom Short

More information about the R-help mailing list