# Data imputation question

I have a variable with some missing values

a <- rnorm(100);
a[sample(1:100,10)] <- NA;
a;


How can I fill missing values with previous non missing value?

for example if I have sequence:

a<- (3, 2, 1, 6, 3, NA, 23, 23, NA);

first NA should be replaced by first previous non NA number 3, second NA should be replaced with 23 etc.

Thanks

• Your question lacks a host of critical details. The situation you just programmed gives no context as to what you are trying to accomplish, nor to what "previous non missing value" refers to. Is your data a time series? Jul 8 '11 at 18:10
• -1 This is too vague to be treated seriously.
– user88
Jul 8 '11 at 18:15
• point taken. Will refine question with an example. Jul 8 '11 at 21:06
• @ user333 As far as I can see, your "imputation approach" lacks a sound statistical base. To get a better understanding how imputation works, you might want to check out the following (non-technical) literature. Jul 9 '11 at 9:57
• So it is a pure programming question? Than, maybe it is a better idea to ask this on SO? Here you will be rather criticized about your method...
– user88
Jul 9 '11 at 10:36

set.seed(5)
a <- rnorm(20);
a[sample(1:20,4)] <- NA;


a is:

 [1] -0.84085548  1.38435934 -1.25549186  0.07014277          NA -0.60290798
[7] -0.47216639 -0.63537131 -0.28577363          NA  1.22763034 -0.80177945
[13] -1.08039260 -0.15753436          NA -0.13898614          NA -2.18396676
[19]  0.24081726 -0.25935541


To set each NA to the previous value:

NAs <- which(is.na(a))
a[NAs] <- a[NAs-1]


giving

 [1] -0.84085548  1.38435934 -1.25549186  0.07014277  0.07014277 -0.60290798
[7] -0.47216639 -0.63537131 -0.28577363 -0.28577363  1.22763034 -0.80177945
[13] -1.08039260 -0.15753436 -0.15753436 -0.13898614 -0.13898614 -2.18396676
[19]  0.24081726 -0.25935541


Note that this fails if first value is missing

I would outright remove any features that have far too many missing values to impute and use KNN to impute missing values for the remaining ones.

• You could also use a bagged tree model for imputation, but that requires more processing power.
– Zach
Jul 9 '11 at 17:16

A combination of "is.na" and "lag" should do what you want, but, as previous commentators have pointed out, this may not be the best method of imputation.