[R] Replace NA values with previous valid value in array

arun smartpink111 at yahoo.com
Tue Oct 22 20:58:58 CEST 2013


Hi,

c1 = (1,2,3,4,5,6,NA,7,8,NA,9,10,NA)
library(zoo)

na.locf(c1)
# [1]  1  2  3  4  5  6  6  7  8  8  9 10 10

A.K.

Hi, I want to "fix" an array that contains several NA elements. And I 
would like to replace them with the previous valid element. 

So my array c = (1,2,3,4,5,6,NA,7,8,NA,9,10,NA) become cfixed= (1,2,3,4,5,6,6,7,8,8,9,10,10) 

(that's an example the actual array has thousands of elements) 

Thanks!



More information about the R-help mailing list