0

I have a column contain and I wanted to remove the (*) at the end of the string but some has space in between and some has not:

Variable
0. No (1)
1. Yes(2)
2. Refuse to Answer (3)
3. Agree(4)
4. Disagree (5)

Desired outcome:

Variable
0.No
1.Yes
2.Refuse to Answer
3.Agree
4.Disagree 

I want to remove the punctuation marks (), but I left the dot and removed the number inside the blanket.

I tried to do it one by one:

gsub("[[:punct:]]","",df$column)
gsub("[[:num:]]","",df$column)

but it will also remove the front part of the string. Is there any advice? thanks.

Peter Chung
  • 1,010
  • 1
  • 13
  • 31
  • 1
    Does this answer your question? [Remove parentheses and text within from strings in R](https://stackoverflow.com/questions/24173194/remove-parentheses-and-text-within-from-strings-in-r) – benson23 Jun 07 '23 at 06:20
  • If this is the exact string format, I would just drop the last 4 characters of each string. From memory, `stringr::str_sub(x, 1, -4)`. – Paul Stafford Allen Jun 07 '23 at 06:27

2 Answers2

1

You can use sub with *\\(.* to match a space 0 to n times *, followed by ( \\( and followed by anything .*.
\\. * will match . followed by 0 to n space. Here also instead of \\. * would give the desired result.

sub("\\. *", ".", sub(" *\\(.*", "", s))
#[1] "Variable"           "0.No"               "1.Yes"             
#[4] "2.Refuse to Answer" "3.Agree"            "4.Disagree"        

Data

s <- c("Variable", "0. No (1)", "1. Yes(2)", "2. Refuse to Answer (3)",
  "3. Agree(4)", "4. Disagree (5)")
GKi
  • 37,245
  • 2
  • 26
  • 48
1

Alternative method without gsub:

# Data (assumed data frame format, note no space for "Yes" or "Agree")
s <- data.frame(Variable = c("0. No (1)", "1. Yes(2)", "2. Refuse to Answer (3)",
       "3. Agree(4)", "4. Disagree (5)"))

# cut off last three characters, then trim whitespace
stringr::str_sub(s$Variable, 1, -4) |> stringr::str_trim() 

gives:

[1] "0. No"               "1. Yes"              "2. Refuse to Answer" "3. Agree"            "4. Disagree" 
Paul Stafford Allen
  • 1,840
  • 1
  • 5
  • 16