I am trying to replace missing values (NA
) in a vector. NA
between two equal number is replaced by that number. NA
between two different values, should stay NA
. For example, given vector "a", I want it to be "b".
a = c(1, NA, NA, NA, 1, NA, NA, NA, 2, NA, NA, 2, 3, NA, NA, 3)
b = c(1, 1, 1, 1, 1, NA, NA, NA, 2, 2, 2, 2, 3, 3, 3, 3)
As you can see, the second run of NA
, between the values 1
and 2
, is not replaced.
Is there a way to vectorize the calculation?
NA
at the beginning or end of the vector, do they stayNA
? – Awl