how to replace or remove a part of string?

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

Created on 2022-06-04 by the reprex package (v2.0.1)

1 Like

thanks a lot, i have resolved this issue.
using a function below

mutate(column_name = str_remove(colunm_name , "\(.+\)"))

would you explain what ".+" mean in "\(.+\)"?
i consider ".+" means every character in parentheses, it is right?

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".

1 Like

got it!
thanks a lot.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.