[R] Vector of functions

Dennis Murphy djmuser at gmail.com
Sat Jul 2 22:04:52 CEST 2011


Hi,

To amplify on Josh's cogent remarks, the reason you can't create a
vector of functions is because vectors are composed of atomic objects
and functions are not atomic objects:

> is.atomic(f.3)
[1] FALSE
> is.atomic(1)
[1] TRUE
> is.atomic(TRUE)
[1] TRUE
> is.atomic('a')
[1] TRUE

'Vectors' of functions are actually (and appropriately) assigned to
lists. You can check the structure of any R object with str():

> fns <- c(f.1, f.2, f.3)
> str(fns)   ## Oops, R coerced your vector into a list
List of 3
 $ :function (a)
  ..- attr(*, "source")= chr [1:3] "function(a) {" ...
 $ :function (a)
  ..- attr(*, "source")= chr [1:3] "function(a) {" ...
 $ :function (a)
  ..- attr(*, "source")= chr [1:3] "function(a) {" ...

> funs <- list(f.1, f.2, f.3)    # clearer code
> str(funs)
List of 3
 $ :function (a)
  ..- attr(*, "source")= chr [1:3] "function(a) {" ...
 $ :function (a)
  ..- attr(*, "source")= chr [1:3] "function(a) {" ...
 $ :function (a)
  ..- attr(*, "source")= chr [1:3] "function(a) {" ...
> identical(fns, funs)   # Are the two objects identical?
[1] TRUE

You can have all kinds of fun with (lists of) functions. I wanted to
plot your second function:

> curve(funs[[2]], -10, 10)
Error in curve(funs[[2]], -10, 10) :
  'expr' must be a function, call or an expression containing 'x'

## Oh yeah, the argument of the function that curve() needs is x.
## We don't need to rewrite the function, though:

> curve(funs[[2]](x), -10, 10)

Now one can verify that the function calls make sense:

> funs[[2]](0)
[1] 1
> funs[[2]](pi/2)
[1] 0.1588316
> funs[[2]](2 * pi)
[1] 1.394927e-05
> funs[[2]](Inf)
[1] 0

If you're going to be doing a lot of work with functions, I'd suggest
picking up a book on R programming. Fortunately, there's a good one
auf Deutsch by Uwe Ligges: Programmieren mit R.
http://www.statistik.tu-dortmund.de/~ligges/PmitR/
and several good ones in English. See the book list page at CRAN:
http://www.r-project.org/doc/bib/R-books.html

HTH,
Dennis

On Sat, Jul 2, 2011 at 9:36 AM, Thomas Stibor <thomas.stibor at in.tum.de> wrote:
> Hi there,
>
> I have a vector of some functions e.g.
> #-----------------------------#
> f.1 <- function(a) {
>  return( a );
> }
> f.2 <- function(a) {
>  return( 1 - (tanh(a))^2 );
> }
> f.3 <- function(a) {
>  return( 1 / (1+exp(-a)) * (1 - (1 / (1+exp(-a)))) );
> }
>
> func.l <- c(f.1, f.2, f.3);
> #-----------------------------#
>
> and would like to calculate the output value of a function in the
> vector, that is, pick e.g. function at position 2 in func.l and calculate the
> output value for a=42.
>
> func.l[2](42); # gives error
>
> f <- func.l[2];
> f(42); # gives error
>
> Is there an easy way to solve this problem?
>
> Cheers,
>  Thomas
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list