Take a list of strings:
strings <- c("ABC_XZY", "qwe_xyz", "XYZ")
I'd like to get all elements in strings that don't contain a specific substring
avoid <- c("ABC")
I can do this
library(stringr)
library(dplyr)
library(purrr)
strings %>%
.[!map_lgl(., str_detect, avoid)]
[1] "qwe_xyz" "XYZ"
What I'd like to do though is specify several substrings
avoid_2 <- c("ABC", "qwe")
And then map over the list as before (doesn't work)
strings %>%
.[!map_lgl(., str_detect, avoid_2)]
Error: Result 1 must be a single logical, not a logical vector of length 2
What I want is
[1] "XYZ"
The error is clear - each element of string is generating a logical for each element of avoid_2, for total of 2 logical/element and map_lgl can only handle one/element.
I can of course do each substring separately, but I don't want to - I want to make a list of substrings
don't want, but does work
strings %>%
.[!map_lgl(., str_detect, "ABC")] %>%
.[!map_lgl(., str_detect, "qwe")]