Carson Farmer
2008-Jul-02 21:17 UTC
[R] set values in data.frame to NA conditional on another data.frame
Hello List, Is there a faster way to set values in one data.frame equal to NA conditional on the corresponding value in another data.frame? Currently I am using: > b[is.na(a)] <- NA where 'a' and 'b' are data.frames of equal size/dimensions, and 'a' contains NAs but 'b' does not. This is extremely slow as is, as my data.frames are about 6000 columns by 6000 rows in size. Are there any tricks to speed things up here? Thank you in advance, Carson
Petr PIKAL
2008-Jul-03 07:01 UTC
[R] Odp: set values in data.frame to NA conditional on another data.frame
Hi is your data all numeric or all character? If yes you can transform it to matrix and then such setting shall be quite quick. vec<-rnorm(6000*6000) mat<-vec dim(mat)<-c(6000,6000) mat2<-mat mat2[sample(1:6000, 500), sample(1:6000, 500)]<-NA sum(is.na(mat2)) [1] 250000> 500*500[1] 250000 system.time(mat[is.na(mat2)]<-NA) user system elapsed 0.61 0.11 0.72> > sum(is.na(mat))[1] 250000 Regards Petr r-help-bounces at r-project.org napsal dne 02.07.2008 23:17:44:> Hello List, > > Is there a faster way to set values in one data.frame equal to NA > conditional on the corresponding value in another data.frame? Currently > I am using: > > b[is.na(a)] <- NA > where 'a' and 'b' are data.frames of equal size/dimensions, and 'a' > contains NAs but 'b' does not. This is extremely slow as is, as my > data.frames are about 6000 columns by 6000 rows in size. > Are there any tricks to speed things up here? > > Thank you in advance, > > Carson > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.