[R] more about outer

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sun Sep 23 18:09:58 CEST 2001


Erich Neuwirth <erich.neuwirth at univie.ac.at> writes:

> this is my current answer to my own question.
> should we have such a funcion in a more general version
> (i.e. with an arbitraty number of argument lists, for n-ary
> functions)
> in the core language?
> 
> myouter<-function(x,y,fun){
> t(sapply(x,function(arg1) sapply(y,function(arg2) fun(arg1,arg2))))
> }

I'd be partial to this kind of constructions 

vectorize2 <- function(f) 
    function(x, y,...)
        sapply(seq, along=x, function(i)f(x[i],y[i],...))

so that you can simply do

outer(x, y, vectorize2(fun))

A similar technique for functions of one variable would be

vectorize <- function(f) 
    function(x, ...)
        sapply(x, f)


The case of N variables is -um- interesting...

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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