我编写了一个带有三个参数的函数:
create.template <- function(t.list, x, y){
temp <- cbind(get(t.list[x]), get(t.list[y]), NA)
}
Run Code Online (Sandbox Code Playgroud)
此函数的输出是一个具有 11 列和 17 行的 data.frame。
现在我想用两个列表在函数上创建一个循环,一个用于 x,一个用于 y。从而
x.list <- list(1,2,3)
y.list <- list(4,5,6)
Run Code Online (Sandbox Code Playgroud)
在最后一步,我想建立类似
for (x in x.list and y in y.list){
create.template(t.list, x, y)
}
Run Code Online (Sandbox Code Playgroud)
并可能在一个最终数据帧中按行组合生成的数据帧(3 个数据帧,每个数据帧 11 列)。
我知道您可以在 Python 中使用 zip() 函数执行此操作,然后通过 append() 和 concatenate() 轻松附加结果,但到目前为止我还没有在 R 中找到等效项。任何帮助表示高度赞赏!
mget
我们可以使用 、Reduce
或do.call
来cbind
获取list
多个对象的值vectors
Reduce(cbind, c(mget(ls(pattern = "\\.list")), NA))
Run Code Online (Sandbox Code Playgroud)
或者
do.call(cbind, c(mget(c("x.list", "y.list")), NA))
Run Code Online (Sandbox Code Playgroud)