Reputation: 53
I'm trying to remove strings that contain a specific character pattern. My data looks somethink like this:
places <- c("copenhagen", "copenhagens", "Berlin", "Hamburg")
I would like to remove all elements that contain "copenhagen", i.e. "copenhagen"
and "copenhagens"
.
But I was only able to come up with the following code:
library(stringr)
replacement.vector <- c("copenhagen", "copenhagens")
for(i in 1:length(replacement.vector)){
places = lapply(places, FUN=function(x)
gsub(paste0("\\b",replacement.vector[i],"\\b"), "", x))
I'm looking fo a function that enables me to remove all elements that contain "copenhagen" without having to specify whether or not the element also includes other letters.
Best, Dose
Upvotes: 3
Views: 4293
Reputation: 887971
Based on the OP's code, it seems like we need to subset the 'places'. In that case, it may be better to use grep
with invert= TRUE
argument
grep("copenhagen", places, invert=TRUE, value = TRUE)
#[1] "Berlin" "Hamburg"
or use grepl
and negate (!
)
places[!grepl("copenhagen", places)]
#[1] "Berlin" "Hamburg"
Upvotes: 5