I am looking for the most efficient way to replace all occurrences of NaN in a data frame with NA. I can do this with a double loop, but it seems that there should be a higher level and more efficient way. With is.na, I could use ifelse, but if.nan seems not to have similar capabilities. TIA, Jon Peck Jon K. Peck jkpeck@aya.yale.edu [[alternative HTML version deleted]]
try this: dat <- data.frame(x = rnorm(10), y = rnorm(10), z = rnorm(10), g = gl(5,2)) dat$x[sample(10, 3)] <- NaN dat$y[sample(10, 3)] <- NaN dat$z[sample(10, 3)] <- NaN dat[] <- lapply(dat, function(x){ x[is.nan(x)] <- NA x }) I hope it helps. Best, Dimitris Peck, Jon wrote:> I am looking for the most efficient way to replace all occurrences of NaN in a data frame with NA. I can do this with a double loop, but it seems that there should be a higher level and more efficient way. With is.na, I could use ifelse, but if.nan seems not to have similar capabilities. > > > > TIA, > > Jon Peck > > > > Jon K. Peck > > jkpeck at aya.yale.edu > > > > > [[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. > >-- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://perswww.kuleuven.be/dimitris_rizopoulos/ Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Peck, Jon <peck <at> spss.com> writes:> > I am looking for the most efficient way to replace all occurrences of NaN in adata frame with NA. I can do this> with a double loop, but it seems that there should be a higher level and moreefficient way. With is.na, I> could use ifelse, but if.nan seems not to have similar capabilities.dt[sample(12,3)] = NaN df = data.frame(matrix(dt,nrow=3)) df[sapply(df,is.na)] = NA Dieter
Hi Jon, Here's one way.> x <- c(1,2,3,4,NaN) > y <- c(1,2,NaN,4,5) > > myDF <- data.frame(x,y) > myDFx y 1 1 1 2 2 2 3 3 NaN 4 4 4 5 NaN 5> > myDF[ is.na(myDF) ] <- NA > myDFx y 1 1 1 2 2 2 3 3 NA 4 4 4 5 NA 5 Cheers, Bob ========================================================Bob Muenchen (pronounced Min'-chen), Manager Statistical Consulting Center U of TN Office of Information Technology 200 Stokely Management Center, Knoxville, TN 37996-0520 Voice: (865) 974-5230 FAX: (865) 974-4810 Email: muenchen at utk.edu Web: http://oit.utk.edu/scc, News: http://listserv.utk.edu/archives/statnews.html ========================================================> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Peck, Jon > Sent: Friday, August 15, 2008 10:28 PM > To: r-help at r-project.org > Subject: [R] Dealing with NaN's in data frames > > I am looking for the most efficient way to replace all occurrences of > NaN in a data frame with NA. I can do this with a double loop, but it > seems that there should be a higher level and more efficient way.With> is.na, I could use ifelse, but if.nan seems not to have similar > capabilities. > > > > TIA, > > Jon Peck > > > > Jon K. Peck > > jkpeck at aya.yale.edu > > > > > [[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.
Try this: is.na(DF) <- is.na(DF) or this which returns a transformed data frame without overwriting DF: replace(DF, is.na(DF), NA) On Fri, Aug 15, 2008 at 10:27 PM, Peck, Jon <peck at spss.com> wrote:> I am looking for the most efficient way to replace all occurrences of NaN in a data frame with NA. I can do this with a double loop, but it seems that there should be a higher level and more efficient way. With is.na, I could use ifelse, but if.nan seems not to have similar capabilities. > > > > TIA, > > Jon Peck > > > > Jon K. Peck > > jkpeck at aya.yale.edu > > > > > [[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. >