Saving run time in loop

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving run time in loop

vincent.deluard
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.
Reply | Threaded
Open this post in threaded view
|

Re: Saving run time in loop

Rolf Turner-3
On 20/04/11 18:30, vincent.deluard wrote:

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

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Saving run time in loop

djmuseR
In reply to this post by vincent.deluard
Hi:

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

Dennis

On Tue, Apr 19, 2011 at 11:30 PM, vincent.deluard
<[hidden email]> wrote:

> 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.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Out of Office Reply

vincent.deluard
This post has NOT been accepted by the mailing list yet.

Thank you for your message. I am currently on my honey moon, with limited access to e-mail.

 

I will be returning on Tuesday May 3.

 

For urgent issues, please contact Minyi Chen at [hidden email]  or  (+1) 646-512-5616.

 

--------------------------------------------

Vincent Deluard, CFA

[hidden email]

Executive  Vice President - Research

TrimTabs Investment Research

40 Wall Street, 28th Floor

New York, NY 10005

Phone: (+1) 646-512-5616

 

Reply | Threaded
Open this post in threaded view
|

Out of Office Reply

vincent.deluard
This post has NOT been accepted by the mailing list yet.
In reply to this post by Rolf Turner-3

Thank you for your message. I am currently on my honey moon, with limited access to e-mail.

 

I will be returning on Tuesday May 3.

 

For urgent issues, please contact Minyi Chen at [hidden email]  or  (+1) 646-512-5616.

 

--------------------------------------------

Vincent Deluard, CFA

[hidden email]

Executive  Vice President - Research

TrimTabs Investment Research

40 Wall Street, 28th Floor

New York, NY 10005

Phone: (+1) 646-512-5616