【问题标题】:Create new column to show partial matches across strings in dplyr创建新列以显示 dplyr 中字符串之间的部分匹配
【发布时间】:2018-08-13 11:44:57
【问题描述】:

我正在尝试创建一个新列,以显示我的数据框中两列中的字符串之间是否存在任何匹配项。 This question 几乎是我要问的,但不是过滤,我想创建一个新列来显示是否存在匹配项(TRUE 或 FALSE)。

这是一个示例数据框:

 transcript        target
 he saw the dog    saw
 she gave them it  gave
 watch out for     danger
 real bravery      brave

我想创建一个新列来显示两者之间的任何匹配:

 transcript        target    match
 he saw the dog    saw        T
 she gave them it  gave       T
 watch out for     danger     F
 real bravery      brave      T

我更喜欢使用 dplyr() 但我愿意接受其他建议!

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    使用stringr::str_detect 我们可以检查transcript 是否包含target

    library(stringr)
    library(dplyr)
    df %>% mutate_if(is.factor, as.character) %>%    #If transcript and target are character class  in your df then no need to this step
           mutate(match = str_detect(transcript,target))
    
    
             transcript target match
    1   he saw the dog    saw  TRUE
    2 she gave them it   gave  TRUE
    3    watch out for danger FALSE
    4     real bravery  brave  TRUE
    

    【讨论】:

    • 嗨 Suliman,如何使用 str_detect 为新列提供值?示例:如果 str_detect 在现有列中找到一个单词,则创建一个新列并将该行标记为“找到”。谢谢。
    • 嗨 @AdilK,str_detect 返回 T/F,因此我们可以像往常一样使用 ifelse,例如df %>% mutate(newcol = ifelse(str_detect(transcript,target), "found", "not found"))
    • 谢谢,苏利曼。我在这里问了这个问题,并得到了关于使用 grep1 的答案,它在我的数据集中有效。是否有 grep1 函数的 dplyr 版本? stackoverflow.com/questions/61505957/…
    【解决方案2】:

    您要求使用 dplyr 方法,但这里也是使用 grepl 的基本 R 方法:

    df1$match <- mapply(grepl, df1$target, df1$transcript)
    
    df1
            transcript target match
    1   he saw the dog    saw  TRUE
    2 she gave them it   gave  TRUE
    3    watch out for danger FALSE
    4     real bravery  brave  TRUE
    

    在 dplyr mutate 语句中使用 grepl

    df1 %>% 
      mutate(match = mapply(grepl, target, transcript))
    
            transcript target match
    1   he saw the dog    saw  TRUE
    2 she gave them it   gave  TRUE
    3    watch out for danger FALSE
    4     real bravery  brave  TRUE
    

    【讨论】:

      【解决方案3】:

      一个选项可以是使用dplyr::rowwise()grepl 来创建匹配列:

      library(dplyr)
      
      df %>% rowwise() %>%
        mutate(match  = grepl(target,transcript)) %>%
        as.data.frame()
      
      #         transcript target match
      # 1   he saw the dog    saw  TRUE
      # 2 she gave them it   gave  TRUE
      # 3    watch out for danger FALSE
      # 4     real bravery  brave  TRUE
      

      数据:

      df <- read.table(text = 
      "transcript        target
      'he saw the dog'    saw
      'she gave them it'  gave
      'watch out for'     danger
      'real bravery'      brave",
      header = TRUE, stringsAsFactors = FALSE)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多