From tlumley at u.washington.edu  Mon Oct 31 20:33:59 2005
From: tlumley at u.washington.edu (Thomas Lumley)
Date: Mon, 31 Oct 2005 11:33:59 -0800 (PST)
Subject: [Rd] [R] unvectorized option for outer()
In-Reply-To: <39B6DDB9048D0F4DAD42CB26AAFF0AFAFED53F@usctmx1106.merck.com>
References: <39B6DDB9048D0F4DAD42CB26AAFF0AFAFED53F@usctmx1106.merck.com>
Message-ID: <Pine.LNX.4.63a.0510311131070.24405@homer22.u.washington.edu>

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

>> From: Thomas Lumley
>>
>> On Sun, 30 Oct 2005, Jonathan Rougier wrote:
>>
>>> I'm not sure about this.  Perhaps I am a dinosaur, but my feeling is
>>> that if people are writing functions in R that might be subject to
>>> simple operations like outer products, then they ought to be writing
>>> vectorised functions!
>>
>> I would agree.  How about an oapply() function that does
>> multiway (rather
>> than just two-way) outer products.  Basing the name on "apply" would
>> emphasize the similarity to other flexible, not particularly
>> optimized
>> second-order functions.
>>
>>  	-thomas
>
> 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)
> }
>

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



[Rd] [R] unvectorized option for outer()
Thomas Lumley