Subset vector not containing word in piped operation in R (regex)
Asked Answered
S

1

7

How do I subset a vector for elements that do not contain a word in a piped operation? (I'm really into piping)

I'm hoping there's some way to invert str_subset. In the following example, I'd like to just return the second element of x instead of the elements with hi in them:

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
Sung answered 21/4, 2018 at 17:42 Comment(1)
In package stringi there is stri_subset_fixed("hi", negate = TRUE). It works, I have just tried it.Elexa
A
10

You can use ^(?!.*hi) to assert string not contain hi; The regex uses negative look ahead ?! and asserts the string doesn't contain a pattern .*hi:

x %>% 
    str_dup(2) %>%  # just an example operation
    str_subset("^(?!.*hi)")  
# [1] "byebye"

Or filter by reversing str_detect:

x %>% 
    str_dup(2) %>%  # just an example operation
    {.[!str_detect(., "hi")]}  
# [1] "byebye"
Agnes answered 21/4, 2018 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.