title: Add factor column to dataframe based on existing column
body: <p>Let's say I have a dataframe:</p>
  
  <pre><code>word &lt;- c("good", "great", "bad", "poor", "eh")
  userid &lt;- c(1, 2, 3, 4, 5)
  d &lt;- data.frame(userid, word)
  </code></pre>
  
  <p>I want to add a dataframe column, <code>sentiment</code>, that is a <code>factor</code> and depends on what <code>word</code> is:</p>
  
  <pre><code>words_pos &lt;- c("good", "great")
  words_neg &lt;- c("bad", "poor")
  calculate_sentiment &lt;- function(x) {
       if (x %in% words_pos) {
           return("pos")
       } else if (x %in% words_neg) {
           return("neg")
       }
       return(NA)
  }
  d$sentiment &lt;- apply(d, 1, function(x) calculate_sentiment(x['word'])
  </code></pre>
  
  <p>However, now <code>d$sentiment</code> is of type "character". How do I make it a factor with the right levels? <code>pos</code>, <code>neg</code>, <code>NA</code> -- I'm not even sure if <code>NA</code> should be a factor level, as I'm just learning R.</p>
  
  <p>Thanks!</p>