[R] outer() is not working with my simple function

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Tue Dec 10 17:44:12 CET 2024


On 2024-12-10 11:36 a.m., Christofer Bogaso wrote:
> Hi,
> 
> I have below code
> 
> FN1 = function(x, y) 3
> 
> outer(1:9, 1:9, FN1)
> 
> With above I get error as below
> 
> Error in dim(robj) <- c(dX, dY) :
> 
>    dims [product 81] do not match the length of object [1]
> 
> Could you please help to understand why is it failing? I am just
> expecting to get a matrix with all elements as 3

The help page says this about the function:  "It must be a vectorized 
function (or the name of one) expecting at least two arguments and 
returning a value with the same length as the first (and the second)." 
So your function could be

   FN1 <- function(x, y) rep_len(3, length(x))

and it would work.

Duncan Murdoch



More information about the R-help mailing list