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]]
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 addressing: 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 data.tables. - Tom Tom Short