0

Say I have a tibble that looks like this

library(tidyverse)
df <- tibble(`var` = c(1,2), `text` = c("elephant $ more text", "cat $ some more text"))

and I would like to programmatically get all the text from the character $ to be removed, so to obtain the equivalent result of

df <- tibble(`var` = c(1,2), `text` = c("elephant ", "cat "))

I tried to use the equivalent of Removing strings after a certain character in a given text

but it seems that thee special character $ does not yield the desired outcome.

Portland
  • 103
  • 3

1 Answers1

2

You can achieve this by simply escaping the $ character using the \\ characters:

df %>%
  mutate(text = gsub("\\\$.*", "", text))
var text
1 elephant more text
2 cat some more text
Oxbowerce
  • 7,077
  • 2
  • 8
  • 22