[R] I might be dumb : a simple question about "foreach"

Michael Knudsen micknudsen at gmail.com
Mon Jul 20 15:05:37 CEST 2009


On Mon, Jul 20, 2009 at 2:48 PM, Olivier
ETERRADOSSI<Olivier.Eterradossi at ema.fr> wrote:

>>  x <- foreach(i = 1:3) %do% sqrt(i)
>
> and get :
>>
>> Erreur dans sqrt(i) : indice hors limites ( i.e. "error in sqrt(i) : index
>> out of bounds")

I once got similar errors because I didn't encapsulate the part af
%do% or %dopar% in curly brackets. Try

x <- foreach(i = 1:3) %do% { sqrt(i) }

I should say, however, that in this particular case, your original
code evaluates without errors on my computer (Mac OSX 10.5.7 with R
2.9.1).

By the way, remember to use %dopar% instead of %do%, if you want to
take advantage of multiple cores. While being totally ecstatic after
discovering foreach, I wrote the following (very simple) guide:

http://lifeofknudsen.blogspot.com/2009/07/most-of-work-i-do-in-r-has-to-do-with.html

Maybe you'll find it useful, maybe not.

-- 
Michael Knudsen
micknudsen at gmail.com
http://lifeofknudsen.blogspot.com/




More information about the R-help mailing list