R如何从foreach循环返回多个数据帧作为输出

时间:2018-06-19 09:39:55

标签: r dataframe foreach return

我试图平行下面的模拟我与foreach一起运行,但我正在努力将结果组合成一个有意义的数据结构。

我想做什么(或者我没有并行化的方式): 1.创建4个数据框以将结果存储在: 2.运行我的模拟循环(例如100次),并在每个循环中将结果存储到我的4个数据帧的相应行中。

示例代码:

sim_n = 10

sim_power = as.data.frame(matrix(nrow=sim_n, ncol=8))
sim_rank  = as.data.frame(matrix(nrow=sim_n, ncol=8))
sim_mean  = as.data.frame(matrix(nrow=sim_n, ncol=8))
sim_base  = as.data.frame(matrix(nrow=sim_n, ncol=8))

for (i in 1:sim_n)
{
 # Here I'm doing sth. different in reality, so take this just as an example
 sim_power[i,] = runif(8, 1, 10)
 sim_rank[i,] = runif(8, 1, 10)
 sim_mean[i,] = runif(8, 1, 10)
 sim_base[i,] = runif(8, 1, 10)
}

在我完成循环之后,我有4个不错的数据框,一切都很好。

现在尝试用foreach做这个,我不知道如何设置它。我基本上做了或多或少相同,只是将循环改为:

test = foreach (i =1:sim_n) %dopar%
{
 # Here I'm doing sth. different in reality, so take this just as an example
 sim_power[i,] = runif(8, 1, 10)
 sim_rank[i,] = runif(8, 1, 10)
 sim_mean[i,] = runif(8, 1, 10)
 sim_base[i,] = runif(8, 1, 10)
}

但它显然不起作用。我用Google搜索,发现我可能需要创建自己的输出功能,但我真的不知道该怎么做。

注意:在foreach循环之前,我添加了所有相关的并行化初始化,以便工作正常。

更新

如果我执行以下操作,我相信我会将结果列为列表列表,但话说再来一次,我不确定如何对其进行重组以获得4个数据帧。< / p>

test = foreach (i =1:sim_n) %dopar%
{
 # Here I'm doing sth. different in reality, so take this just as an example
 sim_power = runif(8, 1, 10)
 sim_rank = runif(8, 1, 10)
 sim_mean = runif(8, 1, 10)
 sim_base = runif(8, 1, 10)

 return(list(sim_power, sim_rank, sim_mean, sim_base))
}

2 个答案:

答案 0 :(得分:1)

使用列表是个好主意。您也可以使用parLapply直接为您提供列表。您可以采取以下措施:

test = foreach (i =1:sim_n) &dopar%
{
 # Here I'm doing sth. different in reality, so take this just as an example
 sim_power = runif(8, 1, 10)
 sim_rank = runif(8, 1, 10)
 sim_mean = runif(8, 1, 10)
 sim_base = runif(8, 1, 10)

 return(list(sim_power, sim_rank, sim_mean, sim_base))
}

library(data.table)

sim_power <- rbindlist(lapply(test,function(x){x[[1]]}))
sim_rank <- rbindlist(lapply(test,function(x){x[[2]]}))
sim_mean <- rbindlist(lapply(test,function(x){x[[3]]}))
sim_base <- rbindlist(lapply(test,function(x){x[[4]]}))

答案 1 :(得分:0)

在denis的帮助下,进一步搜索了我遇到的错误,我想我找到了解决方案。不确定这是最优雅还是最快的方法,但似乎可行:

test = foreach (i =1:sim_n) %dopar%
{
 # Here I'm doing sth. different in reality, so take this just as an example
 sim_power = runif(8, 1, 10)
 sim_rank = runif(8, 1, 10)
 sim_mean = runif(8, 1, 10)
 sim_base = runif(8, 1, 10)

 return(list(sim_power, sim_rank, sim_mean, sim_base))
}

sim_power = as.data.frame(do.call(rbind,lapply(test,function(x){x[[1]]})))
sim_rank  = as.data.frame(do.call(rbind,lapply(test,function(x){x[[2]]})))
sim_mean  = as.data.frame(do.call(rbind,lapply(test,function(x){x[[3]]})))
sim_base  = as.data.frame(do.call(rbind,lapply(test,function(x){x[[4]]})))