Skip to content

Saving run time in loop

3 messages · vincent.deluard, Rolf Turner, Dennis Murphy

#
Hi r users,

I am trying to compute the "moving variance" of a large matrix. I now use a
loop but I am looking for a faster solution. Here is a sample of the code.

Source= matrix(rnorm(400),ncol=100)
variances= matrix(rep(NA,4*100),ncol=100)

for (i in 1:80) {variances[,i]=apply(Source[,i:(i+80)],1,var)}

any idea? Many thanks in advance.


Vincent.

--
View this message in context: http://r.789695.n4.nabble.com/Saving-run-time-in-loop-tp3462228p3462228.html
Sent from the R help mailing list archive at Nabble.com.
#
On 20/04/11 18:30, vincent.deluard wrote:
It's not at all clear what you are actually trying to do, and the sample
code that you give does not work.  I.e. it throws an error.

Be more careful and think more clearly.

     cheers,

         Rolf Turner
#
Hi:

Perhaps rollapply() in the zoo package might be helpful.

Dennis

On Tue, Apr 19, 2011 at 11:30 PM, vincent.deluard
<vincent.deluard at trimtabs.com> wrote: