[R] Re ading Functions that are in a Vector

Gabor Grothendieck ggrothendieck at gmail.com
Mon Sep 28 05:02:27 CEST 2009


On Sun, Sep 27, 2009 at 10:36 PM, trumpetsaz <stephaniezimmer at gmail.com> wrote:
>
> I am trying to write a function that will have an input of a vector of
> functions. Here is a simplistic example.
> sumstats <- c(mean,sd)
> sumstats[1]
> #Gives this error
> #> sumstats[1]
> #[[1]]
> #function (x, ...)
> #UseMethod("mean")
> #<environment: namespace:base>

That's not an error.  The code asked it to return the first function
so that's what it did.

>
> I thought about restricting the input to character variables such as the
> following
> sumstats2 <- c("mean","sd")
> Is there a way to change "mean" to the function mean?

This gives a vector of functions given a vector of their names:

 sumstat1 <- lapply(sumstats2, get)




More information about the R-help mailing list