R foreach 与 .combine=rbindlist [英] R foreach with .combine=rbindlist
本文介绍了R foreach 与 .combine=rbindlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带有 .combine = rbindlist 的 foreach.这似乎不起作用,尽管如果我使用 .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
有人能做到吗?
提前致谢.
推荐答案
基本上就是你所说的 - rbindlist
假定一个 list
参数,并且你得到的错误和这个一样:
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 与 .combine=rbindlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文