Reputation: 1238
I would like to remove after specific text in rows whatever exist.
I use this:
dframe$col1 <- gsub("Stock Amazon|Or Another|Google","",dframe$col1)
but it removes the whole row and not whatever after the string. What can I do to fix it?
Upvotes: 0
Views: 41
Reputation: 21442
If, as you say, you want to remove whatever text exists after some text, then lookaround may help you:
DATA:
text <- c("some text Stock Amazon more text", "text again Or Another text", "yet more Google and this")
SOLUTION:
This pattern says, "if you see Stock Amazon
or Or Another
or Google
on the left delete whatever comes next":
gsub("(?<=Stock Amazon|Or Another|Google).*", "", text, perl = T)
[1] "some text Stock Amazon" "text again Or Another" "yet more Google"
Upvotes: 1
Reputation: 1
It's not easy to answer without an example, but here is a pointer using the stringr library:
library(tidyverse)
dframe <- dframe %>%
mutate(col1 = str_extract(col1, "Your text"))
Hope it helps.
Upvotes: 0