I have a unique character, each letter follows a number. For instance: A1B10C5
A1B10C5
I would like to split it into letter <- c(A, B, C) and number <- c(1, 10, 5) using R.
letter <- c(A, B, C)
number <- c(1, 10, 5)
We can use regex lookarounds to split between the letters and numbers
v1 <- strsplit(str1, "(?<=[A-Za-z])(?=[0-9])|(?<=[0-9])(?=[A-Za-z])", perl = TRUE)[[1]] v1[c(TRUE, FALSE)] #[1] "A" "B" "C" as.numeric(v1[c(FALSE, TRUE)]) #[1] 1 10 5
str1 <- "A1B10C5"
2.1m questions
2.1m answers
60 comments
57.0k users