I have a string of numbers:
n1 = c(1, 1, 0, 6, 0, 0, 10, 10, 11, 12, 0, 0, 19, 23, 0, 0)
I need to replace 0 with the corresponding number right "behind" it to get, while leaving the 0s in the tail alone (cause there is nothing right behind them):
n2 = c(1, 1, 6, 6, 10, 10, 10, 10, 11, 12, 19, 19, 19, 23, 0, 0)
How can I get from n1 to n2?
This seems to be a much harder question than the one I've asked earlier:
How to fill in the preceding numbers whenever there is a 0 in R?
where flodel has come up with an elegant solution:
n2 <- n1[cummax(seq_along(n1) * (n1 != 0))]
However, this solution does not work here; I've tried but failed to adapt the code.
Can someone else figure out an elegant solution?
Thanks in advance!