NAs are not allowed in subscripted assignments

Your logic will need to also exclude NAs in the subset. See the following example. Note the subsets vectors are stored away before x is modified.

x <- c(1,3,5,7,NA,2,4,6)
subset1 <- x>=5 & !is.na(x)
subset2 <-  x<5 & !is.na(x)

x[subset1] <- which(subset1)
x[subset2] <- 10*which(subset2)

Leave a Comment