I need help with replacing NaN with zero (the value '0') in my dataset. The reason is that I can't get it to graph because of the NaN in the dataset. I have tried: data[is.nan(data)] <- 0 that others have suggested in the help archives but this does nothing so I am not sure what I am doing wrong. Thanks in advance for your help. [[alternative HTML version deleted]]
Spilak,Jacqueline [Edm] wrote:> I need help with replacing NaN with zero (the value '0') in my dataset. > The reason is that I can't get it to graph because of the NaN in the > dataset. I have tried: > data[is.nan(data)] <- 0Since data is a data.frame and not a matrix, you might want to loop over its columns and apply your replacement for each column separately. Uwe Ligges> that others have suggested in the help archives but this does nothing so > I am not sure what I am doing wrong. > Thanks in advance for your help. > > [[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.
what about ?sub and ?ifelse Spilak,Jacqueline [Edm] wrote:> I need help with replacing NaN with zero (the value '0') in my dataset. > The reason is that I can't get it to graph because of the NaN in the > dataset. I have tried: > data[is.nan(data)] <- 0 > that others have suggested in the help archives but this does nothing so > I am not sure what I am doing wrong. > Thanks in advance for your help. > [[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.Veja quais s?o os assuntos do momento no Yahoo! +Buscados http://br.maisbuscados.yahoo.com
"Spilak,Jacqueline [Edm]" <Jacqueline.Spilak at ec.gc.ca> wrote:> I need help with replacing NaN with zero (the value '0') in my dataset. > The reason is that I can't get it to graph because of the NaN in the > dataset. I have tried: > data[is.nan(data)] <- 0 > that others have suggested in the help archives but this does nothing so > I am not sure what I am doing wrong.The function is.nan() does not operate like is.na(). One could consider that a design deficiency in R. You can overcome it with apply(), is in this short script:> tmp = data.frame(a = 1:3, b = 4:6) > tmp$a[1] = 0/0 > print(tmp)a b 1 NaN 4 2 2 5 3 3 6> mask <- apply(tmp, 2, is.nan) > tmp2 <- tmp > tmp2[mask] <-1000 > print(tmp2)a b 1 1000 4 2 2 5 3 3 6 Here I used 1000 instead of 0 to make it more easily seen. For plotting, I would not use 0 as you propose, as that will plot a point at zero. It is usually better to use NA, as in> is.na(tmp2[mask]) <- TRUEHTH -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement.
Mike Prager <mike.prager at noaa.gov> wrote:> The function is.nan() does not operate like is.na(). One could > consider that a design deficiency in R.I meant to write, "design inconsistency". -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement.