0

I want to delete the character:" (mean (SD))", and I used 3 methods but none of them are successful. I don't know why.

 names <- c("Y_OR_N (mean (SD))", "age (mean (SD))", "101004A (mean (SD))", "101016 (mean (SD))", "209003 (mean (SD))", "210005 (mean (SD))", "GFR (mean (SD))", "307001 (mean (SD))", "308001 (mean (SD))", "308014 (mean (SD))")


 names %>% str_remove(pattern = " (mean (SD))")
 names %>% str_replace(pattern = " (mean (SD))", replacement = "")
 names %>% gsub(x = ., pattern = " (mean (SD))", replacement = "") 

Any help will be highly appreciated!

Sotos
  • 51,121
  • 6
  • 32
  • 66
zhiwei li
  • 1,635
  • 8
  • 26
  • 2
    You can just do `str_remove(names, fixed(" (mean (SD))"))`. – tmfmnk Feb 25 '20 at 15:37
  • It seems that you can achieve it by simply deleting everything after the space. Try `sub(' .*', '', x)` **NOTE** I changed `names` to `x` as `names` is a predefined function in R – Sotos Feb 25 '20 at 15:38

2 Answers2

0

Parenthesis are special, try escaping them with the \\

names %>% str_remove_all(pattern = " \\(mean \\(SD\\)\\)")
M.Viking
  • 5,067
  • 4
  • 17
  • 33
0

You can try the code below, using gsub + trimws (optional), i.e.,

trimws(gsub("(mean (SD))","",names,fixed = TRUE))

such that

> trimws(gsub("(mean (SD))","",names,fixed = T))
 [1] "Y_OR_N"  "age"     "101004A" "101016"  "209003" 
 [6] "210005"  "GFR"     "307001"  "308001"  "308014" 
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81