R - Remove dashes from a column with phone numbers

user3922483 picture user3922483 · Aug 13, 2014 · Viewed 16.4k times · Source

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

Answer

A5C1D2H2I1M1N2O1R2T1 picture A5C1D2H2I1M1N2O1R2T1 · Aug 13, 2014

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)