在R(package doParallel)中使用%dopar%而不是%do%时出错 [英] Error when using %dopar% instead of %do% in R (package doParallel)

查看:1161
本文介绍了在R(package doParallel)中使用%dopar%而不是%do%时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了一个奇怪的错误。
假设在一个名为data的列表中有10个xts对象。我现在使用

  data_names<  - 名称(数据)
combs< - combn( data_names,3)

我的基本目标是在1080个三元组上做一个PCA。
为了加快速度,我想要使用 doParallel 包。所以这里是缩短的代码片段,直到发生错误:

  list<  -  foreach(i = 1:ncol梳子))%darb%{
tmp_triple < - combs [,i]

p1 <-data [tmp_triple [[1]]] [[1]]
p2 < ; -data [tmp_triple [[2]]] [[1]]
p3 <-data [tmp_triple [[3]]] [[1]]

data.merge< - 合并(p1,p2,p3,all = FALSE)
}

合并功能似乎是问题。错误是

任务1失败 - 不能将类c(xts,zoo)强制转换为data.frame

到目前为止,我还是无法找到解决这个问题的办法,而且我甚至不知道该找什么。

解决方案

问题可能是您没有为每个worker调用 library(xts)。你不会说什么后端你使用,所以我不能100%肯定。

如果这是问题,那么这段代码将解决它: / p>

  list<  -  foreach(i = 1:ncol(combs))%dopar%{
library(xts)
tmp_triple < - combs [,i]

p1 <-data [tmp_triple [[1]]] [[1]]
p2 <-data [tmp_triple [ 2]]] [[1]]
p3 <-data [tmp_triple [[3]]] [[1]]

data.merge< - merge(p1,p2, p3,all = FALSE)
}


I've come up with a strange error. Suppose I have 10 xts objects in a list called data. I now search for every three combinations using

   data_names <- names(data)
   combs <- combn(data_names, 3)

My basic goal is to do a PCA on those 1080 triples. To speed things up I wanted do use the package doParallel. So here is the snippet shortened till the point where the error occurs:

list <- foreach(i=1:ncol(combs)) %dopar% {
    tmp_triple <- combs[,i]

    p1<-data[tmp_triple[[1]]][[1]]
    p2<-data[tmp_triple[[2]]][[1]]
    p3<-data[tmp_triple[[3]]][[1]]

    data.merge <- merge(p1,p2,p3,all=FALSE)
}

Here, the merge function seems to be the problem. The error is

task 1 failed - "cannot coerce class 'c("xts", "zoo")' into a data.frame"

However, when changing %dopar% to a normal serial %do% everything works as accepted.

Till now I was not able to find any solution to this problem and I'm not even sure what to look for.

解决方案

The problem is likely that you haven't called library(xts) on each of the workers. You don't say what backend you're using, so I can't be 100% sure.

If that's the problem, then this code will fix it:

list <- foreach(i=1:ncol(combs)) %dopar% {
    library(xts)
    tmp_triple <- combs[,i]

    p1<-data[tmp_triple[[1]]][[1]]
    p2<-data[tmp_triple[[2]]][[1]]
    p3<-data[tmp_triple[[3]]][[1]]

    data.merge <- merge(p1,p2,p3,all=FALSE)
}

这篇关于在R(package doParallel)中使用%dopar%而不是%do%时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆