Re: [Rd] Speed of for loops

2007-01-31 Thread Tom McCallum
Thank you all for your advice and tips. In the end, I think the for loop is the easiest way forward due to other requirements but its good to know that I haven't missed anything too obvious. Tom On Tue, 30 Jan 2007 23:42:27 -, Oleg Sklyar <[EMAIL PROTECTED]> wrote: > It is surely an ele

Re: [Rd] Speed of for loops

2007-01-30 Thread Herve Pages
Hi, Byron Ellis wrote: > IIRC a for loop has more per-iteration overhead that lapply, but the > real answer is "it depends on what you're doing exactly." I've seen it > be a faster, slower and equal approach. gen.iter = function(y=NA) { function(x) { y <<- if(is.na(y)) x else x+y } } sapply

Re: [Rd] Speed of for loops

2007-01-30 Thread Byron Ellis
IIRC a for loop has more per-iteration overhead that lapply, but the real answer is "it depends on what you're doing exactly." I've seen it be a faster, slower and equal approach. On 1/30/07, Oleg Sklyar <[EMAIL PROTECTED]> wrote: > It is surely an elegant way of doing things (although far from be

Re: [Rd] Speed of for loops

2007-01-30 Thread Oleg Sklyar
It is surely an elegant way of doing things (although far from being easy to parse visually) but is it really faster than a loop? After all, the indexing problem is the same and sapply simply does the same job as for in this case, plus "<<-" will _search_ through the environment on every single

Re: [Rd] Speed of for loops

2007-01-30 Thread Byron Ellis
Actually, better yet: gen.iter = function(y=NA) { function(x) { y <<- if(is.na(y)) x else x+y } } sapply(x,gen.iter()) On 1/30/07, Byron Ellis <[EMAIL PROTECTED]> wrote: > Actually, why not use a closure to store previous value(s)? > > In the simple case, which depends on x_i and y_{i-1}

Re: [Rd] Speed of for loops

2007-01-30 Thread Byron Ellis
Actually, why not use a closure to store previous value(s)? In the simple case, which depends on x_i and y_{i-1} gen.iter = function(x) { y = NA function(i) { y <<- if(is.na(y)) x[i] else y+x[i] } } y = sapply(1:10,gen.iter(x)) Obviously you can modify the function for the bo

Re: [Rd] Speed of for loops

2007-01-30 Thread Herve Pages
Tom McCallum wrote: > Hi Everyone, > > I have a question about for loops. If you have something like: > > f <- function(x) { > y <- rep(NA,10); > for( i in 1:10 ) { > if ( i > 3 ) { > if ( is.na(y[i-3]) == FALSE ) { >

Re: [Rd] Speed of for loops

2007-01-30 Thread Ramon Diaz-Uriarte
On Tuesday 30 January 2007 15:46, Tamas K Papp wrote: > On Tue, Jan 30, 2007 at 12:15:29PM +, Oleg Sklyar wrote: > > magnitude using c-functions for "complex" vector indexing operations. If > > you need instructions, I can send you a very nice "Step-by-step guide > > for using C/C++ in R" which

Re: [Rd] Speed of for loops

2007-01-30 Thread Tamas K Papp
On Tue, Jan 30, 2007 at 12:15:29PM +, Oleg Sklyar wrote: > magnitude using c-functions for "complex" vector indexing operations. If > you need instructions, I can send you a very nice "Step-by-step guide > for using C/C++ in R" which goes beyond "Writing R Extensions" document. Hi Oleg, Ca

Re: [Rd] Speed of for loops

2007-01-30 Thread Oleg Sklyar
Tom, *apply's generally speed up calculations dramatically. However, if and only if you do a repetitive operation on a vector, list matrix which does NOT require accessing other elements of that variable than the one currently in the *apply index. This means in your case any of *apply will not