R(正则表达式)中的管道操作中不包含单词的子集向量
Subset vector not containing word in piped operation in R (regex)
如何在管道操作中为不包含单词的元素设置向量子集? (我真的很喜欢滚边)
我希望有一些方法可以反转 str_subset
。在下面的示例中,我只想 return x
的第二个元素,而不是 hi
中的元素:
library(stringr)
x <- c("hi", "bye", "hip")
x %>%
str_dup(2) %>% # just an example operation
str_subset("hi") # I want to return the inverse of this
您可以使用 ^(?!.*hi)
断言字符串不包含 hi
;正则表达式使用否定前瞻 ?!
并断言字符串不包含模式 .*hi
:
x %>%
str_dup(2) %>% # just an example operation
str_subset("^(?!.*hi)")
# [1] "byebye"
或反向过滤str_detect
:
x %>%
str_dup(2) %>% # just an example operation
{.[!str_detect(., "hi")]}
# [1] "byebye"
如何在管道操作中为不包含单词的元素设置向量子集? (我真的很喜欢滚边)
我希望有一些方法可以反转 str_subset
。在下面的示例中,我只想 return x
的第二个元素,而不是 hi
中的元素:
library(stringr)
x <- c("hi", "bye", "hip")
x %>%
str_dup(2) %>% # just an example operation
str_subset("hi") # I want to return the inverse of this
您可以使用 ^(?!.*hi)
断言字符串不包含 hi
;正则表达式使用否定前瞻 ?!
并断言字符串不包含模式 .*hi
:
x %>%
str_dup(2) %>% # just an example operation
str_subset("^(?!.*hi)")
# [1] "byebye"
或反向过滤str_detect
:
x %>%
str_dup(2) %>% # just an example operation
{.[!str_detect(., "hi")]}
# [1] "byebye"