Missing value when calculating current medians?

I would like to smooth out the time series to avoid false jitter / error. In other words, I want to make very local reliable anti-aliasing.

I met roulette and roulette in a zoo pack, but ran into a problem because my vector had NA. I then read somewhere that these functions of the zoo are used during work, and this is the problem.

== Examples ==

median(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10),na.rm = TRUE) runmed(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10),k=3) 

The first line returns 2, but will return NA if na.rm = TRUE not included. The second line returns Error in runmed(c(1, 1, 1, 2, 2, 2, 7, NA, 1, 2, 3, 10, 10, 10), k = 3) : NA/NaN/Inf in foreign function call (arg 1) . Cannot add na.rm argument to string.

How can I get runed to handle NA? . By the way, rollmean returns a vector that is valid before NA, and then returns NA for each value after that.

+4
source share
2 answers

Use na.omit

 runmed(na.omit(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10)),k=3) # [1] 1 1 1 2 2 2 2 2 2 3 10 10 10 #attr(,"k") #[1] 3 

Or use one of the na.* Functions from zoo ( na.locf , na.approx , na.spline , na.aggregate , etc.) for example.

 runmed(na.locf(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10)),k=3) #[1] 1 1 1 2 2 2 7 7 2 2 3 10 10 10 #attr(,"k") #[1] 3 
+5
source

See runquantile in the caTools package.

+1
source

All Articles