[R] foreach(): how to do calculations "between" two foreach loops?

Marius Hofert m_hofert at web.de
Wed May 11 23:44:49 CEST 2011


Dear expeRts,

is it possible to carry out calculations between different foreach() calls? 
As for nested loops, you want to carry out calcuations not depending on the inner
loop only once and not for each iteration of the innermost loop.

Cheers,

Marius


library(foreach)

foreach(i=1:3) %:% 
    foreach(j=1:2) %do% {
	    i <- i+1 
        print(paste(i,j))
    }

foreach(i=1:3) %:% 
    i <- i+1 # lengthy calculation which has to be done only once, not for each j 
    foreach(j=1:2) %do% {
        print(paste(i,j))
    }



More information about the R-help mailing list