Skip to content

unvectorized option for outer()

2 messages · Liaw, Andy, Thomas Lumley

#
I'll toss in my $0.02:  The following is my attempt at creating a "general
outer" that works with more than two arguments.

gouter <- function (x, FUN, ...) {
    xgrid <- as.list(do.call("expand.grid", x))
    names(xgrid) <- NULL
    array(do.call(deparse(substitute(FUN)), c(xgrid, list(...))), 
        dim = sapply(x, length), dimnames = x)
}


Here's an example:
gouter> gouter(list(letters[1:2], 1:3, 2:4), paste, sep = "")
, , 2

  1     2     3    
a "a12" "a22" "a32"
b "b12" "b22" "b32"

, , 3

  1     2     3    
a "a13" "a23" "a33"
b "b13" "b23" "b33"

, , 4

  1     2     3    
a "a14" "a24" "a34"
b "b14" "b24" "b34"

Andy
#
On Mon, 31 Oct 2005, Liaw, Andy wrote:

            
Yes, that's the sort of thing I had in mind.  The name "gouter" isn't 
exactly to my taste -- the point was that outer() is the fast but 
restricted function and that the general but slow function should have a 
different name (eg xapply or oapply).


 	-thomas