time series - R replacing missing values with the mean of surroundings values -


my dataset looks following (let's call "a"):

date value 2013-01-01 12.2 2013-01-02 na 2013-01-03 na 2013-01-04 16.8 2013-01-05 10.1 2013-01-06 na 2013-01-07 12.0 

i replace na mean of closest surroundings values (the previous , next values in series).

i tried following not convinced output...

miss.val=which(is.na(a$value)) library(zoo) z=zoo(a$value,a$date) z.corr=na.approx(z) z.corr[(miss.val-1):(miss.val+1),] 

using na.locf (last observation carried forward) package zoo:

r> library("zoo") r> x <- c(12.2, na, na, 16.8, 10.1, na, 12.0) r> (na.locf(x) + rev(na.locf(rev(x))))/2 [1] 12.20 14.50 14.50 16.80 10.10 11.05 12.00 

(does not work if first or last element of x na)


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -