I have a 50*50 matrix,some entry are NAs,I want to replace these NA by 0,so can I use some syntax to do so other than using ifelse? I tried to use replace(a,NA,0),it didnt work~~(a is matrix name) Thanks~ [[alternative HTML version deleted]]
Hi: what you want to do is not recommended if you're doing a statistical computation ( many threads on this. search the archives if you want more details ) but if you really want to do that below should work: mat[is.na(mat)]<-0 On Thu, Mar 5, 2009 at 11:22 PM, Manli Yan wrote:> I have a 50*50 matrix,some entry are NAs,I want to replace these NA by > 0,so can I use some syntax to do so other than using ifelse? > I tried to use replace(a,NA,0),it didnt work~~(a is matrix name) > > Thanks~ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
?is.na Manli Yan wrote:> > I have a 50*50 matrix,some entry are NAs,I want to replace these NA by > 0,so can I use some syntax to do so other than using ifelse? > I tried to use replace(a,NA,0),it didnt work~~(a is matrix name) > > Thanks~ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/how-to-omit-NA-without-using-ifelse-tp22365996p22371063.html Sent from the R help mailing list archive at Nabble.com.
Hi Manli. Try the replace() function as below: replace(a,is.na(a),0) #where a is the name of your 50 x 50 matrix Below is an example: a<-matrix(c(sqrt(-2:3)), nrow=2) # produces a 2 x 3 matrix some of whose elements are NaN (or NA) # due to square root operator on negative integers replace(a, is.na(a), 0) [,1] [,2] [,3] [1,] 0 0 1.414214 [2,] 0 1 1.732051 ############################ bartjoosen wrote:> > ?is.na > > > > Manli Yan wrote: >> >> I have a 50*50 matrix,some entry are NAs,I want to replace these NA by >> 0,so can I use some syntax to do so other than using ifelse? >> I tried to use replace(a,NA,0),it didnt work~~(a is matrix name) >> >> Thanks~ >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >> > >-- View this message in context: http://www.nabble.com/how-to-omit-NA-without-using-ifelse-tp22365996p22387672.html Sent from the R help mailing list archive at Nabble.com.