[R] foreach (package) .combine option

Luca Cerone luca.cerone at gmail.com
Tue Apr 29 16:42:39 CEST 2014


Dear all,
I am having some difficulties understanding how the .combine option in
the foreach function of the "foreach" package works.

I think I understand the examples in the manual using rbind, cbind and c.

However if I write my own function (no particular purpose in mind,
just to figure out how the .combine option works):

cfun <- function(a,b) sqrt(a**2 + b**2)

I don't understand the output of the function:

For example :

x <- foreach(i=1:3, .combine='cfun') %do% seq(5)

Produces:
> x
[1] 1.732051 3.464102 5.196152 6.928203 8.660254

Now so far as I understand these are in order:

sqrt(1+1+1), sqrt(4 + 4 + 4) [ = sqrt(2**2 + 2**2 + 2**2) ], sqrt(27)
[ = sqrt(3**2 + 3**2 + 3**2)] and so on...

But really I don't understand how cfun is used to produced these
results, what is passed to a and b in this case???

Thanks a lot you all,


Cheers,
Luca



More information about the R-help mailing list