Re: [R] Speeding things up

From: Henrik Bengtsson <hb_at_stat.berkeley.edu>
Date: Mon 08 Jan 2007 - 11:27:18 GMT

First, since you only update the 'ddtd' conditioned on 'value', you should be able to vectorize removing the loop. I let you figure out how to do that yourself.

Second, you apply the "$" operator multiple times in the loop that will definitely add some overhead. It should be faster to extract 'value' and 'ddtd' and work with those instead.

/Henrik

On 1/8/07, Benjamin Dickgiesser <dickgiesser@gmail.com> wrote:
> Hi,
>
> is it possible to do this operation faster? I am going over 35k data
> entries and this takes quite some time.
>
> for(cnt in 2:length(sdata$date))
> {
>
> if(sdata$value[cnt] < sdata$value[cnt - 1]) {
> sdata$ddtd[cnt] <- sdata$ddtd[cnt - 1] + sdata$value[cnt - 1] -
> sdata$value[cnt]
> }
> else sdata$ddtd[cnt] <- 0
>
> }
> return(sdata)
>
> Thank you,
> Benjamin
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>



R-help@stat.math.ethz.ch 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. Received on Tue Jan 09 05:14:21 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 08 Jan 2007 - 19:31:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.