Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a character to letters and numbers

Tags:

regex

split

r

I have a unique character, each letter follows a number. For instance: A1B10C5

I would like to split it into letter <- c(A, B, C) and number <- c(1, 10, 5) using R.

like image 884
Wang Avatar asked Aug 21 '17 20:08

Wang


4 Answers

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

data

str1 <- "A1B10C5"
like image 165
akrun Avatar answered Oct 23 '22 03:10

akrun


str_extract_all is another way to do this:

library(stringr)

> str <- "A1B10C5"
> str
[1] "A1B10C5"

> str_extract_all(str, "[0-9]+")
[[1]]
[1] "1"  "10" "5" 

> str_extract_all(str, "[aA-zZ]+")
[[1]]
[1] "A" "B" "C"
like image 36
Sagar Avatar answered Oct 23 '22 01:10

Sagar


To extract letters and numbers at same time, you can use str_match_all to get letters and numbers in two separate columns:

library(stringr)
str_match_all("A1B10C5", "([a-zA-Z]+)([0-9]+)")[[1]][,-1]

#     [,1] [,2]
#[1,] "A"  "1" 
#[2,] "B"  "10"
#[3,] "C"  "5" 
like image 3
Psidom Avatar answered Oct 23 '22 01:10

Psidom


You can also use the base R regmatches with gregexpr:

regmatches(this, gregexpr("[0-9]+",  "A1B10C5"))
[[1]]
[1] "1"  "10" "5" 

regmatches(this, gregexpr("[A-Z]+",  "A1B10C5"))
[[1]]
[1] "A" "B" "C"

These return lists with a single element, a character vector. As akrun does, you can extract the list item using [[1]] and can also convert the vector of digits to numeric like this:

as.numeric(regmatches(this, gregexpr("[0-9]+", this))[[1]])
like image 2
lmo Avatar answered Oct 23 '22 03:10

lmo