Re: [R] functions of functions

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Mon 09 May 2005 - 17:34:32 EST

Robin Hankin wrote:

> Hi
>
> I have an application where my difficulty boils down to not
> being able to define a function f() with the following properties:
>
> f("sin",0:2,x) #returns sin(x+0) + sin(x+1) + sin(x+2)
> f(c("sin","cos"), 1:2,x) #returns sin(x+1) + cos(x+2)
> f(c("sin","cos","exp"),3,x) #returns sin(x+3) + cos(x+3) + exp(x+3)
>
> anyone?

Not really nice, but hopefully works:

f <- function(foo, int, x){

   # too lazy to think myself about recycling:    X <- cbind(foo, x + int)
   # mapply-ing over both columns
   values <- mapply(function(foo, x) do.call(foo, list(x)),

        X[,1], as.integer(X[,2]))
   # caculating the sum:
   return(sum(values))
}

Uwe

>
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
> tel 023-8059-7743
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon May 09 17:38:33 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:39 EST