I am trying to replace all the zeros with the previous number from a list.
The list is something like this:
x <- c(3,0,0,0,0,1,0,0,0,0,0,2,0,0,0,0,3,0,0,0,1,0,2,0)
I tried already the function
x <- c(3,0,0,0,0,1,0,0,0,0,0,2,0,0,0,0,3,0,0,0,1,0,2,0)
replace (x, x==0, first(x))
[1] 3 3 3 3 3 1 3 3 3 3 3 2 3 3 3 3 3 3 3 3 1 3 2 3
But it changes the first value of the list =3 to all the zeros and the 2's and 1's are neglected.
Also
replace (x, x==0, x)
[1] 3 3 0 0 0 1 0 1 0 0 0 2 0 0 2 0 3 0 0 0 1 3 2 0