I would like to replace/remove those parts of a string (name) that match to other columns (stateand city) in my data table.
I managed to identify the rows, e.g. with city, like so:
dt%>% filter(str_detect(name, city)) but I am missing a way to use gsub (or grep) with the rowwise value of the column city.
I know that a rather manual approach like storing all city names in a vector and enter them in gsub would work but it would also falsely remove the "dallas" of row 2. (This was manageable for states though and could be combined with gsub to also remove "of".)
Data and desired output
dt<- data.table(city = c("arecibo","arecibo","cabo rojo", "new york", "dallas"),
state=c("pr", "pr", "pr", "ny", "tx"),
name=c("frutas of pr arecibo", "dallas frutas of pr", "cabo rojo metal plant", "greens new york", "cowboy shoes dallas tx"),
desired=c("frutas", "dallas frutas", "metal plant", "greens", "cowboy shoes"))