str_detect 删除部分但不是所有具有指定结尾的字符串

str_detect removing some but not all strings with specified ending

我想删除任何以竖线中的 2 个字符中的任何一个结尾的字符串。在这个例子中它是“.o”或“.t”。其中一些被删除,但不是全部,我不明白为什么。我怀疑 'pattern = ' 参数有问题。

ex1 <- structure(list(variables = structure(1:18, .Label = c("canopy15", 
"canopy16", "DistanceToRoad", "DistanceToEdge", "EdgeDistance", 
"TrailDistance", "CARCOR.o", "EUOALA.o", "FAGGRA.o", "LINBEN.o", 
"MALSP..o", "PRUSER.o", "ROSMUL.o", "RUBPHO.o", "VIBDEN.o", "ACERUB.t", 
"FAGGRA.t", "NYSSYL.t"), class = "factor")), row.names = c(NA, 
-18L), class = "data.frame")

ex1 %>%
dplyr::filter(stringr::str_detect(string = variables,
pattern = c("\.o$", "\.t$"),
negate = TRUE))

##output
# variables
# 1        canopy15
# 2        canopy16
# 3  DistanceToRoad
# 4  DistanceToEdge
# 5    EdgeDistance
# 6   TrailDistance
# 7        EUOALA.o
# 8        LINBEN.o
# 9        PRUSER.o
# 10       RUBPHO.o
# 11       FAGGRA.t

pattern有多个元素,所以是循环,因此检查o$一行,然后t$检查下一行,依此类推。尝试改为:

ex1 %>%
  dplyr::filter(stringr::str_detect(string = variables,
                                    pattern = c("\.(o|t)$"),
                                    negate = TRUE))

对于那些不像 well-versed 正则表达式的人,这里有一个更简单的答案。

library(tidyverse)
ex1 %>% filter(str_detect(string = variables, pattern = ".t$", negate = TRUE),
               str_detect(string = variables, pattern = ".o$", negate = TRUE))