Skip to content

more about outer

4 messages · Peter Dalgaard, Erich Neuwirth

#
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))))
}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Erich Neuwirth <erich.neuwirth at univie.ac.at> writes:
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...
#
thanks,

your original code did not wortk for me,
but the following version seems to work


vectorize2 <- function(f) 
    function(x, y,...)
        sapply(1:length(x), function(i)f(x[i],y[i],...))
Peter Dalgaard BSA wrote:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Erich Neuwirth <erich.neuwirth at univie.ac.at> writes:
...
Oops, yes. What I meant was
 
  sapply(seq(along=x), function(i)f(x[i],y[i],...))

(There's a difference if length(x) == 0)