Remove numbers from alphanumeric characters

If your goal is just to remove numbers, then the removeNumbers() function removes numbers from a text. Using it reduces the risk of mistakes.

library(tm)

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

x <- removeNumbers(x)

x

[1] "ACO"    "BCKDHB" "CD"    

Using stringr

Most stringr functions handle regex

str_replace_all will do what you need

str_replace_all(c('ACO2', 'BCKDHB456', 'CD444'), "[:digit:]", "")

You can use gsub for this:

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

or

gsub('[0-9]+', '', x)
# [1] "ACO"    "BCKDHB" "CD" 

Tags:

Regex

R