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

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Thu Jul 24 15:23:32 CEST 2008


> 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

You were nearly right - you just had an extra index that you didn't need. 
Try:
a[a[b==1]<2 | a[b==1]>3] <- NA

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list