mardi 5 janvier 2016

Formatting Phone Numbers in R

So I have a list of phone numbers that don't have standard formatting, thus they can be (123)-456-7890, 1234567890, 456890, 456-7890.

I want to go through my list of phone numbers and format them to be 123-456-7890 456-7890

So far what I have in R is

 phone_num <- gsub("-","", gsub("\\(","", gsub("\\)","", csv_file$Phone)))

 if(nchar(phone_num)== 10) {
    cleaned_phone <-gsub("(\\d{3})(\\d{3})(\\d{4}$)", "\\1-\\2-\\3", phone_num) 
  }
    else if (nchar(phone_num) == 7){
             cleaned_phone <-gsub("(\\d{3})(\\d{4}$)", "\\1-\\2", phone_num)
    }
         else (nchar(phone_num) != 7 || nchar(phone_num) != 10){
              cleaned_phone <- phone_num
         }

But I am getting errors - any ideas? Thank you!!!

Aucun commentaire:

Enregistrer un commentaire