title: How to create a new variable in a data.frame based on a condition?
body: <p>Assume we have a dataframe </p>
  
  <pre><code>x   y
  1   1
  2   4 
  4   5 
  </code></pre>
  
  <p>how can you add a new variable to the dataframe such that if x is less than or equal to 1 it returns "good" if x is between 3 and 5 it returns "bad" else returns "fair"</p>
  
  <pre><code>x   y  w
  1   1  "good"
  2   2   "fair"
  5   5   "bad"
  </code></pre>
  
  <p>Applied the method shown by ocram., however this one here does not work. </p>
  
  <pre><code>d1 &lt;- c("e", "c", "a")
  d2 &lt;- c("e", "a", "b")
  
  w &lt;- ifelse(d1 == "e" &amp; (d2=="e"), 1, ifelse((d1 == "a") &amp; (d2 =="b"), 2, ifelse(d1 == "e"),3,99))
  </code></pre>
  
  <p>Any ideas?
  Thanks</p>