user3922483
user3922483

Reputation: 183

R - Remove dashes from a column with phone numbers

I'd like to create a new column of phone numbers with no dashes. I have data that is a mix of just numbers and some numbers with dashes. The data looks as follows:

Phone
555-555-5555
1234567890
555-3456789
222-222-2222
51318312491

Upvotes: 5

Views: 25827

Answers (2)

lawyeR
lawyeR

Reputation: 7664

Building on the answer of @Ananda Mahto, it seemed useful to show how to break the numbers up again and put a parenthetical around the area code.

phone <- c("1234567890", "555-3456789", "222-222-2222", "5131831249")
phone <- gsub("-", "", phone)
gsub("(^\\d{3})(\\d{3})(\\d{4}$)", "(\\1) \\2 \\3", phone)
[1] "(123) 456 7890" "(555) 345 6789" "(222) 222 2222" "(513) 183 1249"

The second regex creates three capture groups, two with three digits and the final one with four. Then R substitutes them back in with a space between each and ( ) around the first one. You could also put hyphens between capture group 2 and capture group 3. [Not sure at all why Skype appeared out of nowhere!]

Upvotes: 6

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193657

Since you are dealing with a very straightforward substitution, you can easily use gsub to find the character you want to remove and replace it with nothing.

Assuming your dataset is called "mydf" and the column of interest is "Phone", try this:

gsub("-", "", mydf$Phone)

Upvotes: 23

Related Questions