Remove numbers from alphanumeric characters
You can use gsub
for this:
gsub('[[:digit:]]+', '', x)
or
gsub('[0-9]+', '', x)
# [1] "ACO" "BCKDHB" "CD"
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:]", "")
A solution using stringi:
# your data
x <-c('ACO2', 'BCKDHB456', 'CD444')
# extract capital letters
x <- stri_extract_all_regex(x, "[A-Z]+")
# unlist, so that you have a vector
x <- unlist(x)
Solution in one line: