title: New column with nested if else based on two columns, ignoring NA if present in only one other column
body: <p>I have a dataset containing two variables, var1 and var2. Some cases have a value in var1 and some in var2. Cases with a value in var1 are NA in var2 and vice versa. I would like to combine these variables into one with the value 1 or 0. The new variable should take on the value of 1 if var1 OR var2 has a value of 1, the value of 0 if var1 or var2 has a value of 0 and NA only if both var1 and var2 is NA.</p>
  
  <p>Basically, what I am trying to do is to get R to ignore NA in var1 or var2 if there is a value in the other, and only return NA is NA are present in BOTH variables. </p>
  
  <p>I have tried the following solutions:</p>
  
  <p>First attempt:</p>
  
  <pre><code>df$new_var&lt;-ifelse(df$var1==1|df$var2==1,1,
                      ifelse(df$var1&lt;1|df$var2&lt;1,0,
                          ifelse(is.na(df$var1)&amp;df$var2&lt;1,0,0
                             ifelse(is.na(df$var2)&amp;df$var1&lt;1,0,0))))
  </code></pre>
  
  <p>Second attempt: </p>
  
  <pre><code>df$new_var&lt;-ifelse(df$var1==1|df$var2==1,1,
                      ifelse(df$var1==0|df$var2==0,0,NA)) 
  </code></pre>
  
  <p>Third attempt: </p>
  
  <pre><code>df$new_var&lt;-ifelse(df$var1==1|df$var2==1,1,
                      ifelse(df$var1==0|df$var2==0,0,NA))
  </code></pre>
  
  <p>All returns a variable with 1 and NAs and no 0. </p>
  
  <p>What I am doing wrong?</p>