0
$\begingroup$

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

$\endgroup$
7
  • 3
    $\begingroup$ 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? $\endgroup$
    – Andy W
    Jul 8 '11 at 18:10
  • $\begingroup$ -1 This is too vague to be treated seriously. $\endgroup$
    – user88
    Jul 8 '11 at 18:15
  • $\begingroup$ point taken. Will refine question with an example. $\endgroup$
    – user333
    Jul 8 '11 at 21:06
  • $\begingroup$ @ 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. $\endgroup$ Jul 9 '11 at 9:57
  • 2
    $\begingroup$ 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... $\endgroup$
    – user88
    Jul 9 '11 at 10:36
2
$\begingroup$

To just technically answer your question

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

$\endgroup$
0
$\begingroup$

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.

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

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.