s-news
[Top] [All Lists]

Re: moving median in S without looping

To: "Loecher, Markus" <Markus.Loecher@scr.siemens.com>
Subject: Re: moving median in S without looping
From: John Fox <jfox@mcmaster.ca>
Date: Tue, 21 Jan 2003 08:39:16 -0500
Cc: "'s-news@wubios.wustl.edu'" <s-news@wubios.wustl.edu>
In-reply-to: <20B20848358CDA44AB6A2E277D2E1C5ED66BC5@postoffice.scr.siem ens.com>
Dear Markus,

You could use the following function (which loops only over the span of the median, not over the data):

    running.median <- function(x, lag){
        n <- length(x)
        X <- matrix(0, n, lag)
        for (i in 1:lag) X[1:(n-i), i] <- x[-(1:i)]
        apply(X, 1, median)[1:(n-lag)]
        }

(You might also want to take a look at the smooth() function.)

I hope that this helps,
 John

At 01:49 PM 1/20/2003 -0500, you wrote:
Hello,
does anyone know how to compute a moving median in Splus without using
explicit looping ?

Thanks so much,

Markus
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox@mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------


<Prev in Thread] Current Thread [Next in Thread>