19
> fun1 <- function(x,y){x+y} > outer(seq(1,5,length=5),seq(6,10,length=5),fun1) [,1] [,2] [,3] [,4] [,5] [1,] 7 8 9 10 11 [2,] 8 9 10 11 12 [3,] 9 10 11 12 13 [4,] 10 11 12 13 14 [5,] 11 12 13 14 15 > fun2 <- function(x,y){z<-c(x,y);z[1]+z[2]} > outer(seq(1,5,length=5),seq(6,10,length=5),fun2) Error in dim(robj) <- c(dX, dY) : dims [product 25] do not match the length of object [1] 

Why doesn't fun2() work? Aren't fun2() and fun1() essentially the same thing?

1
  • 5
    No, they are not the same. fun1 returns the vector sum of x and y, while fun2 returns the scalar sum of the first and second elements of combined x and y. Commented Apr 5, 2011 at 15:39

2 Answers 2

18

As an alternative, you can just replace fun2 with Vectorize(fun2) when passing it as argument to outer:

fun2 <- function(x,y){z<-c(x,y);z[1]+z[2]} outer(seq(1,5,length=5),seq(6,10,length=5), Vectorize(fun2)) 
Sign up to request clarification or add additional context in comments.

Comments

6

The answer becomes obvious if you read ?outer:

Details: ‘X’ and ‘Y’ must be suitable arguments for ‘FUN’. Each will be extended by ‘rep’ to length the products of the lengths of ‘X’ and ‘Y’ before ‘FUN’ is called. ‘FUN’ is called with these two extended vectors as arguments. Therefore, it must be a vectorized function (or the name of one), expecting at least two arguments. 

Think about what you are doing, you are concatenating two vectors into one vector, then sum the first and second elements of this vector. fun1() on the other hand does the vectorised sum of the inputs, so the returned object is of the same length as the individual lengths of the inputs. In fun2(), the output is a vector of length 1 and it was expecting 25.

The way to make the idea behind fun2() work is to cbind() not c() the two inputs:

> fun3 <- function(x, y) { z <- cbind(x, y); z[,1] + z[,2]} > outer(seq(1,5,length=5),seq(6,10,length=5),fun3) [,1] [,2] [,3] [,4] [,5] [1,] 7 8 9 10 11 [2,] 8 9 10 11 12 [3,] 9 10 11 12 13 [4,] 10 11 12 13 14 [5,] 11 12 13 14 15 

1 Comment

so Outer works like the following: 1. a<-expand.grid(seq1,seq2) 2. b<-apply(a,1,sum) 3.matrix(b,nr=5) right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.