[R] Dealing with NaN's in data frames

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Sat Aug 16 09:50:55 CEST 2008


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



More information about the R-help mailing list