[R] data.table evaluating columns

Rob Forler
Wed, 03 Mar 2010 00:09:32 -0000

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), data2c(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), data2c(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

}

However, I have to convert to a matrix on the right side and the left side
doesn't work because you can't assign. I've also tried:

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), data2c(3,3,2,3,5,2)))

for(i in 1:length(names)){
frame[, list(as.matrix(frame[,names[i], with=F] )< 3) ]
}

but then I lose the other columns and I don't have the correct name for the
new column.

Anyone have any suggestions on the best approach to doing this?

Thanks,
Rob

	[[alternative HTML version deleted]]