I have
df<-c("That's","you're", "'am")
and I would like to remove the part of a word after and including the apostrophe which should return
c("That", "you", "")
tidyverse
solution or a solution usable within a pipe |>
structure preferable
CodePudding user response:
Replace '
and whatever follows it, using str_replace
in stringr
.
library(stringr)
str_replace(df, "'.*", "")
#[1] "That" "you" ""
CodePudding user response:
Using R base sub
> sub("'.*", "", df)
[1] "That" "you" ""
CodePudding user response:
Your example data only has one word per string. If you also need it to work for strings containing multiple words then use:
gsub("'\\w*\\b","",df)
CodePudding user response:
Using trimws
in base R
trimws(df, whitespace = "'.*")
[1] "That" "you" ""