Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standardizing phone number data

Tags:

regex

r

I'm looking for a more elegant solution for this:

phone_number <- function(x) {
x <- gsub("[\\() -]", "", x) 
x <- gsub("^(.{3})(.{3})(.*)","\\1-\\2-\\3", x, perl = TRUE)
}

This would take data like this:

(123) 123-1234
123-123-1234
123 123-1234

And produce this:

123-123-1234
123-123-1234
123-123-1234
like image 548
SCDCE Avatar asked Mar 12 '26 23:03

SCDCE


1 Answers

We could do

gsub(".*(\\d{3}).*(\\d{3}).*(\\d+).*?", "\\1-\\2-\\3", x)
# [1] "123-123-1234" "123-123-1234" "123-123-1234"

assuming that we have only three groups of digits somewhere in a character and they are what we need.

like image 95
Julius Vainora Avatar answered Mar 15 '26 11:03

Julius Vainora