【发布时间】:2014-02-17 20:09:03
【问题描述】:
我正在使用 data.table 对大型数据集(45M 行,4 个 int 列)进行一些重复查找。
这就是我想做的。
library(data.table)
# generate some data, u's can show up in multiple s's
d1 <- data.table(u=rep(1:500,2), s=round(runif(1000,1,100),0))
setkey(d1, u, s)
# for each u, I want to lookup all their s's
us <- d1[J(u=1), "s", with=F]
# for each of the s's in the above data.table,
# I want to lookup other u's from the parent data.table d1
# DOESN'T WORK:
otherus <- d1[J(s = us), "u", with=F]
# THIS WORKS but takes a really long time on my large dataset:
otherus <- merge(d1, us, by='s')
Merge 适用于我的目的,但由于我的 'd1' >>> 'us',它需要很长时间。起初我想也许我正在使用来自基础的合并,但根据文档,它看起来像 data.table 合并被调度是类(first_arg to merge)是一个data.table。
我仍然习惯于 data.table J() 语法。有没有更好的方法来实现这一点?
提前致谢。
【问题讨论】:
-
哎呀,这些名字令人困惑。
us不应该指代多个u,而不是与特定u关联的多个s?无论如何,这对我来说听起来像是图论。你在找邻居,对吧?如果是这样,您可能需要查看igraph包。
标签: r merge data.table lookup