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. Thanks a lot Marc
Marc Girondot <marc_grt <at> yahoo.fr> writes:> > 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]Because whatever the dimensions of your 2 input vectors, this function simply returns the value 1 and outer expects to generate an output that has a value for each pair of x and y values, not a single value for the whole set of xy pairs. Note the lines in the source where the error occurs robj <- FUN(X, Y, ...) dim(robj) <- c(dX, dY) where dX and dY are the lengths of x and y, respectively in your case;> Thanks a lot > > Marc >-- Kenneth Knoblauch Inserm U846 Stem-cell and Brain Research Institute Department of Integrative Neurosciences 18 avenue du Doyen L?pine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: +33 (0)6 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html
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
On Apr 30, 2014, at 11:44 PM, Marc Girondot 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.Try using rep() to get a vector of the correct length:> outer(0:1, 0:1, FUN=function(x, y) {rep(1, length(x))})[,1] [,2] [1,] 1 1 [2,] 1 1> > Thanks a lot > > Marc > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA