ifelse argument returns index of variable instead of variable value
<p>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:</p>

<pre><code>SSN &lt;- 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 &lt;- gsub('-','', SSN$SSN)
SSN$SSN &lt;- ifelse(grepl('\\d{9}', SSN$SSN), SSN$SSN, SSN$id) 
</code></pre>

<p>This results in this:</p>

<pre><code>       SSN   id
1 123456789 3005
2         2 3006
3 666786543 3007
4 800445678 3008
5         5 3009
6         6 3010
</code></pre>

<p>But what I need is this:</p>

<pre><code>SSN   id
1 123456789 3005
2      3006 3006
3 666786543 3007
4 800445678 3008
5      3009 3009
6      3010 3010
</code></pre>

<p>Any assistance would be greatly appreciated! Thanks in advance!</p>