1

Take

 cubeAndAdd<-function(x,y){x^3+y^3} outer(-1:1,-1:1,function(x,y) Vectorize(cubeAndAdd(x,y))) 

Upon running this, you will get the warning message:

Warning message: In formals(fun) : argument is not a function 

Why is this? After all, if I truly wasn't using a function, then this code wouldn't run at all.

3
  • Maybe just do: outer(-1:1,-1:1,Vectorize(cubeAndAdd))? Since you already define x and y, is there a need to redefine a lambda? Commented May 11, 2020 at 15:12
  • 1
    @NelsonGon That works and is definitely better than what I had, but it doesn't explain the error. Commented May 11, 2020 at 15:21
  • The error is defining function(x,y) again which introduces another lambda and hence it is unclear what to match to the function. If you really want to be explicit, then: outer(-1:1,-1:1,Vectorize(function(x,y) cubeAndAdd(x,y))) Commented May 11, 2020 at 15:22

1 Answer 1

3

The problem comes from what you're 'feeding' to Vectorize.

Vectorize wants a function as its argument. cubeAndAdd is a function, but cubeAndAdd(x,y) is a function call.

To make your outer loop syntactically correct, you should use Vectorize to create the vectorized function, and then call that new function:

outer(-1:1,-1:1,function(x,y) Vectorize(cubeAndAdd)(x,y)) 

Here, Vectorize(cubeAndAdd) is the function, and you're calling it using (x,y) as arguments: so Vectorize(cubeAndAdd)(x,y)

(Although the suggestion to just remove the entire anonymous function(x,y) from the outer loop works here (and makes the one-liner shorter), it's often a good idea to explicitly 'feed' the arguments to the function, as you are doing, since this allows one to use functions that expect additional arguments).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.