What is a smart (i.e. not a cycle) way to get the length of each spell of missing values in a vector? My ideal output is a vector that has the same length in which each missing value is replaced by the length of the missing value spell of which it is a part, and all other values are 0.
So for type input:
x <- c(2,6,1,2,NA,NA,NA,3,4,NA,NA)
I would like, for example, the conclusion:
y <- c(0,0,0,0,3,3,3,0,0,2,2)
source
share