【问题标题】:Quanteda kwic append data to outputQuanteda kwic 将数据附加到输出
【发布时间】:2017-01-20 08:03:17
【问题描述】:

我想将一些元数据附加到 kwic 输出中,例如客户 ID(见下文),以便轻松查找主文件。我尝试使用 cbind 附加数据,但没有正确匹配。

如果这是可能的例子将不胜感激。

     docname    position    contextPre      keyword    contextPost          CustID
     text3790     5    nothing at all looks  good   and sounds great           1
     text3801    11    think the offer is a  good   value and has a lot        3
     text3874    10    not so sure thats a   good   word to use                5

原始数据帧

       CustID   Comment
         1      nothing at all looks good and sounds great
         2      did not see anything that was very appealing
         3      I think the offer is a good value and has a lot of potential
         4      these items look terrible how are you still in business
         5      not so sure thats a good word to use
         6      having a hard time believing some place would sell an item so low
         7      it may be worth investing in some additional equipment

【问题讨论】:

    标签: r


    【解决方案1】:

    起初我认为理想的解决方案是使用docvars,但kwic 似乎没有显示它们的选项。我仍然需要将 id-doc 映射表与 kwic 结果合并。

    library(data.table)
    library(quanteda)
    
    s <- "CustID,   Comment
    1,      nothing at all looks good and sounds great
    2,      did not see anything that was very appealing
    3,      I think the offer is a good value and has a lot of potential
    4,      these items look terrible how are you still in business
    5,      not so sure thats a good word to use
    6,      having a hard time believing some place would sell an item so low
    7,      it may be worth investing in some additional equipment"
    
    # I'm using data.table mainly to read the data easily. 
    dt <- fread(s, data.table=FALSE)
    
    # all operations below apply to data frame
    myCorpus <- corpus(df$Comment)
    # the Corpus and CustID came from same data frame, 
    # thus ensured the mapping is correct
    docvars(myCorpus, "CustID") <- df$CustID
    summary(myCorpus)
    # build the mapping table of docname and CustID. 
    # The docname is in row.names, have to make an explicit column
    dv_table <- docvars(myCorpus)
    id_table <- data.frame(docname = row.names(dv_table), CustID = dv_table$CustID)
    result <- kwic(myCorpus, "good", window = 3, valuetype = "glob")
    id_result <- merge(result, id_table, by = "docname")
    

    结果:

    > id_result
      docname position   contextPre keyword      contextPost CustID
    1   text1        5 at all looks    good and sounds great      1
    2   text3        7   offer is a    good value and has         3
    3   text5        6 sure thats a    good word to use           5
    

    【讨论】:

    • 谢谢!我慢慢意识到需要某种类型的合并或连接。如果您还可以将原始数据框中的其他列作为输出的一部分包括在内,这将是 KWIC 中的一个不错的功能。干杯。
    • KWIC 可能不会知道原始数据框中的其他列,因为它正在处理语料库,而语料库旨在从其他类型的数据而不是数据帧中读取。我们可以在docvar添加列,如果作者愿意,可以通过KWIC访问。虽然包作者似乎只是将它们用于情节或语料库的报告。
    • 感谢@Pierre Lafortune,以前不知道data.table=FALSE 选项!
    【解决方案2】:

    它是一个data.frame 对象,因此您可以按常规方式添加列:

    library(quanteda)
    h <- head(kwic(inaugTexts, "secure*", window = 3, valuetype = "glob"))
    
    #Add new ID column
    h$CustID <- 1:nrow(h)
    

    【讨论】:

    • 感谢您的反馈。添加新列有效,但最终我将正确的客户 ID 分配给他们的报表。
    • 你也可以这样做。为什么没有在问题中提供 id 映射?
    • 这就是我要问的,如何将这些拼图拼在一起。
    • 您的客户 ID 现在在哪里?
    • 得到你...data.frame
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    相关资源
    最近更新 更多