Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove numbers from alphanumeric characters

Tags:

regex

r

I have a list of alphanumeric characters that looks like:

x <-c('ACO2', 'BCKDHB456', 'CD444') 

I would like the following output:

x <-c('ACO', 'BCKDHB', 'CD') 

Any suggestions?

# dput(tmp2)  structure(c(432L, 326L, 217L, 371L, 179L, 182L, 188L, 268L, 255L,...,  ), class = "factor") 
like image 766
Bfu38 Avatar asked Nov 27 '12 17:11

Bfu38


People also ask

How do I remove numbers from a string in R?

To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space.


1 Answers

You can use gsub for this:

gsub('[[:digit:]]+', '', x) 

or

gsub('[0-9]+', '', x) # [1] "ACO"    "BCKDHB" "CD"  
like image 168
Justin Avatar answered Sep 20 '22 15:09

Justin