Replace NaN with zero

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace NaN with zero

Spilak,Jacqueline [Edm]
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Replace NaN with zero

Uwe Ligges-3


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

Since 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Replace NaN with zero

Alexandre Swarowsky
In reply to this post by Spilak,Jacqueline [Edm]
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]]
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Replace NaN with zero

Mike Prager
In reply to this post by Spilak,Jacqueline [Edm]
"Spilak,Jacqueline [Edm]" <[hidden email]> 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]) <- TRUE

HTH


--
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Replace NaN with zero

Mike Prager
Mike Prager <[hidden email]> 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.

______________________________________________
[hidden email] 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.