2

I wrote this nifty function to apply a function for every combination of vectorized arguments:

require(plyr)
require(ggplot2)


###eapply accepts a function and and a call to expand grid
###where columns created by expand.grid must correspond to arguments of fun
##each row created by expand.grid will be called by fun independently

###arguments
##fun either a function or a non-empty character string naming the function to be called.
###... vectors, factors, or a list containing thse

###value
###a data frame

##Details
##at this time, elements of ... must be at least partially named to match args of fun
##positional matching does not work

###from the ddply documentation page:
###The most unambiguous behaviour is achieved when fun returns a data frame - in that case pieces will 
###be combined with rbind.fill. If fun returns an atomic vector of fixed length, it will be rbinded 
###together and converted to a data frame. Any other values will result in an error. 

eapply <- function(fun,...){
    if(!is.character(fun)) fun <- as.character(substitute(fun))
    adply(
        expand.grid(...),
        1,
        function(x,fun) do.call(fun,x),
        fun
    )
}


##example use:

m <- function(n,visit.cost){
    if(n*visit.cost < 250){
        c("total.cost"=n*visit.cost)
    }else{
        c("total.cost"=250 + (n*visit.cost-250)*.25)
    }

}


d <- eapply(m, n=1:30, visit.cost=c(40,60,80,100))


ggplot(d,aes(x=n,y=total.cost,color=as.factor(visit.cost),group=visit.cost)) + geom_line()

How can I rewrite the function such that the arguments passed to expand.grid need not be named:

d <- eapply(m, 1:30, c(40,60,80,100))

Alternatively, are there any existing functions that have similar functionality?

Michael
  • 5,808
  • 4
  • 30
  • 39
  • 2
    Not an answer, but this is somewhat similar to http://stackoverflow.com/questions/6515134/combination-of-expand-grid-and-mapply – Ben Bolker Oct 13 '12 at 00:05
  • 1
    hmm, I didn't know about the m*ply functions. Of course Hadley has thought of everything... – Michael Oct 13 '12 at 00:08

1 Answers1

4

Not the most elegant but this works. Most importantly, it allows you to pass variables to expand.grid without naming them.

eeyore <- function(fun, ...){
    if(!is.character(fun)) fun <- as.character(substitute(fun))

f <- match.fun(fun)
args <- as.list(substitute(list(...)))[-1]
foo <- expand.grid(llply(args, eval))
foo$F <- apply(foo, 1, function(x) { f(x[[1]], x[[2]])})
foo
}

d <- eeyore(m, 1:30, c(40,60,80,100))
Maiasaura
  • 32,226
  • 27
  • 104
  • 108