title: R: Troubles with ifelse
body: <p>I have two measures for the same object. The measure is binary (1,0) but many observations are also missing, such that the possible options are: 1, 0, NA.</p>
  <p>Data Have:</p>
  <pre><code>Source1 Source2
  NA      NA
  NA      0
  NA      1
  0       NA
  0       0
  0       1
  1       NA
  1       0
  1       1
  </code></pre>
  <p>(Sources can contradict each other, ignore that for now).</p>
  <p>I would like to create a third composite variable that summarizes the two variables, such that IF EITHER of the two sources = 1, then the composite variable should be equal to 1. Otherwise, if either of the sources is not missing, then the composite variable should be equal to zero. Lastly, only if both sources are missing, the composite variable should be set to missing.</p>
  <p>Data Want:</p>
  <pre><code>Source1 Source2 Composite
  NA      NA      NA
  NA      0       0
  NA      1       1
  0       NA      0
  0       0       0
  0       1       1
  1       NA      1
  1       0       1
  1       1       1
  </code></pre>
  <p>I have tried different approaches but continue to have the same issue.</p>
  <p>Attempt 1:</p>
  <pre><code>df&lt;- df %&gt;% mutate(combined = ifelse(df$source1==1 | df$source2==1, 1, 
                                ifelse(df$source1==0 | df$source2==0, 0, NA)))
  </code></pre>
  <p>Attempt 2:</p>
  <pre><code>df2&lt;- df %&gt;% mutate(combined = ifelse(is.na(df$source1)   &amp; is.na(df$source2), NA, 
                                 ifelse(df$source1 == 1     | df$source2 ==1,    1, 0)))
  </code></pre>
  <p>Attempt 3:</p>
  <pre><code>df3&lt;- df %&gt;% mutate(combined = ifelse(df$source1==1, 1, 
                                 ifelse(df$source1==0 &amp; df$source2==1, 1,
                                        ifelse(df$source1==0 &amp;     df$source2==0, 0,
                                        ifelse(df$source1==0 &amp; is.na(df$source2), 0,       
                                 ifelse(is.na(df$source1) &amp; df$source2'==1, 1,
                                        ifelse(is.na(df$source1) &amp; df$source2==0, 0, NA)))))))
  </code></pre>
  <p>The codes identify whether there is a 1 in either source, but the rest of the values are all missing regardless of there being a 0 or not.</p>
  <p>Actual Output:</p>
  <pre><code>Source1 Source2 Composite
  NA      NA      NA
  NA      0       NA
  NA      1       1
  0       NA      NA
  0       0       NA
  0       1       1
  1       NA      1
  1       0       1
  1       1       1
  </code></pre>