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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With