FDose FDose - 3 months ago 8
R Question

R: How to remove a string containing a specific character pattern?

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

Answer

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"