Re: [R] NA and logical indexes

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Sat, 29 Nov 2008 13:37:37 +0800

G'day Sebastian,

On Fri, 28 Nov 2008 16:35:13 -0600
"Sebastian P. Luque" <spluque_at_gmail.com> wrote:

> On Fri, 28 Nov 2008 22:01:15 -0000 (GMT),
> (Ted Harding) <Ted.Harding_at_manchester.ac.uk> wrote:
>
> > NA can seem to have a bewildering logic, but it all becomes clear if
> > you interpret NA as "value unkown".
> > [...]
> > Clear? ( :)) Hoping this helps, Ted.

>
> Crystal! It's something that one needs to be extra careful with when
> playing with logicals.

Or, rather, if you expect that your code one day has to handle data in which values from which you construct logicals might be missing (NA), then it might be worthwhile to think carefully about this case and how you want to handle missing values. In your case, do you want to return the rows where foo$A is missing or not? Both is possible:

R> foo <- data.frame(A=gl(2, 5, labels=letters[1:2]), X=runif(10))
R> foo$A[1] <- NA
R> foo[!is.na(foo$A) & foo$A == "b", ]  
   A         X
6  b 0.8418619
7  b 0.6833322
8  b 0.1582648

9 b 0.4431542
10 b 0.7603365
R> foo[is.na(foo$A) | foo$A == "b", ]

      A X

1  <NA> 0.8922147
6     b 0.8418619
7     b 0.6833322
8     b 0.1582648
9     b 0.4431542
10    b 0.7603365

Cheers,

        Berwin


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. Received on Sat 29 Nov 2008 - 05:44:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 29 Nov 2008 - 10:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive