Note:这是我最初发布到 data.table 帮助组的一个问题。马特·道尔(Matt Dowle)要求提供更详细的示例,我发布了这个示例,但我在电子邮件格式方面遇到了麻烦。我已经知道如何格式化 SO,所以我想我应该将其发布在这里。
我基本上想做的是根据该行中的值从 data.table 中提取行也前一行或后一行中的值。现在,我正在为未来和过去的行创建新列,然后在这些列上键入 data.table,但这是资源密集型且繁重的。
下面的例子说明了我现在使用的方法。该示例使用文档中的单词(我对两者都使用数字索引)。我想对特定单词进行子集化,但前提是它前面或后面有另一个单词或一组单词:
我首先创建一个虚拟数据集,其中包含 10 个包含 100 万个单词的文档。该集中有三个独特的单词。
library(data.table)
set.seed(1000)
DT<-data.table(wordindex=sample(1:3,1000000,replace=T),docindex=sample(1:10,1000000,replace=T))
setkey(DT,docindex)
DT[,position:=seq.int(1:.N),by=docindex]
wordindex docindex position
1: 1 1 1
2: 1 1 2
3: 3 1 3
4: 3 1 4
5: 1 1 5
---
999996: 2 10 99811
999997: 2 10 99812
999998: 3 10 99813
999999: 1 10 99814
1000000: 3 10 99815
请注意,简单地计算所有文档中第一个唯一单词的出现次数既简单又美观。
setkey(DT,wordindex)
count<-DT[J(1),list(count.1=.N),by=docindex]
count
docindex count.1
1: 1 33533
2: 2 33067
3: 3 33538
4: 4 33053
5: 5 33231
6: 6 33002
7: 7 33369
8: 8 33353
9: 9 33485
10: 10 33225
当考虑到前面的位置时,情况会变得更加混乱。这是一个计算所有文档中第一个唯一单词出现次数的查询unless接下来是第二个独特的词。首先,我创建一个新列,其中包含前面一个位置的单词,然后键入这两个单词。
setkey(DT,docindex,position)
DT[,lead_wordindex:=DT[list(docindex,position+1)][,wordindex]]
wordindex docindex position lead_wordindex
1: 1 1 1 1
2: 1 1 2 3
3: 3 1 3 3
4: 3 1 4 1
5: 1 1 5 2
---
999996: 2 10 99811 2
999997: 2 10 99812 3
999998: 3 10 99813 1
999999: 1 10 99814 3
1000000: 3 10 99815 NA
setkey(DT,wordindex,lead_wordindex)
countr2<-DT[J(c(1,1),c(1,3)),list(count.1=.N),by=docindex]
countr2
docindex count.1
1: 1 22301
2: 2 21835
3: 3 22490
4: 4 21830
5: 5 22218
6: 6 21914
7: 7 22370
8: 8 22265
9: 9 22211
10: 10 22190
我有一个非常大的数据集,上述查询的内存分配失败。作为替代方案,我们可以通过过滤原始数据集然后将其连接回所需位置,仅为相关数据子集创建此新列:
setkey(DT,wordindex)
filter<-DT[J(1),list(wordindex,docindex,position)]
filter[,lead_position:=position+1]
wordindex wordindex docindex position lead_position
1: 1 1 2 99717 99718
2: 1 1 3 99807 99808
3: 1 1 4 100243 100244
4: 1 1 1 1 2
5: 1 1 1 42 43
---
332852: 1 1 10 99785 99786
332853: 1 1 10 99787 99788
332854: 1 1 10 99798 99799
332855: 1 1 10 99804 99805
332856: 1 1 10 99814 99815
setkey(DT,docindex,position)
filter[,lead_wordindex:=DT[J(filter[,list(docindex,lead_position)])][,wordindex]]
wordindex wordindex docindex position lead_position lead_wordindex
1: 1 1 2 99717 99718 NA
2: 1 1 3 99807 99808 NA
3: 1 1 4 100243 100244 NA
4: 1 1 1 1 2 1
5: 1 1 1 42 43 1
---
332852: 1 1 10 99785 99786 3
332853: 1 1 10 99787 99788 3
332854: 1 1 10 99798 99799 3
332855: 1 1 10 99804 99805 3
332856: 1 1 10 99814 99815 3
setkey(filter,wordindex,lead_wordindex)
countr2.1<-filter[J(c(1,1),c(1,3)),list(count.1=.N),by=docindex]
countr2.1
docindex count.1
1: 1 22301
2: 2 21835
3: 3 22490
4: 4 21830
5: 5 22218
6: 6 21914
7: 7 22370
8: 8 22265
9: 9 22211
10: 10 22190
我觉得很丑。此外,我可能想要查看多个单词,因此需要创建另一栏。简单但昂贵的方法是:
setkey(DT,docindex,position)
DT[,lead_lead_wordindex:=DT[list(docindex,position+2)][,wordindex]]
wordindex docindex position lead_wordindex lead_lead_wordindex
1: 1 1 1 1 3
2: 1 1 2 3 3
3: 3 1 3 3 1
4: 3 1 4 1 2
5: 1 1 5 2 3
---
999996: 2 10 99811 2 3
999997: 2 10 99812 3 1
999998: 3 10 99813 1 3
999999: 1 10 99814 3 NA
1000000: 3 10 99815 NA NA
setkey(DT,wordindex,lead_wordindex,lead_lead_wordindex)
countr23<-DT[J(1,2,3),list(count.1=.N),by=docindex]
countr23
docindex count.1
1: 1 3684
2: 2 3746
3: 3 3717
4: 4 3727
5: 5 3700
6: 6 3779
7: 7 3702
8: 8 3756
9: 9 3702
10: 10 3744
然而,由于尺寸的原因,我目前不得不使用丑陋的过滤和连接方式。
那么问题来了,有没有更简单、更美观的方式呢?
UPDATE:
感谢 Arun 和 eddi 提供了解决问题的干净简单的代码。在我的约 200M 行数据上,这个解决方案可以在大约 10 秒内处理简单的单词组合,这非常好!
然而,我确实有一个额外的问题,这使得矢量扫描方法不太理想。尽管在示例中我只查找一个单词组合,但实际上我可能在每个位置都有一个单词向量要查找。当我出于此目的将“==”语句更改为“%in%”(100 个或更多单词的向量)时,查询需要更长的时间才能运行。因此,如果存在的话,我仍然对二分搜索解决方案感兴趣。然而,如果阿伦不知道,那也可能不知道,我很乐意接受他的回答。