【发布时间】:2015-06-06 19:47:57
【问题描述】:
This solution 几乎是我需要的,但不适用于我的情况。 这是我尝试过的:
comb_apply <- function(f,...){
exp <- expand.grid(...,stringsAsFactors = FALSE)
apply(exp,1,function(x) do.call(f,x))
}
#--- Testing Code
l1 <- list("val1","val2")
l2 <- list(2,3)
testFunc<-function(x,y){
list(x,y)
}
#--- Executing Test Code
comb_apply(testFunc,l1,l2)
comb_apply(paste,l1,l2)
它适用于 paste 示例,但当我尝试使用 testFunc 时,我收到消息:Error in (function (x, y) : unused arguments (Var1 = "val1", Var2 = 2)。
我的期望是得到结果:
list(list("val1",2),list("val1",3),list("val2",2),list("val2",3))
动机
我来自Mathematica,在其上执行这个操作很简单:
l1 = {"val1", "val2"}
l2 = {2, 3}
testFunc = {#1, #2} &
Outer[testFunc, l1, l2]
在R怎么办?
【问题讨论】:
-
R有一个?outer函数。 -
@sgibb 我需要一个通用案例,其中可能有 2 个以上的列表。