R xts: Convert all 0 values ββin a time series with a last found value other than 0
I am working on an xts object that contains some 0's in one of the columns and I need to replace them with the last non-0 value found in the sequence.
For example, I'm simplifying here using a regular R vector:
v = c(1, 2, 3, 0, 0, 0)
I need to change all 0s with the last value! = 0 in the sequence, in this case 3. The resulting vector should be:
> v
[1] 1 2 3 3 3 3
How can I achieve this, perhaps using transform () or some other vector operation?
+3
source to share