【问题标题】:Compare two columns of strings for row-wise text match比较两列字符串以进行逐行文本匹配
【发布时间】:2017-04-11 12:33:46
【问题描述】:

在单个数据集 (QueryTM) 中,我有两列 Query 和 TM。我想检查查询是否包含 TM 的值(在同一行中)。举个例子,如果 TM 是“Coca Cola”,Query 是“Coca Cola India”,Query 应该与 TM 匹配。但是,如果查询是“Coca Colala India”,它不应该匹配。结果将存储在另一列中,例如 Result

我使用 R 作为平台。

【问题讨论】:

    标签: r text match


    【解决方案1】:

    您需要添加单词边界以捕获精确匹配。使用mapply 你可以做到,

    dd$result <- mapply(grepl, paste0('\\b', dd$TM, '\\b'), dd$Query)
    
    dd
    #            TM              Query result
    #1    Coca Cola  Coca Colala India  FALSE
    #2 Fanta Orange Fanta Orange India   TRUE
    

    数据

    dput(dd)
    structure(list(TM = c("Coca Cola", "Fanta Orange"), Query = c("Coca Colala India", 
    "Fanta Orange India")), .Names = c("TM", "Query"), row.names = c(NA, 
    -2L), class = "data.frame")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2021-03-13
      相关资源
      最近更新 更多