I am trying to figure out a clean way to add multiple new columns corresponding to multiple existing columns all at once in R. I want each new column cell to be a 0 if the corresponding cell in the corresponding column is an NA value, or a 1 if the corresponding cell has any other value (see example tables below).
I have been able to accomplish this by creating the new columns first, then with a messy which function and a loop. I was wondering if there was a clean way to do this and add in all the new columns with values at once.
Start data
| A | B |
|---|---|
| 5 | NA |
| NA | 4 |
After function is applied
| A | B | A New | B New |
|---|---|---|---|
| 5 | NA | 1 | 0 |
| NA | 4 | 0 | 1 |