Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-26 Thread Prof Brian Ripley
On Thu, 25 Dec 2008, Oliver Bandel wrote: Bert Gunter gunter.berton at gene.com writes: FWIW: Good advice below! -- after all, the first rule of optimizing code is: Don't! For the record (yet again), the apply() family of functions (and their packaged derivatives, of course) are merely

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-26 Thread Peter Dalgaard
Prof Brian Ripley wrote: On Thu, 25 Dec 2008, Oliver Bandel wrote: The apply-functions do bring speed-advantages. This is not only what I read about it, I have used the apply-functions and really got results faster. The reason is simple: an apply-function does make in C, what otherwise

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-26 Thread Bert Gunter
Thankyou for the clarification, Brian. This is very helpful (as usual). However, I think the important point, which I misstated, is that whether it be for() or, e.g. lapply(), the loop contents must be evaluated at the interpreted R level, and this is where most time is typically spent. To get

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-26 Thread Prof Brian Ripley
On Fri, 26 Dec 2008, Bert Gunter wrote: Thankyou for the clarification, Brian. This is very helpful (as usual). However, I think the important point, which I misstated, is that whether it be for() or, e.g. lapply(), the loop contents must be evaluated at the interpreted R level, and this is

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-23 Thread Brigid Mooney
I have used some of your advice in making some changes to my function and function call before reposting. Instead of nesting many 'for' loops, I have gotten to the point where I only have one.(Also, please note, I am pasting the function calcProfit at the end of this message as it is a bit

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-23 Thread David Winsemius
On Dec 23, 2008, at 9:55 AM, Brigid Mooney wrote: I have used some of your advice in making some changes to my function and function call before reposting. Instead of nesting many 'for' loops, I have gotten to the point where I only have one.(Also, please note, I am pasting the

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-23 Thread Brigid Mooney
Thank you again for your help. I updated the parsing at the beginning of the calcProfit function with: if (class(IterParam) == numeric) { long - IterParam[long] short - IterParam[short] investment - IterParam[investment] stoploss - IterParam[stoploss]

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-23 Thread David Winsemius
On Dec 23, 2008, at 10:56 AM, Brigid Mooney wrote: Thank you again for your help. snip - With the 'apply' call, Results2 is of class list. Results2 - apply(CombParam, 1, calcProfit, X, Y)

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-23 Thread Brigid Mooney
-- Problem Description: (reproducible code below)

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-23 Thread Daniel Nordlund
Avoiding multiple nested for loops (as requested in the subject) is usually a good idea, especially if you can take advantage of vectorized functions. You were able redesign your code to use a single for loop. I presume there was a substantial improvement in program speed. How much additional

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-23 Thread David Winsemius
I have to agree with Daniel Nordlund regarding not creating subsidiary problems when the main problem has been cracked. Nonetheless, ... might you be happier with the result of changing the last data.frame() call in calcProfit to c()? I get a matrix: str(Results2) num [1:14, 1:16]

Re: [R] How can I avoid nested 'for' loops or quicken the process?

2008-12-22 Thread Charles C. Berry
On Mon, 22 Dec 2008, Brigid Mooney wrote: Hi All, I'm still pretty new to using R - and I was hoping I might be able to get some advice as to how to use 'apply' or a similar function instead of using nested for loops. Unfortunately, you have given nothing that is reproducible. The details

[R] How can I avoid nested 'for' loops or quicken the process?

2008-12-22 Thread Brigid Mooney
Hi All, I'm still pretty new to using R - and I was hoping I might be able to get some advice as to how to use 'apply' or a similar function instead of using nested for loops. Right now I have a script which uses nested for loops similar to this: i - 1 for(a in Alpha) { for (b in Beta) { for (c