[R] Problem applying "outer" to a list of functions

Joshua Wiley jwiley.psych at gmail.com
Sat Oct 16 20:08:55 CEST 2010


Hi,

On Sat, Oct 16, 2010 at 9:52 AM, jschamel <jtschamel at alaska.edu> wrote:
>
> I'm trying to construct an array with output from the application of
> homogeneous input to a bunch of different functions (my final goal is to
> easily analyze running time of a lot of competing functions and summarize
> the results).  In my current iteration, however, only the first function in
> the list is being applied, and I'm not sure why.

Because you only call the first element of the list, fl[[1]], that is
the foo function.  You might play around with a simple example in
outer not involving functions to get a better feel for what it does.

  Here's my dummy code with
> output
>
>
>> foo <- function(x){paste('foo(',x,')',sep='')}
>> bar <- function(x){paste('bar(',x,')',sep='')}
>> outer(list(foo,bar),1:2,function(fl,x){fl[[1]](x)})
>     [,1]     [,2]
> [1,] "foo(1)" "foo(2)"
> [2,] "foo(1)" "foo(2)"
>
>
> Any insight would be appreciated!  Thanks!

For your overall goal, here is how I would call a bunch of functions
with the same arguments:

lapply(list(foo, bar), do.call, list(1:2))

Cheers

> --
> View this message in context: http://r.789695.n4.nabble.com/Problem-applying-outer-to-a-list-of-functions-tp2998406p2998406.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list