【问题标题】:How to count the number multiple strings appear in another string list by group?如何按组计算多个字符串出现在另一个字符串列表中的数量?
【发布时间】:2022-01-02 02:23:32
【问题描述】:

现在我有两个数据,nametext,我想计算name中每个名字出现的次数text中的当前年份,即生成数据result。如何做到这一点?我尝试了 lapplygrepl,但都失败了。非常感谢!

name=data.table(year=c(2018,2019,2020),
                  name0=list(c("A","B","C"),c("B","C"),c("D","E","F")))
text=data.table(year=c(2018,2018,2019,2019,2020),
                text0=list(c("DEF","BG","CG"),c("ART","CWW"),c("DLK","BU","FO"),
                           c("A45","11B","C23"),c("EIU","CM")))
result=data.table(year=c(2018,2018,2018,2019,2019,2020,2020,2020),
                 name0=c("A","B","C","B","C","D","E","F"),
                 count=c(1,1,2,2,1,0,1,0))

【问题讨论】:

    标签: r data.table lapply stringr grepl


    【解决方案1】:

    可以合并未列出的值:

    library(data.table)
    merge(
      name[, .(name0 = unlist(name0)), by = .(year)],
      text[, .(name0 = unlist(strsplit(unlist(text0), ""))), by=.(year)][, ign := 1],
      by = c("year", "name0"), all.x = TRUE, allow.cartesian = TRUE
    )[,.(count = sum(!is.na(ign))), by = .(year, name0)]
    #     year  name0 count
    #    <num> <char> <int>
    # 1:  2018      A     1
    # 2:  2018      B     1
    # 3:  2018      C     2
    # 4:  2019      B     2
    # 5:  2019      C     1
    # 6:  2020      D     0
    # 7:  2020      E     1
    # 8:  2020      F     0
    

    ign 变量是为了让我们可以强制使用all.x=TRUE,但要考虑那些在y 中找不到的内容。


    较慢但可能更节省内存的方法:

    namelong <- name[, .(name0 = unlist(name0)), by = .(year)]
    namelong
    #     year  name0
    #    <num> <char>
    # 1:  2018      A
    # 2:  2018      B
    # 3:  2018      C
    # 4:  2019      B
    # 5:  2019      C
    # 6:  2020      D
    # 7:  2020      E
    # 8:  2020      F
    
    func <- function(yr, nm) text[year == yr, sum(grepl(nm, unlist(text0)))]
    namelong[, count := do.call(mapply, c(list(FUN=func), unname(namelong)))]
    #     year  name0 count
    #    <num> <char> <int>
    # 1:  2018      A     1
    # 2:  2018      B     1
    # 3:  2018      C     2
    # 4:  2019      B     2
    # 5:  2019      C     1
    # 6:  2020      D     0
    # 7:  2020      E     1
    # 8:  2020      F     0
    

    【讨论】:

    • 谢谢。我试过你的代码,例子中没有问题,但是我的实际数据很大,text的行数超过1亿,所以R报Error: memory exhausted (limited?)。你知道如何解决这个问题吗?
    • 看看我的编辑是否提供了一种方法。它会变慢(它与for 循环相差不远),但扩展到内存的程度较小。
    • 非常感谢!问题解决了。
    猜你喜欢
    • 1970-01-01
    • 2011-07-01
    • 2014-11-14
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多