Re: [R] functions of functions

From: Rich FitzJohn <rich.fitzjohn_at_gmail.com>
Date: Mon 09 May 2005 - 18:14:32 EST

This has a fairly brutal approach to recycling, and won't warn you if one argument's length is not a multiple of another, etc.

But seems to do the trick.

f <- function(f, y, x) {

  n <- max(c(length(f), length(y)))
  f <- rep(f, length.out=n)
  y <- rep(y, length.out=n)

  rowSums(mapply(function(f, y) (match.fun(f))(y + x), f=f, y=y)) }

Testing, it matches your examples...
x <- rnorm(20)
identical(all.equal(sin(x+0) + sin(x+1) + sin(x+2),

                    f("sin",0:2,x)), TRUE)
identical(all.equal(sin(x+1) + cos(x+2),
                    f(c("sin","cos"), 1:2,x)), TRUE)
identical(all.equal(sin(x+3) + cos(x+3) + exp(x+3),
                    f(c("sin","cos","exp"),3,x)), TRUE)

Cheers,
Rich

On 5/9/05, Robin Hankin <r.hankin@noc.soton.ac.uk> wrote:
> Hello Uwe
>
> thanks for this. Unfortunately it doesn't quite do what I want:
>
> R> x <- c(0.3,0.3,0.5)
> R> f(c("sin","cos"),1:2,x)
>
> 1] 1.266795
> Warning messages:
> 1: longer object length
> is not a multiple of shorter object length in: x + int
> 2: number of rows of result
> is not a multiple of vector length (arg 1) in: cbind(foo, x + int)
> R>
>
> [
>
> I need
>
> R> sin(x+1) + cos(x+2)
> [1] 0.2972822 0.2972822 0.1963514
>
> ]
>
> best wishes
>
> Robin
>
>
> On May 9, 2005, at 08:34 am, Uwe Ligges wrote:
>
> > 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
>

-- 
Rich FitzJohn
rich.fitzjohn <at> gmail.com   |    http://homepages.paradise.net.nz/richa183
                      You are in a maze of twisty little functions, all alike

______________________________________________
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 18:18:28 2005

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