[datatable-help] Column-wise value replacement

Melanie Bacou mel at mbacou.com
Tue Apr 17 13:02:46 CEST 2012


Dear all,

I have a large data.table and I am simply trying to replace all zeros 
with NAs in a subset of columns. So I tried first:

> dt[, lapply(.SD, function(x) x := ifelse(x==0, NA, x)), .SDcols=3:30]
Error in lapply(.SD, function(x) `:=`(x, ifelse(x == 0, NA, x))) :
   object '.SD' not found

Clearly that's not the right approach...

Then I tried:

> for (i in names(dt)[3:30]) {
   eval(parse(text=paste("dt[`", i, "`==0, `", i, "` := NA]", sep="")))
}
That worked but is rather ugly.

would you recommend any better way to avoid the eval(parse()) to 
perform such simple tasks?

Thanks in advance, --Mel.




More information about the datatable-help mailing list