gpt4 book ai didi

r - 同时循环遍历 R 中的两个列表

转载 作者:行者123 更新时间:2023-12-05 01:25:44 25 4
gpt4 key购买 nike

我编写了一个带有三个参数的函数:

create.template <- function(t.list, x, y){
temp <- cbind(get(t.list[x]), get(t.list[y]), NA)
}

此函数的输出是一个具有 11 列和 17 行的 data.frame。

现在我想用两个列表在函数上创建一个循环,一个用于 x,一个用于 y。从而
x.list <- list(1,2,3)
y.list <- list(4,5,6)

在最后一步中,我想建立类似
for (x in x.list and y in y.list){
create.template(t.list, x, y)
}

并可能在一个最终数据帧中按行组合生成的数据帧(3 个数据帧,每个数据帧 11 列)。

我知道您可以在 Python 中使用 zip() 函数执行此操作,然后通过 append() 和 concatenate() 轻松附加结果,但到目前为止我还没有在 R 中找到等效项。任何帮助表示高度赞赏!

最佳答案

我们可以通过 mget 获取多个对象的值, 使用 Reducedo.callcbind listvectors

Reduce(cbind, c(mget(ls(pattern = "\\.list")), NA))

或者
do.call(cbind, c(mget(c("x.list", "y.list")), NA))

关于r - 同时循环遍历 R 中的两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44518654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com