1

Quick question: Would anyone help explain why I got all NA's for the rolling median output. I set na.rm to TRUE but that doesn't seem to help. Thanks!

library(RcppRoll)
input <- c(1:10, rep(NA, 190))
output <- roll_median(input, n = 120, fill = NA, align = 'right', na.rm = TRUE)

For reference, I expect the right output to be:

my.expectation <- rep(NA, 119)
for(i in 1:81){
  my.expectation <- c(my.expectation, median(input[i:(i + 119)], na.rm = TRUE))
}

I know how to do this with rollapply:

library(zoo)
median.n <- function(x){median(x, na.rm = TRUE)}
rollapplyr(data = input, width = 120, FUN = median.n, fill = NA)

But for speed reason (I am manipulating a data frame with tens of millions of rows), I wish to do it with roll_median. Does anyone know how to accomplish this? Thanks!

Yuanchu Dang
  • 307
  • 4
  • 14
  • Sorry this is not a rollapply question technically but I'm having trouble finding better tags... – Yuanchu Dang Jan 29 '16 at 14:18
  • I think it is because `n = 120` but your input has at most 10 consecutive numbers. For example, `n = 4` gives you some non-NA. – Kota Mori Jan 29 '16 at 14:19
  • @KotaMori Thanks! I think you are right.. The effect I want can be done with rollapplyr, but do you happen to know how to accomplish that with roll_median? – Yuanchu Dang Jan 29 '16 at 14:30
  • @KotaMori Actually setting n = 20 also gives me some strange non-NA result that I can't make sense of... Would you take a look? – Yuanchu Dang Jan 29 '16 at 17:25
  • 1
    The rollapply line could be written: `rollapplyr(data = input, width = 120, FUN = median, na.rm = TRUE, fill = NA)` . Also zoo also has a `rollmedian` function. You could look at its help `?rollmedian` and also look at `?runmed` (in base R and is written in C) which `rollmedian` calls. – G. Grothendieck Jan 29 '16 at 18:39
  • @G.Grothendieck Thanks G.! Will take a look. BTW, great last name with the algebraic geometry mathematician hah – Yuanchu Dang Jan 29 '16 at 21:13
  • I did `sapply(1:30, function(n) roll_median(input, n = n, fill = NA, align = 'center', na.rm = T))` and find weird behaviour at `n = 6, 26` and others. – Kota Mori Jan 30 '16 at 04:03

0 Answers0