hi everyone!
some strings in a column have some characters i do not needed.
like apple(china), water (japan), cake(Britain).
i use function---
mutate_at("column name" , str_replace , "(china)" , "" )
but the outcome is apple()
how can i remove () ?
or do you have any better solution? and explain to me, please!
Parentheses are special characters in regular expression so you have to escape them with back slashes to have them be interpreted as literal parentheses.
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
DF <- data.frame(Things=c("apple(china)", "water(japan)",
"cake(britain)"))
DF <- DF |> mutate(Things = str_remove(Things, "\\(.+\\)"))
DF
#> Things
#> 1 apple
#> 2 water
#> 3 cake
The .+ is a "regular expression", a kind of advanced wild card. Regular expressions are commonly used in R and other programming languages for matching text. There are many online tutorials and it would be useful for you to study them at some point. The . in the expression means "any character" and the "+" means "one or more". Together they mean "one or more of any character".