【问题标题】:Filter rows based on dynamic pattern根据动态模式过滤行
【发布时间】:2021-06-16 13:11:18
【问题描述】:

我在数据框dfin 列Orthographic 中有语音数据:

df <- data.frame(
  Orthographic = c("this is it at least probably",
                   "well not probably it's not intuitive",
                   "sure no it's I mean it's very intuitive",
                   "I don't mean to be rude but it's anything but you know",
                   "well okay maybe"),
  Repeat = c(NA, "probably", "it's,intuitive", "I,mean,it's", NA),
  Repeat_pattern = c(NA, "\\b(probably)\\b", "\\b(it's|intuitive)\\b", "\\b(I,mean|it's)\\b", 
                     NA))

我想filter基于一个动态模式的行,即nonevernot作为单词的出现或n't之前 Repeat 列中列出的任何单词。但是,使用模式\\b(no|never|not)\\b|n't\\b\\s Repeat_pattern 中的交替模式,我得到这个错误:

df %>%
   filter(grepl(paste0("\\b(no|never|not)\\b|n't\\b\\s", Repeat_pattern), Orthographic))
                             Orthographic         Repeat         Repeat_pattern
1    well not probably it's not intuitive       probably       \\b(probably)\\b
2 sure no it's I mean it's very intuitive it's,intuitive \\b(it's|intuitive)\\b
Warning message:
In grepl(paste0("\\b(no|never|not)\\b|n't\\b\\s", Repeat_pattern),  :
  argument 'pattern' has length > 1 and only the first element will be used

我不知道为什么“只使用第一个元素”,因为两个模式组件似乎连接得很好:

paste0("\\b(no|never|not)\\b|n't\\b\\s", df$Repeat_pattern)
[1] "\\b(no|never|not)\\b|n't\\b\\sNA"                     "\\b(no|never|not)\\b|n't\\b\\s\\b(probably)\\b"      
[3] "\\b(no|never|not)\\b|n't\\b\\s\\b(it's|intuitive)\\b" "\\b(no|never|not)\\b|n't\\b\\s\\b(I,mean|it's)\\b"   
[5] "\\b(no|never|not)\\b|n't\\b\\sNA"

预期的输出是这样的:

2                   well not probably it's not intuitive       probably       \\b(probably)\\b
3                sure no it's I mean it's very intuitive it's,intuitive \\b(it's|intuitive)\\b
4 I don't mean to be rude but it's anything but you know    I,mean,it's    \\b(I,mean|it's)\\b

【问题讨论】:

    标签: r regex dplyr


    【解决方案1】:

    这里看起来像是矢量化问题,您需要在这里使用stringr::str_detect 而不是grepl

    另外,您没有很好地对否定词替代品进行分组,所有这些都必须位于一个组中,并且您的 n't 现在是字符串中的必填项。

    另外,NA 值被强制转换为文本并添加到正则表达式模式中,而您似乎想要丢弃 Repeat_patternNA 的项目。

    您可以通过使用来修复您的代码

    df %>%
        filter(ifelse(is.na(Repeat_pattern), FALSE, str_detect(Orthographic, paste0("(?:\\bno|\\bnever|\\bnot|n't)\\b.*", Repeat_pattern))))
    

    输出:

                                                Orthographic         Repeat         Repeat_pattern
    1                   well not probably it's not intuitive       probably       \\b(probably)\\b
    2                sure no it's I mean it's very intuitive it's,intuitive \\b(it's|intuitive)\\b
    3 I don't mean to be rude but it's anything but you know    I,mean,it's    \\b(I|mean|it's)\\b
    

    我也认为最后一个模式一定是\\b(I|mean|it's)\\b,而不是\\b(I,mean|it's)\\b

    如果“否”字与Repeat 列中的字之间只能有空格,请在我的模式中将.* 替换为\\s+。我使用.*\b 来确保在“否”字右侧的任何地方都有匹配项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 2023-04-06
      相关资源
      最近更新 更多