[R] Running median
David Brahm
brahm at alum.mit.edu
Tue Aug 20 02:18:47 CEST 2002
I have a Date x Stock (223 x 520) matrix of "trading volume". I can calculate
a 5-day (past) average in about 1 second using:
R> apply(vol, 1, filter, filter=c(0, rep(1/5,5)), sides=1)
I would like to do the same with a 5-day median, e.g.:
R> mymed <- function(x, n=5) {
R> r <- rep(NA, length(x))
R> for (i in (n+1):length(x)) r[i] <- median(x[i-(1:n)])
R> return(r)
R> }
R> apply(vol, 1, mymed)
only faster (the above takes 65 seconds). Is there already a function (or some
C code) to do this? Any clever way to vectorize it?
smooth() in package "eda" with kind="3" calculates a running median of 3
values, so I may start with the code in library/eda/src/smooth.c, but it
doesn't generalize easily to N values. Also, decmedian() in package "pastecs"
may be relevant, but doesn't seem any faster than my naive code.
Thanks!
--
-- David Brahm (brahm at alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list