注意:这是我最初发布到data.table帮助组的一个问题。马特·道尔(Matt Dowle)要求提供一个更详细的示例,我发布了这个示例,但是我在电子邮件格式方面遇到了麻烦。我已经知道如何在SO上格式化,所以我想我可以将其发布在这里。

我基本上想做的是基于该行中的值以及前一行或后一行中的值从data.table中获取子集行。现在,我正在为将来的行和过去的行创建新列,然后在这些列上键入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提供的简洁代码来解决问题。在我约200M的行数据上,此解决方案仅需约10秒即可对单词进行简单组合,效果非常好!

但是,我确实有一个额外的问题,这使矢量扫描方法变得不太理想。尽管在该示例中,我仅查找一个单词组合,但实际上,我可能在每个位置都有一个单词矢量来寻找。为此,当我将“ ==”语句更改为“%in%”(向量为100个单词或更多)时,查询将花费更长的时间才能运行。因此,如果有的话,我仍然会对二进制搜索解决方案感兴趣。但是,如果阿伦一无所知,那可能就不知道了,我很乐意接受他的回答。

最佳答案

这是我突然想到的另一个想法。它只需要再创建一列,并对子集使用二进制搜索。

在根据数据生成的DT上,首先我们将添加额外的列:

# the extra column:
DT[, I := .I]


我们需要这个,因为我们将在setkeydocindexwordindex。这是我们可以创建子集而不创建额外列的唯一方法(至少我能想到的)。因此,我们需要一种稍后提取“原始”位置的方法来检查您的状况(因此I)。

添加额外的列后,让我们在上面提到的两列上设置键:

setkey(DT, docindex, wordindex)


大!这里的想法是提取所需单词匹配的位置-这里的值是1L。然后,提取您可能(或可能不希望)在此字词之后正确位置出现的所有其他字词。然后,我们只需保留(或删除)满足条件的那些索引。

这是一个可以解决这个问题的函数。这绝不是完整的,但是应该给您一个想法。

foo <- function(DT, doc_key, word_key, rest_key=NULL, match=FALSE) {
    ## note that I'm using 1.9.3, where this results in a vector
    ## if you're using 1.9.2, you'll have to change the joins accordingly
    idx1 = DT[J(doc_key, word_key), I]
    for (i in seq_along(rest_key)) {
        this_key = rest_key[i]
        idx2 = DT[J(doc_key, this_key), I]
        if (match) idx1 = idx1[which((idx1+i) %in% idx2)]
        else idx1 = idx1[which(!(idx1+i) %in% idx2)]
    }
    DT[idx1, .N, by=c(key(DT)[1L])]
}


在这里,DT是已添加data.table列的I,然后如前所述,已在两列上调用setkey

doc_key基本上包含docindex中的所有唯一值-这里是1:10。 word_key基本上是1升。 rest_key是您要检查的值,它不在i的位置后的word_key位置。

首先,我们为I中的1L的所有匹配项提取idx1(直接)。接下来,我们遍历rest_key的每个值,并将该位置添加到idx1 = idx1+i,并检查该值是否出现在idx2中。如果是这样,则根据您是要提取匹配项还是不匹配项,我们将保留(或删除它们)。

并且在此循环结束时,idx1应该仅具有所需的条目。希望这可以帮助。下面显示的是另一个答案中已经讨论过的案例的演示。



让我们考虑您的第一种情况。对于docindex中第i个位置为1Li+1 th不是2L的每个组的所有条目的计数。这基本上是:

system.time(ans1 <- foo(DT, 1:10, 1L, 2L, FALSE))

#  user  system elapsed
# 0.066   0.019   0.085

# old method took 0.12 seconds

#     docindex     N
#  1:        1 22301
#  2:        2 21836
#  3:        3 22491
#  4:        4 21831
#  5:        5 22218
#  6:        6 21914
#  7:        7 22370
#  8:        8 22265
#  9:        9 22211
# 10:       10 22190




那第二种情况呢?在这里,我们希望第i+1i+2位置分别为2L和3L,这与之前情况下的不相等情况相反。因此,我们在此处设置match=TRUE

system.time(ans2 <- foo(DT, 1:10, 1L, 2:3,TRUE))
#  user  system elapsed
# 0.080   0.011   0.090

# old method took 0.22 seconds

#     docindex    N
#  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


扩展此功能很容易。例如:如果您想让i+1等于2L,但i+2 th不等于3L,则可以将match更改为vector = length(rest_key)并指定逻辑值。

我希望这对于您的实际情况是很快的-至少比其他版本要快。

高温超导

07-24 09:51
查看更多