[R] How to avoid a for-loop?

2007-03-12 Thread Oliver Faulhaber
Hi all, as I am trying to move slowly from just working to good code, I'd like to ask if there's a smarter way than using a for-loop in tasks like the example below. I need to obtain the extrema of the cumulated sum of a detrended time series. The following code is currently used, please have a

Re: [R] How to avoid a for-loop?

2007-03-12 Thread jim holtman
I think you only have to calculate X.cum.sum[1:i]-seq(1:i)*X.cum.mean[i] once for the entire vector since it appears that you are just computing it over and over again for lengthing vectors. Here I have just computed it once and then did a running min/max on the data. This should run faster:

Re: [R] How to avoid a for-loop?

2007-03-12 Thread jim holtman
Forget this. I missed the single index on X.cum.mean[i]!! On 3/12/07, jim holtman [EMAIL PROTECTED] wrote: I think you only have to calculate X.cum.sum[1:i]-seq(1:i)*X.cum.mean[i] once for the entire vector since it appears that you are just computing it over and over again for lengthing