Dear r-plus users, i would like to use outer in the following case outer(x,y,FUN="fun") i suppose that my function fun is of the following form: fun<-function(x,y) { if(y>x) return(x+y) if(y<=x) return(0) } My problem is that the command outer(x,y,FUN="fun") return me a null matrix instead of an upper triangular matrix. Is somebody have a solution ? Thanks for your help. -- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- Olivier MARTIN phone: (33) 04 76 61 53 55 Projet IS2 06 08 67 93 42 INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 655, Av. de l'Europe Montbonnot e-mail:olivier.martin at inrialpes.fr 38334 Saint Ismier cedex -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20010315/4496c794/attachment.html
On Thursday 15 March 2001 18:11, Olivier Martin wrote:> Dear r-plus users,r-plus? That's new! :-)> > i would like to use outer in the following case outer(x,y,FUN="fun") > i suppose that my function fun is of the following form: > fun<-function(x,y) > { > if(y>x) return(x+y) > if(y<=x) return(0) > } > My problem is that the command outer(x,y,FUN="fun") return me a > null matrix instead of an upper triangular matrix. > Is somebody have a solution ?you would have to change the if() clause: x and y are your data, not indices. Try this: m <- outer(x,y,FUN="+") m[lower.tri(m, diag=T)] <- 0 Cheers Kaspar -- Kaspar Pflugshaupt Geobotanical Institute ETH Zurich, Switzerland http://www.geobot.umnw.ethz.ch mailto:pflugshaupt at geobot.umnw.ethz.ch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Olivier Martin wrote:> > Dear r-plus users, > > i would like to use outer in the following case outer(x,y,FUN="fun") > i suppose that my function fun is of the following form: > fun<-function(x,y) > { > if(y>x) return(x+y) > if(y<=x) return(0) > } > My problem is that the command outer(x,y,FUN="fun") return me a > null matrix instead of an upper triangular matrix. > Is somebody have a solution ?If you supply vectors to if(y>x), then just the first element is actually used. ifelse does the job: fun <- function(x,y) { ifelse(y>x, x+y, 0) } should solve your problem Achim --------------------------- Institut f?r Statistik Technische Universit?t Wien> Thanks for your help. > > > > -- > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > Olivier MARTIN phone: (33) 04 76 61 53 55 > Projet IS2 06 08 67 93 42 > INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 > 655, Av. de l'Europe > Montbonnot e-mail:olivier.martin at inrialpes.fr > 38334 Saint Ismier cedex > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi Oliver,>>> Olivier Martin <olivier.martin at inrialpes.fr> 03/15 11:11 AM >>>Dear r-plus users, i would like to use outer in the following case outer(x,y,FUN="fun") i suppose that my function fun is of the following form: fun<-function(x,y) { if(y>x) return(x+y) if(y<=x) return(0) } My problem is that the command outer(x,y,FUN="fun") return me a null matrix instead of an upper triangular matrix. Is somebody have a solution ? The arguments x,y in fun are not labelling the cell indices in the outer product. Here are a couple simple examples of what fun will do for you.> x <- 1:3 > y <- 1:3 > outer(x,y,FUN="fun")[,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0 (Since x=y above, fun(x,y)=0 always)> x <- 1:3 > y <- 2:4 > outer(x,y,FUN="fun")[,1] [,2] [,3] [1,] 3 4 5 [2,] 4 5 6 [3,] 5 6 7>Thanks for your help. Jagat -- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- Olivier MARTIN phone: (33) 04 76 61 53 55 Projet IS2 06 08 67 93 42 INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 655, Av. de l'Europe Montbonnot e-mail:olivier.martin at inrialpes.fr 38334 Saint Ismier cedex -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._