[R] NAs - NAs are not allowed in subscripted assignments

Gabor Csardi csardi at rmki.kfki.hu
Thu Jul 24 18:58:32 CEST 2008


On Thu, Jul 24, 2008 at 09:30:54AM -0700, Nordlund, Dan (DSHS/RDA) wrote:
[...]
> > > a <- c(rep(seq(1,4),4),NA,NA)
> > > b <- c(rep(seq(1,2),7),NA,NA,1,2)
> > 
> > Andreas,
> > 
> > what is wrong with 
> > 
> > a[ (a < 2 | a > 3) & b==1 ] <- NA
> > 
> > ? Isn't this what you want?
> > 
[...]
> 
> As I mentioned in my response to this thread, there are some things I don't quite understand with logical indexing.  Using the above example,
> 
> > a[ (a < 2 | a > 3) & b==1 ]
> 
> returns
> 
> [1]  1  1  1  1 NA NA
> 
> Where do the NA values come from?

This is not really about logical indexing, just operations on numeric 
and logical vectors, and how they handle NA values. Just keep in mind 
that NA means that we don't know the actual value. All operations 
were desinged (I believe) with this in mind.
Here is some help:

> NA == 1
[1] NA
> class(NA == 1)
[1] "logical"

This is NA, obviously, as _we don't know_ whether NA is equal to 1 or not.

> TRUE & NA
[1] NA
> FALSE | NA
[1] NA

The same applies here, for the result we would need to know whether 
NA is TRUE or FALSE. However, we have

> FALSE & NA
[1] FALSE
> TRUE | NA
[1] TRUE

In these cases the result can be calculated without knowing what 
actually NA is. 

Logical indexing is simple, for every TRUE value in the logical vector
we choose the corresponding element from the indexed vector. If we 
index with NA, then the chosen element is NA as well.

> (1:5)[ c(T,T,T,T,T) ]
[1] 1 2 3 4 5
> (1:5)[ c(T,T,T,F,T) ]
[1] 1 2 3 5
> (1:5)[ c(T,T,T,F,NA) ]
[1]  1  2  3 NA
> (1:5)[ c(NA,T,T,F,NA) ]
[1] NA  2  3 NA

Does this help? Best,
Gabor

> Dan
> 
> Daniel J. Nordlund
> Washington State Department of Social and Health Services
> Planning, Performance, and Accountability
> Research and Data Analysis Division
> Olympia, WA  98504-5204
>  
>  
> 
> ______________________________________________
> 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.

-- 
Csardi Gabor <csardi at rmki.kfki.hu>    UNIL DGM



More information about the R-help mailing list