首页 > 解决方案 > 在 dplyr() 中使用 str_detect 跨两列进行匹配

问题描述

我正在尝试跨两列进行匹配mydata以创建一个名为match. 我首先要删除在Word1or中包含“none”的所有行Word2

 Token     Word1     Word2
 1         cat       cat
 2         dog       cat
 3         hamster   none
 4         sheep     sheep
 5         none      hamster

我在中使用了以下代码dplyr()

 mydata %>% filter(Word1 != "none" | Word2 != "none") %>%
   mutate(match = str_detect(Word1 , Word2)) %>%
   filter(match == FALSE)

我收到以下错误:

 Error in mutate_impl(.data, dots) : 
   Evaluation error: Incorrectly nested parentheses in regexp pattern. 
 (U_REGEX_MISMATCHED_PAREN).

当我mydata %>% filter(Word1 != "none" | Word2 != "none")只运行该函数时,它工作正常,所以我看不到括号中的错误。但也许我在这里遗漏了一些非常明显的东西!(我之前已经str_detect成功使用过很多次了!)

我也找到了这个关于使用的答案fixed(x),但是当我将它应用到我的代码中时,mutate(match = str_detect(Word1 , Word2), fixed(x))我在输出中得到了同样的错误。

标签: rregexdplyr

解决方案


推荐阅读