[R] How to?

Yves Gauvreau cyg at sympatico.ca
Thu Aug 24 16:57:30 CEST 2000


Ok, I used a dumb example and I kind of forgot that m was already a matrix.

Prof Ripley suggested I use matrix operator in this case which is a lot
faster.

In fact what I'd like to know is what is the most efficient R way to do
thing like a moving average on a vector. Something generic enough that one
could use practically any function on a constant width window of the vector?

Thanks

Yves Gauvreau



----- Original Message -----
From: "Duncan Murdoch" <murdoch at stats.uwo.ca>
To: "Yves Gauvreau" <cyg at sympatico.ca>
Sent: Thursday, August 24, 2000 10:33 AM
Subject: Re: [R] How to?


> On Thu, 24 Aug 2000 10:13:05 -0400, you wrote in message
> <000901c00dd5$6c2baa50$0201a8c0 at Workgroup>:
>
> >Hi,
> >
> >Is there a way to apply a function to rows or columns of a matrix?
> >
> >Now I use apply(as.matrix(1:nrow(m), 1, function(x) mean(m[x,])) which
works
> >fine but kind of slow on large matrix. I'm sure there is something on
this
> >somewhere but I can't find it.
>
> You're very close:  you want
>
> apply(m,1,mean)
>
> to get means by row, and
>
> apply(m,2,mean)
>
> to get means by column.  It's slow, there's no way around that.
>
> Duncan Murdoch
>

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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