[R] outer

Achim Zeileis zeileis at ci.tuwien.ac.at
Thu Mar 15 18:53:09 CET 2001


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list