I have some data like these
var1 var2
10 NA
101 NA
NA 86
11 NA
NA 11
NA 61
If one variable is NA then the other one is not, and vice-versa.
How can I combine them into a single variable:
var3
10
101
86
11
11
61
I can do it easily with a loop, but it is quite slow, so I would like to find an easier way. I thought about assigning 0 to the values that are NA and then just adding the variables together…is there a better way ?
Various methods exist. Here’s one way:
Here it is working on your example:
This method is relatively general – it works with non-numeric data for example:
The suggestion of replacing
NAwith0and adding wouldn’t work in that case.