[R] outer() problem
    Berend Hasselman 
    bhh at xs4all.nl
       
    Thu May  1 10:02:23 CEST 2014
    
    
  
On 01-05-2014, at 08:44, Marc Girondot <marc_grt at yahoo.fr> wrote:
> Dear list-members,
> 
> Can someone explains me why the last command gives an error. Thanks a lot:
> > outer(0:1, 0:1, FUN=function(x, y) {x+y})
>     [,1] [,2]
> [1,]    0    1
> [2,]    1    2
> > outer(0:1, 0:1, FUN=function(x, y) {x})
>     [,1] [,2]
> [1,]    0    0
> [2,]    1    1
> > outer(0:1, 0:1, FUN=function(x, y) {1})
> Erreur dans outer(0:1, 0:1, FUN = function(x, y) { :
>  dims [produit 4] ne correspond pas à la longueur de l'objet [1]
> 
> Of course I simplify a lot my problem.
As the documentation for outer says the function arguments x and y are vectors not scalars.
Try this to see that
outer(0:1, 0:1, FUN=function(x, y) {print(x);x}) 
outer(0:1, 0:1, FUN=function(x, y) {print(x);print(y);x}) 
You are returning a scalar whereas outer expects the function to return a vector of the correct length.
A possible solution would be
outer(0:1, 0:1, FUN=function(x, y) {1+0*x}) 
outer(0:1, 0:1, FUN=function(x, y) {rep(1,length(x))}) 
Berend
    
    
More information about the R-help
mailing list