# [R] Problem with "outer" function

Alexander Sokol alexandersokol at ofir.dk
Thu Oct 28 15:30:29 CEST 2004

```Hello,

I am having a problem with the "outer" function.

I am using R 1.9.1 on Windows 2000. My problem may be described as follows:

I have a function f of 3 variables and 3 vectors, aa, bb and cc. I would like
to evaluate this function in a 3-dimensional grid of points corresponding to
all combinations of the elements of the 3 vectors. For example, if

aa<-c(1,2)
bb<-c(3,4)
cc<-c(5,6)

then I'd like a loop which evaluates the 2^3 values f(1,3,5), f(1,3,6),
f(1,4,5), f(1,4,6),f(2,3,5),f(2,3,6),f(2,4,5) and f(2,4,6).

The easy way to do this would be to nest 3 for-next loops, but I hope that I
more elegant way is possible, especially because in the actual problem at hand
I need to make such evaluations with a function of n parameters. I was hoping
to create a 3-dimensional array consisting of 3-element vectors, and then be
able to use a single loop to get through all the values. I expected to do this
with

outer(outer(aa,bb,c),cc,c)

However, I get the following error when I use the "outer" function with the
concatenate function "c" as the third argument:

>outer(aa,bb,c)
Error in outer(aa,bb,c) : dim<- : dims [product 4] do not match the length of
object [8]

Does anyone know a way to get around this? I have consulted the R manuals
without results, and I can't seem to find the problem described in the mail
archives either.

Thanks,
Alexander

```