R foreach with .combine = rbindlist [英] R foreach with .combine=rbindlist

查看:797
本文介绍了R foreach with .combine = rbindlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用foreach与.combine = rbindlist。这看起来不工作,虽然如果我使用.combine = rbind工作正常。

I am using foreach with a .combine = rbindlist. This does not appear to work, although it works fine if I use .combine = rbind.

只是为了说明一个简单的例子 -

Just to illustrate using a simple example --

> t2 <- data.table(col1=c(1,2,3))
> foreach (i=1:3, .combine=rbind) %dopar% unique(t2)
   col1
1:    1
2:    2
3:    3
4:    1
5:    2
6:    3
7:    1
8:    2
9:    3

# But using rbindlist gives an error

> foreach (i=1:3, .combine=rbindlist) %dopar% unique(t2)
error calling combine function:
<simpleError in fun(result.1, result.2): unused argument(s) (result.2)>
NULL

有没有人能够使这项工作?

Has anyone been able to make this work ?

提前感谢。

推荐答案

这基本上是你说的 - rbindlist 假设一个列表参数,您得到的错误与此一样:

It's basically what you said - rbindlist assumes a list argument, and the error you're getting is the same as this one:

result.1 = data.table(blah = 23)
result.2 = data.table(blah = 34)

rbindlist(result.1, result.2)
#Error in rbindlist(result.1, result.2) : unused argument (result.2)

如果你想利用 rbindlist ,那么做法是:

If you want to utilize rbindlist, the way to do it would be this:

rbindlist(foreach (i = 1:3) %dopar% unique(t2))

或此:

foreach (i=1:3, .combine=function(x,y)rbindlist(list(x,y))) %dopar% unique(t2)

这篇关于R foreach with .combine = rbindlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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