evaluate NA to FALSE instead of NA?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

evaluate NA to FALSE instead of NA?

Rainer Krug-3
Hi

I want to evaluate NA and NaN to FALSE (for indexing) so I would like to
have the result as indicated here:

,----
| > p <- c(1:10/100, NA, NaN)
| > p
|  [1] 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10   NA  NaN
| > p[p<=0.05]
| [1] 0.01 0.02 0.03 0.04 0.05   NA   NA
| > p[sapply(p<=0.05, isTRUE)]
| [1] 0.01 0.02 0.03 0.04 0.05  <<<=== I want this
`----

Is there a way that I can do this more easily then in my example above?
It works, but it strikes me that there is not a better way of doing
this - am I missing a command or option?

Thanks,

Rainer

--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[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.

attachment0 (504 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: evaluate NA to FALSE instead of NA?

Pascal Oettli-2
Hi Rainer,

As "complete.cases()" does?

p <- c(1:10/100, NA, NaN)
complete.cases(p)
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE

Regards,
Pascal


On Tue, Oct 14, 2014 at 6:51 PM, Rainer M Krug <[hidden email]> wrote:

> Hi
>
> I want to evaluate NA and NaN to FALSE (for indexing) so I would like to
> have the result as indicated here:
>
> ,----
> | > p <- c(1:10/100, NA, NaN)
> | > p
> |  [1] 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10   NA  NaN
> | > p[p<=0.05]
> | [1] 0.01 0.02 0.03 0.04 0.05   NA   NA
> | > p[sapply(p<=0.05, isTRUE)]
> | [1] 0.01 0.02 0.03 0.04 0.05  <<<=== I want this
> `----
>
> Is there a way that I can do this more easily then in my example above?
> It works, but it strikes me that there is not a better way of doing
> this - am I missing a command or option?
>
> Thanks,
>
> Rainer
>
> --
> Rainer M. Krug
> email: Rainer<at>krugs<dot>de
> PGP: 0x0F52F982
>
> ______________________________________________
> [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.
>



--
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan

______________________________________________
[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: evaluate NA to FALSE instead of NA?

Sven E. Templer
In reply to this post by Rainer Krug-3
use:

which(p<=.05)

this will not yield logical, but integer indices without NA

On 14 October 2014 11:51, Rainer M Krug <[hidden email]> wrote:

> Hi
>
> I want to evaluate NA and NaN to FALSE (for indexing) so I would like to
> have the result as indicated here:
>
> ,----
> | > p <- c(1:10/100, NA, NaN)
> | > p
> |  [1] 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10   NA  NaN
> | > p[p<=0.05]
> | [1] 0.01 0.02 0.03 0.04 0.05   NA   NA
> | > p[sapply(p<=0.05, isTRUE)]
> | [1] 0.01 0.02 0.03 0.04 0.05  <<<=== I want this
> `----
>
> Is there a way that I can do this more easily then in my example above?
> It works, but it strikes me that there is not a better way of doing
> this - am I missing a command or option?
>
> Thanks,
>
> Rainer
>
> --
> Rainer M. Krug
> email: Rainer<at>krugs<dot>de
> PGP: 0x0F52F982
>
> ______________________________________________
> [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: evaluate NA to FALSE instead of NA?

Joshua Wiley-2
In reply to this post by Rainer Krug-3
Hi,

Perhaps still not as short as you want, but I normally use which():

p <- c(1:10/100, NA, NaN)
p[which(p <= .05)]
[1] 0.01 0.02 0.03 0.04 0.05

Cheers,

Josh



On Tue, Oct 14, 2014 at 8:51 PM, Rainer M Krug <[hidden email]> wrote:

> Hi
>
> I want to evaluate NA and NaN to FALSE (for indexing) so I would like to
> have the result as indicated here:
>
> ,----
> | > p <- c(1:10/100, NA, NaN)
> | > p
> |  [1] 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10   NA  NaN
> | > p[p<=0.05]
> | [1] 0.01 0.02 0.03 0.04 0.05   NA   NA
> | > p[sapply(p<=0.05, isTRUE)]
> | [1] 0.01 0.02 0.03 0.04 0.05  <<<=== I want this
> `----
>
> Is there a way that I can do this more easily then in my example above?
> It works, but it strikes me that there is not a better way of doing
> this - am I missing a command or option?
>
> Thanks,
>
> Rainer
>
> --
> Rainer M. Krug
> email: Rainer<at>krugs<dot>de
> PGP: 0x0F52F982
>
> ______________________________________________
> [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.
>
>


--
Joshua F. Wiley
Ph.D. Student, UCLA Department of Psychology
http://joshuawiley.com/
Senior Analyst, Elkhart Group Ltd.
http://elkhartgroup.com
Office: 260.673.5518

        [[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
|

SOLVED: evaluate NA to FALSE instead of NA?

Rainer Krug-3
In reply to this post by Rainer Krug-3

Thanks Joshua and Sven - I completely forgot about which() .

Pascal - I never new about complete.cases - interesting function.

Thanks,

Rainer
 

Rainer M Krug <[hidden email]> writes:

> Hi
>
> I want to evaluate NA and NaN to FALSE (for indexing) so I would like to
> have the result as indicated here:
>
> ,----
> | > p <- c(1:10/100, NA, NaN)
> | > p
> |  [1] 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10   NA  NaN
> | > p[p<=0.05]
> | [1] 0.01 0.02 0.03 0.04 0.05   NA   NA
> | > p[sapply(p<=0.05, isTRUE)]
> | [1] 0.01 0.02 0.03 0.04 0.05  <<<=== I want this
> `----
>
> Is there a way that I can do this more easily then in my example above?
> It works, but it strikes me that there is not a better way of doing
> this - am I missing a command or option?
>
> Thanks,
>
> Rainer
--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[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.

attachment0 (504 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: SOLVED: evaluate NA to FALSE instead of NA?

S Ellison-2
> Thanks Joshua and Sven - I completely forgot about which() .
Also
na.omit(p[p<=0.05])

#and
p[p<=0.05 & !is.na(p)]

S.



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[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.