title: Conditionally changing only some cells in a data frame - ifelse() failure?
body: <p>I am trying to conditionally change some items when cleaning survey data. </p>
  
  <p>I've got two questions, Question X and Question Y. If they respond 1 or 2 for Question X, they go on to answer Question Y. If they answer 3 or 4 for Question X, they skip Question Y. </p>
  
  <p>If they answer X with 1 or 2 and then skip Y, I want to record their 'NULL!' entries as NA - they just didn't answer the question when they should have. 
  If they answer X with 3 or 4 and then skip Y, I want to record their 'NULL!' entries as 0 - they weren't supposed to answer the question, so they didn't. </p>
  
  <p>Here's a reproducible dataset I made:</p>
  
  <pre><code>  set.seed(1)
  df &lt;- data.frame(
    X = as.factor(sample(c("1.00", "2.00", "3.00", "4.00"), 10, replace = TRUE)),
    Y = as.factor(sample(c("1.00", "2.00", "#NULL!"), 10, replace = TRUE))
  )
  df
  </code></pre>
  
  <p>I'm trying to replace the aforementioned 'NULL!' fields with either NA or 0 respectively. I've been trying it with ifelse() and have had little luck - it appears to return anything that is 1.00 or 2.00 as NA and 3.00 or 4.00 as 0. Is there a better way to do this? What am I doing wrong? </p>
  
  <pre><code>levels(df$Y) &lt;- c(levels(df$Y), 0)
      df$Y &lt;- ifelse(df$X == '3.00'| df$X == '4.00', df$Y[df$y == 'NULL!'] &lt;- 0, df$Y[df$Y == '#NULL!'] &lt;- NA)
      df
  </code></pre>
  
  <p>Thank you for your help! </p>