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

From: Kunzler, Andreas <a.kunzler_at_bzaek.de>
Date: Thu, 24 Jul 2008 14:09:06 +0200


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



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 Thu 24 Jul 2008 - 12:27:00 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 Thu 24 Jul 2008 - 14:32:13 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