mardi 6 septembre 2016

ifelse argument returns index of variable instead of variable value

I am trying to replace the SSN value with the corresponding id variable value when the SSN is invalid. The code that I have instead replaces it with the index. Here is some sample data and my code:

SSN <- data.frame(SSN = c("123-45-6789", "MR1-23-4567", "666786543", "800445678",
          "45678AB90", "77212345", "987654321", "234-56-7890", "123004567", "000345678"), 
id = c("3005", "3006", "3007", "3008", "3009", "3010", "3011", "3012", "3013", "3014" ))

SSN$SSN <- gsub('-','', SSN$SSN)
SSN$SSN <- ifelse(grepl('\\d{9}', SSN$SSN), SSN$SSN, SSN$id) 

This results in this:

       SSN   id
1 123456789 3005
2         2 3006
3 666786543 3007
4 800445678 3008
5         5 3009
6         6 3010

But what I need is this:

SSN   id
1 123456789 3005
2      3006 3006
3 666786543 3007
4 800445678 3008
5      3009 3009
6      3010 3010

Any assistance would be greatly appreciated! Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire