Displaying 1 result from an estimated 1 matches for "td20484941".
Did you mean:
a20484941
2008 Nov 20
2
how to replace NA with previous numbers
Hi,
I have a vector with lots of NAs. e.g.
vec = c(NA, NA, 2, NA, NA, 5, NA, 6, NA)
> vec
[1] NA NA 2 NA NA 5 NA 6 NA
I would like to replace NAs with their immediately previous non NA number.
After replacement, the above vector will become
> vec
[1] 0 0 2 2 2 5 5 6 6.
I understand how to do that with a loop but the actual vector is very long
and the loop takes too much time in R.