Home > Software engineering >  Remove numbers from a list of strings
Remove numbers from a list of strings

Time:11-09

I have a list of strings like this

string_list<-c(">=23", ">=50", "large than 23 years", "Yes", "No2", "Yes4", "Negative", "Positive9", "Negative10")

Is there a way to remove the numbers after Yes, No, Positive, and Negative while not affecting the rest of the string? I used string_remove but I can't identify this specific pattern. Here is the list I wanted to have:

string_list<-c(">=23", ">=50", "large than 23 years", "Yes", "No", "Yes", "Negative", "Positive", "Negative")

Thanks!

CodePudding user response:

This should do the trick, which searches for the specific strings and removes the numbers. Note if case may be an issue in your real data (i.e. Yes, yes) use ignore.case = TRUE:

string_list[grep("Yes|No|Posi|Neg", string_list)] <- gsub('[[:digit:]] ', '', string_list[grep("Yes|No|Posi|Neg", string_list)])

Output:

# [1] ">=23"                ">=50"                "large than 23 years"
# [4] "Yes"                 "No"                  "Yes"                
# [7] "Negative"            "Positive"           "Negative" 

CodePudding user response:

string_list<-c(">=23", ">=50", "large than 23 years", "Yes", "No2", "Yes4", "Negative", "Positive9", "Negative10")
gsub("(?i)(yes|no|positive|negative)[0-9] ","\\1", string_list)
#> [1] ">=23"                ">=50"                "large than 23 years"
#> [4] "Yes"                 "No"                  "Yes"                
#> [7] "Negative"            "Positive"            "Negative"

Created on 2022-11-08 with reprex v2.0.2

  • Related