title: Convert factor to integer while maintaining factor level ordering
body: <p>I have an R dataframe where one of the columns is a factor whose levels have an implicit ordering. <strong>How can I convert the factor levels to specific integers</strong> in the following manner:</p>
  
  <ul>
  <li>"Strongly disagree" --> 1</li>
  <li>"Somewhat disagree" --> 2</li>
  <li>"Neutral" --> 3</li>
  <li>"Somewhat agree" --> 4</li>
  <li>"Strongly agree" --> 5</li>
  </ul>
  
  <p>For example, here is my data frame:</p>
  
  <pre><code>agree &lt;- c("Strongly agree", "Somewhat disagree", "Somewhat agree",
             "Neutral", "Strongly agree", "Strongly disagree", "Neutral")
  age &lt;- c(41, 35, 29, 42, 31, 22, 58)
  
  df &lt;- data.frame(age, agree)
  df
  #   age             agree
  # 1  41    Strongly agree
  # 2  35 Somewhat disagree
  # 3  29    Somewhat agree
  # 4  42           Neutral
  # 5  31    Strongly agree
  # 6  22 Strongly disagree
  # 7  58           Neutral
  
  str(df)
  # 'data.frame': 7 obs. of  2 variables:
  #  $ age  : num  41 35 29 42 31 22 58
  #  $ agree: Factor w/ 5 levels "Neutral","Somewhat agree",..: 4 3 2 1 4 5 1
  </code></pre>
  
  <p>Now, I would like to convert the <code>agree</code> column to be an integer column using the mapping that I showed above.</p>
  
  <p>I already searched these other questions about converting factor to integer, but they do not related to maintaining the factor ordering.</p>
  
  <p>"<a href="https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information">How to convert a factor to an integer\numeric without a loss of information?</a>"</p>
  
  <p>"<a href="https://stackoverflow.com/questions/4798343/convert-factor-to-integer">Convert factor to integer</a>"</p>
  
  <p>"<a href="https://stackoverflow.com/questions/9480408/convert-factor-to-integer-in-a-data-frame">Convert factor to integer in a data frame</a>"</p>