R dplyr:通过向量定义的多个正则表达式过滤数据

may*_*cca 3 regex r dplyr

我有一个数据框,我想从中选择重要的列,然后过滤行以包含特定的结尾。

正则表达式使使用符号定义我的结束值变得简单xx$。但是,如何改变多种可能的结局(xx$, yy$)?

虚拟示例:

require(dplyr)

x <- c("aa", "aa", "aa", "bb", "cc", "cc", "cc")
y <- c(101, 102, 113, 201, 202, 344, 407)
type = rep("zz", 7)
df = data.frame(x, y, type)    

# Select all expressions that starts end by "7"
df %>%
  select(x, y) %>%
  filter(grepl("7$", y))

# It seems working when I explicitly define my variables, but I need to use it as a vector instead of values?
df %>%
  select(x, y) %>%
  filter(grepl("[2|7]$", y))  # need to modify this using multiple endings


# How to modify this expression, to use vector of endings (ids) instead?
ids = c(7,2)     # define vector of my values

df %>%
     select(x, y) %>%
     filter(grepl("ids$", y))  # how to change "grepl(ids, y)??"
Run Code Online (Sandbox Code Playgroud)

预期输出:

   x   y type
1 aa 102   zz
2 cc 202   zz
3 cc 407   zz
Run Code Online (Sandbox Code Playgroud)

基于此问题的示例:正则表达式 (RegEx) 和 dplyr::filter()

Wik*_*żew 5

您可以使用

df %>% 
  select(x, y) %> filter(grepl(paste0("(?:", paste(ids, collapse="|"), ")$"), y))
Run Code Online (Sandbox Code Playgroud)

该paste0("(?:", paste(ids, collapse="|"), ")$")部件将构建一个交替模式,由于末端的锚点,该模式仅在字符串的末端匹配$。

注意:如果值可以具有特殊的正则表达式元字符,您需要首先转义字符向量中的值:

regex.escape <- function(string) {
  gsub("([][{}()+*^$|\\\\?.])", "\\\\\\1", string)
}
df %>% 
      select(x, y) %> filter(grepl(paste0("(?:", paste(regex.escape(ids), collapse="|"), ")$"), y))
                                                       ^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

例如,paste0("(?:", paste(c("7", "8", "ids"), collapse="|"), ")$")将输出 (?:7|8|ids)$:

  • (?:- 非捕获组的开始,该组将充当替代项的容器,以便$锚点应用于所有这些项,而不仅仅是最后一个,匹配其中的任何一个
    • 7- 一个7字符
  • |- 或者
  • 8- 一个8字符
  • |- 或者
  • ids- 一个子ids串
  • )- 组结束
  • $- 字符串的末尾。