I have a vector of character data. Most of the elements in the vector consist of one or more letters followed by one or more numbers. I wish to split each element in the
With stringr, if you like (and slightly different from the answer to the other question):
# load library
library(stringr)
#
# load data
my.data <- c("aaa", "b11", "b21", "b101", "b111", "ccc1", "ffffd1", "ccc20", "ffffd13")
#
# extract numbers only
my.data.num <- as.numeric(str_extract(my.data, "[0-9]+"))
#
# check output
my.data.num
[1] NA 11 21 101 111 1 1 20 13
#
# extract characters only
my.data.cha <- (str_extract(my.data, "[aA-zZ]+"))
#
# check output
my.data.cha
[1] "aaa" "b" "b" "b" "b" "ccc" "ffffd" "ccc" "ffffd"