注意: 这是我最初发布到data.table帮助组的一个问题。Matt Dowle要求提供更详细的示例,我发布了这个示例,但在电子邮件中遇到了格式问题。我已经知道如何在SO上进行格式化,所以我想在这里发布它。
我基本上正在尝试根据行中的值以及上一行或下一行中的值来对data.table进行子集处理。目前,我正在为未来和过去的行创建新列,然后将数据表键掉这些列,但这会耗费资源并且繁琐。
下面的示例说明了我现在使用的方法。该示例使用文档中的单词(我为两者都使用数字索引)。我想对特定单词进行子集处理,但仅当它前面或后面跟随另一个单词或一组单词时:
我首先创建一个虚拟数据集,其中包含有一百万个单词的十个文档。该集合中有三个唯一的单词。
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
考虑到位置因素后,情况会变得更加复杂。这是一个查询,用于计算所有文档中第一个唯一单词的出现次数,除非它后面跟着第二个唯一单词。首先,我创建一个包含下一个单词的新列,然后使用这两个单词进行关键字匹配。
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
然而,由于数据量庞大,目前我不得不使用笨拙的过滤和连接方式。
所以问题是,有没有更简单、更美观的方法?
更新:
感谢Arun和eddi提供了干净简单的代码来解决这个问题。在我的约2亿行数据中,这个解决方案可以在大约10秒内完成简单词语组合的查找,效果相当不错!
然而,我还有一个额外的问题,使向量扫描方法不太理想。尽管在这个例子中,我只寻找一个词语组合,但在实践中,我可能需要在每个位置上查找一组词语。当我将“ == ”语句更改为“%in%”以实现这个目的(使用100个或更多单词的向量),查询所需时间会更长。因此,如果存在二进制搜索解决方案,我仍然对其感兴趣。但是,如果Arun不知道是否存在这种方式,那么可能就没有了,我很乐意接受他的答案。
%chin%
比%in%
更快。 - Arun