R 位置匹配欺骗

R 位置匹配欺骗,r,plyr,R,Plyr,我编写了这个漂亮的函数,为每个向量化参数组合应用一个函数: 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

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

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

或者,是否存在具有类似功能的现有功能?

不是最优雅的功能,但它可以工作。最重要的是,它允许您将变量传递给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))

这不是最优雅的,但很管用。最重要的是,它允许您将变量传递给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))

不是答案,但这有点类似于嗯,我不知道m*ply函数。当然Hadley已经想到了一切…不是答案,但这有点类似于嗯,我不知道m*ply函数。当然,Hadley已经想到了一切……我不喜欢调用函数eapplyBooo。不要让这个坏名声永久化!如果它被重命名,我肯定会向上投票。@Dason:Tyler:下一个版本中需要有一个具有该名称的基函数;我不喜欢调用函数eapplyboo。不要让这个坏名声永久化!如果它被重命名,我肯定会向上投票。@Dason:Tyler:下一个版本中需要有一个具有该名称的基函数;