title: Missing values in nested ifelse statements in R
body: <p>At some point in time, I encountered this problem...and solved it. However, as it is a recurring problem and I've now forgotten the solution, hopefully this question will offer clarification to others as well as me :)</p>
  
  <p>I am creating a variable that is based answers to several questions. Each question can have three values: 1, 2, or NA. 1's and 2's are mutually exclusive for each observation.</p>
  
  <p>I simply want to create a variable that is a composite of the choice coded with "1" for each person, and give it a value based on that code.</p>
  
  <p>So let's say I have this <code>df</code>:</p>
  
  <pre><code>ID   var1    var2   var3   var4
  1    1       2      NA     NA
  2    NA      NA     2      1
  3    2       1      NA     NA
  4    2       NA     1      NA
  </code></pre>
  
  <p>I then try to recode based on the following statement: </p>
  
  <pre><code>df$var &lt;- 
      ifelse(
          as.numeric(df$var1) == 1,
          "Gut instinct",
          ifelse(
              as.numeric(df$var2) == 1,
              "Data",
              ifelse(
                  as.numeric(df$var3) == 1,
                  "Science",
                  ifelse(
                      as.numeric(df$var4) == 1,
                      "Philosophy",
                      NA
                  )
              )
          )
      )
  </code></pre>
  
  <p>However, this code only PARTIALLY codes based on the "ifelse".  For example, <code>df$var</code> might have  observation of 'Gut instinct' and 'Philosophy', but the codings for when <code>var2</code> and <code>var3==1</code> are still NA.</p>
  
  <p>Any thoughts on why this might be happening?</p>