Assuming we have a vector of values ββwith missing values, such as:
test <- c(3,6,NA,7,8,NA,NA,5,8,6,NA,4,3,NA,NA,NA)
The goal is to identify a series of NAs that are 2 or less in length in order to apply linear interpolation to a series that have non-NA values ββat their ends. I was able to detect the end index of such series using this code:
which.na <- which(is.na(test))
diff.which.na <- diff(which.na)
which.diff.which.na <- which(diff.which.na>1)
end.index <- which.na[which.diff.which.na]
result:
> end.index
[1] 3 7 11
the latest NA series can be processed with a conditional statement. However, I cannot find the start index of the NA series, because I cannot do the following:
diff.which.na <- diff(which.na,lag=-1)
Thus, the expected result:
beg.index= c(3,6,11)
and
end.index=c(3,7,11)
Any ideas?
thanks
source
share