title: combining 2 columns using ifelse dropping a variable R
body: <p>I am trying to combine the male and female columns I have created into one column.  I tried using some answers I found on stack, but the second sex I queried was excluded.</p>
  
  <p>Build Data Frame:</p>
  
  <pre><code>ID &lt;- 1:10
  SPAYDT &lt;- c("", "2011-12-01", "", "2006-05-01", "", "", "", "", "", "")
  SPAYDTU &lt;- c(1, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  NEUTDT &lt;- c("", "", "", "", "", "", "2013-03-01", "", "", "")
  NEUTDTU &lt;- c(NA, NA, NA, NA, NA, NA, NA, 1, NA, NA)
  df &lt;- as.data.frame(cbind(ID, SPAYDT, SPAYDTU, NEUTDT, NEUTDTU))
  df
  </code></pre>
  
  <p>The goal is to have a column for sex, formated as a factor with 2 levels - Male and Female
  It should say female if the SPAYDT or SPAYDTU have a value in them, and male if the NEUTDT or NEUTDTU have a value in them.
  What I have tried:</p>
  
  <ul>
  <li>using a nested if-else statement to build one sex column</li>
  <li>making two columns then combining using</li>
  </ul>
  
  <pre><code>df$male &lt;- ifelse(NEUTDT!="", "Male",
                    ifelse(NEUTDTU=1, "Male", NA))
  df$female &lt;- ifelse(SPAYDT!="", "Female",
                      ifelse(SPAYDTU==1, "Female", NA))
  df$sex &lt;- ifelse(!is.na(df$female), df$female, df$male)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>df$sex &lt;- ifelse(SPAYDT!="", "Female",
                   ifelse(SPAYDTU==1, "Female",
                          ifelse(NEUTDT!="", "Male",
                                 ifelse(NEUTDTU=1, "Male", NA))))
  </code></pre>
  
  <p>However, no matter what I do, the sex column at the end only has one sex.  I made sure my df was attached for use of column names as variables.  I tried restarting R and running the setup code again.  I just don't know why the ifelse statement is ignoring the second sex input.</p>
  
  <p>Any help is greatly appreciated!</p>
  
  <p>Clarifications:
  In the larger dataframe I am working with I have done data clean up so that each ID only corresponds to 1 sex. Sorry about the mistake in the code.</p>
  
  <p>Desired output:</p>
  
  <pre><code>ID &lt;- 1:10
  SPAYDT &lt;- c("", "2011-12-01", "", "2006-05-01", "", "", "", "", "", "")
  SPAYDTU &lt;- c(1, NA, NA, NA, NA, NA, NA, NA, NA, NA)
  NEUTDT &lt;- c("", "", "", "", "", "", "2013-03-01", "", "", "")
  NEUTDTU &lt;- c(NA, NA, NA, NA, NA, NA, NA, 1, NA, NA)
  SEX &lt;- c("Female", "Female", NA, "Female", NA, NA, "Male", "Male", NA, NA)
  df &lt;- as.data.frame(cbind(ID, SPAYDT, SPAYDTU, NEUTDT, NEUTDTU, SEX))
  df
  </code></pre>