I need to delete leading and trailing missing values (NA
) from a vector. NA
s between numbers should be kept. Note that my next step is index another vector without NA
and keep only values with the same position as in first vector. Here is the example:
a <- c(NA, NA, NA, NA, NA, 5,2,3,1,2,1,5,2, NA, NA, 2,3,2,NA,NA,NA)
b <- sample(1:21)
My desired output is:
a1 <- c(5,2,3,1,2,1,5,2,NA,NA,2,3,2)
# leading and trailing NAs removed
b1 <- b[6:18]
# subset with the indices kept in "a" above.
I thing I can do this with come conditional loop but I want to vectorize it. Thx for help!