I think I have a solution to my own question, but perhaps someone can do better (and I haven't implemented FLATTEN=FALSE ...)xapply <- function(FUN,...,FLATTEN=TRUE,MoreArgs=NULL) { L <- list(...) inds <- do.call(expand.grid,lapply(L,seq_along)) ## Marek's suggestion retlist <- list() for (i in 1:nrow(inds)) { arglist <- mapply(function(x,j) x[[j]],L,as.list(inds[i,]),SIMPLIFY=FALSE) if (FLATTEN) { retlist[[i]] <- do.call(FUN,c(arglist,MoreArgs)) } } retlist} 编辑:我尝试了@baptiste的建议,但这并不容易(或者对我而言并非如此).我最接近的是edit: I tried @baptiste's suggestion, but it's not easy (or wasn't for me). The closest I got wasxapply2 <- function(FUN,...,FLATTEN=TRUE,MoreArgs=NULL) { L <- list(...) xx <- do.call(expand.grid,L) f <- function(...) { do.call(FUN,lapply(list(...),"[[",1)) } mlply(xx,f)}仍然不起作用. expand.grid确实比我想象的要灵活(尽管它创建了一个无法打印的怪异数据框),但是mlply内部发生了很多不可思议的魔术.which still doesn't work. expand.grid is indeed more flexible than I thought (although it creates a weird data frame that can't be printed), but enough magic is happening inside mlply that I can't quite make it work.这是一个测试用例:L1 <- list(data.frame(x=1:10,y=1:10), data.frame(x=runif(10),y=runif(10)), data.frame(x=rnorm(10),y=rnorm(10)))L2 <- list(y~1,y~x,y~poly(x,2)) z <- xapply(lm,L2,L1)xapply(lm,L2,L1) 这篇关于expand.grid和mapply的组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 13:35