2

我编写了这个漂亮的函数来为向量化参数的每个组合应用一个函数:

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()

如何重写函数,使得传递给 expand.grid 的参数不需要命名:

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

或者,是否有任何具有类似功能的现有功能?

4

1 回答 1

4

不是最优雅的,但这很有效。最重要的是,它允许您将变量传递给 expand.grid 而不用命名它们。

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))
于 2012-10-13T00:42:20.857 回答