title: Is there a short alternative to as.numeric(as.character(my.factor)) in R?
body: <p>I am getting very tired of writing <code>as.numeric(as.character(my.factor))</code> if I want to get the numeric value of a factor in R. Although it works, it is not self-evident what the code does and it just feels plain wrong to convert numbers to character strings and back again to do anything with them.  Is there a simpler and more self-explanatory way like <code>factor.values(my.factor)</code>?</p>
  <p>It has been suggested to packing it away in a custom function like</p>
  <pre class="lang-r prettyprint-override"><code>factor.values = function(x) as.numeric(levels(x))[x]  # get the actual values of a factor with numeric labels
  </code></pre>
  <p>the problem with this solution is that it must be copy-pasted between scripts if it is to be reproducible by collaborators. I'm asking if there is a short built-in method to do it. I know this is a VERY small problem, but since it's a frequent one and many find the commonly presented solution anti-intuitive, I raise it anyway.</p>
  <h1>The problem</h1>
  <p>Fpr the unitiated, if you have a factor and want to do numeric operations on it you run into a number of problems:</p>
  <pre class="lang-r prettyprint-override"><code>   &gt; my.factor = factor(c(1, 1, 2, 5, 8, 13, 21))
      &gt; sum(my.factor)  # let's try a numeric operation
      Error in Summary.factor(1:6, na.rm = FALSE) : 
        sum not meaningful for factors
      &gt; as.numeric(my.factor)  # oh, let's make it numeric then.
      [1] 1 1 2 3 4 5 6  # argh! levels numbers and not values
      &gt; as.character(my.factor)  # because the web told me so.
      [1] &quot;1&quot;  &quot;1&quot;  &quot;2&quot;  &quot;5&quot;  &quot;8&quot;  &quot;13&quot; &quot;21&quot;  # closer...
      &gt; as.numeric(as.character(my.factor))  # NOT short or self-explanatory!
      [1]  1  1  2  5  8 13 21  # finally we can sum ...
      &gt; sum(as.numeric(as.character(my.factor)))
      [1] 51
  </code></pre>