title: How to convert entire dataframe to numeric while preserving decimals?
body: <p>I have a mixed class dataframe (numeric and factor) where I am trying to convert the entire data frame to numeric.  The following illustrates the type of data I am working with as well as the problem I am encountering:</p>
  
  <pre><code>&gt; a = as.factor(c(0.01,0.02,0.03,0.04))
  &gt; b = c(2,4,5,7)
  &gt; df1 = data.frame(a,b)
  &gt; class(df1$a)
  [1] "factor"
  &gt; class(df1$b)
  [1] "numeric"
  </code></pre>
  
  <p>When I try and convert the entire data frame to numeric, it alters the numeric values.  For example:</p>
  
  <pre><code>&gt; df2 = as.data.frame(sapply(df1, as.numeric))
  &gt; class(df2$a)
  [1] "numeric"
  &gt; df2
    a b
  1 1 2
  2 2 4
  3 3 5
  4 4 7
  </code></pre>
  
  <p>Previous posts on this site suggest using <code>as.numeric(as.character(df1$a))</code>, which works great for one column.  However, I need to apply this approach to a dataframe that may contain hundreds of columns.  </p>
  
  <p><strong>What are my options for converting an entire dataframe from factor to numeric, while preserving the numeric decimal values?</strong></p>
  
  <p>The following is the output I would like to produce where <code>a</code> and <code>b</code> are numeric:</p>
  
  <pre><code>     a b
  1 0.01 2
  2 0.02 4
  3 0.03 5
  4 0.04 7
  </code></pre>
  
  <hr>
  
  <p>I have read the following related posts, although none of them apply directly to this case:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/20538084/how-to-convert-a-factor-variable-to-numeric-while-preserving-the-numbers-in-r">How to convert a factor variable to numeric while preserving the
  numbers in R</a> This references a single column in a dataframe.</li>
  <li><a href="https://stackoverflow.com/questions/14717217/converting-from-a-character-to-a-numeric-data-frame">converting from a character to a numeric data frame</a>.  This post
  does not take into account decimal values.</li>
  <li><a href="https://stackoverflow.com/questions/7299991/how-can-i-convert-a-factor-column-that-contains-decimal-numbers-to-numeric">How can i convert a factor column that contains decimal numbers to
  numeric?</a>.  This applies to only one column in a data frame.</li>
  </ol>