简体   繁体   中英

removing characters in brackets of indefinite length from end of column names in R

I have some column names in a df as follows:

column1 (-)
column2 (unwantedstring)
column3
column4 (4)

Note that some columns do not have unwanted brackets on the end and should be kept the same.

I want to get rid of the brackets at the end and anything inside it (along with the space before the opening brackets) to get:

column1
column2
column3
column4

Am I on the right track with the below?

df <- df %>%
  rename_with(~str_remove(.x, " \(*\)$"))

Any help would be appreciated

You may use sub here for a base option:

x <- c("column1 (-)", "column2 (unwantedstring)", "column3", "column4 (4)")
output <- sub("\\s+\\(.*\\)$", "", x)
output

[1] "column1" "column2" "column3" "column4"

You can also use the following solution, however, it's a bit complicated.

trimws(regmatches(x, regexpr("\\([^()]\\](SKIP*)(FAIL*)|(?<!\\()[^()]*(?!\\))", x, perl = TRUE)))

[1] "column1" "column2" "column3" "column4"

Thanks for the data Tim Biegeleisen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM