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

Kunzler, Andreas a.kunzler at bzaek.de
Thu Jul 24 14:09:06 CEST 2008


Dear List,

I ran into some trouble handling missing values.

Assume 2 vectors (numeric) including NAs

a <- c(rep(seq(1,4),4),NA,NA)
b <- c(sample(1:2,14,replace=T),NA,NA,1,2)

I want to replace the values of vector a that are smaller than 2 and
larger than 3 into NAs only in case vector b equals 1

a[b==1][a[b==1]<2 | a[b==1]>3] <- NA
The following error accurse:
NAs are not allowed in subscripted assignments

So I tried to exclude NAs

pat<-c((a[b==1]<2 & is.na(a[b==1])==F)| (a[b==1]>3 & is.na(a[b==1])==F))
this is a logical vector

a[b==1][pat==T] <-NA

But it did not help

Even if I have a look at
a[b==1][pat==T]
[1] 1 4 4 1

All I want to do is change those values to NA.
Is it possible that my mistake is caused by not understanding

vector[logic operation a][logic operation b] 

I thought it is pretty much the same as
x <- vector[logic operation a]
x[logic operation b]

I hope someone can help me
Thanks



More information about the R-help mailing list