[r] How to convert entire dataframe to numeric while preserving decimals?

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:

> a = as.factor(c(0.01,0.02,0.03,0.04))
> b = c(2,4,5,7)
> df1 = data.frame(a,b)
> class(df1$a)
[1] "factor"
> class(df1$b)
[1] "numeric"

When I try and convert the entire data frame to numeric, it alters the numeric values. For example:

> df2 = as.data.frame(sapply(df1, as.numeric))
> class(df2$a)
[1] "numeric"
> df2
  a b
1 1 2
2 2 4
3 3 5
4 4 7

Previous posts on this site suggest using as.numeric(as.character(df1$a)), which works great for one column. However, I need to apply this approach to a dataframe that may contain hundreds of columns.

What are my options for converting an entire dataframe from factor to numeric, while preserving the numeric decimal values?

The following is the output I would like to produce where a and b are numeric:

     a b
1 0.01 2
2 0.02 4
3 0.03 5
4 0.04 7

I have read the following related posts, although none of them apply directly to this case:

  1. How to convert a factor variable to numeric while preserving the numbers in R This references a single column in a dataframe.
  2. converting from a character to a numeric data frame. This post does not take into account decimal values.
  3. How can i convert a factor column that contains decimal numbers to numeric?. This applies to only one column in a data frame.

This question is related to r dataframe numeric

The answer is


Using dplyr (a bit like sapply..)

df2 <- mutate_all(df1, function(x) as.numeric(as.character(x)))

which gives:

glimpse(df2)
Observations: 4
Variables: 2
$ a <dbl> 0.01, 0.02, 0.03, 0.04
$ b <dbl> 2, 4, 5, 7

from your df1 which was:

glimpse(df1)
Observations: 4
Variables: 2
$ a <fctr> 0.01, 0.02, 0.03, 0.04
$ b <dbl> 2, 4, 5, 7

> df2 <- data.frame(sapply(df1, function(x) as.numeric(as.character(x))))
> df2
     a b
1 0.01 2
2 0.02 4
3 0.03 5
4 0.04 7
> sapply(df2, class)
        a         b 
"numeric" "numeric" 

df2 <- data.frame(apply(df1, 2, function(x) as.numeric(as.character(x))))

Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to dataframe

Trying to merge 2 dataframes but get ValueError How to show all of columns name on pandas dataframe? Python Pandas - Find difference between two data frames Pandas get the most frequent values of a column Display all dataframe columns in a Jupyter Python Notebook How to convert column with string type to int form in pyspark data frame? Display/Print one column from a DataFrame of Series in Pandas Binning column with python pandas Selection with .loc in python Set value to an entire column of a pandas dataframe

Examples related to numeric

How to convert entire dataframe to numeric while preserving decimals? What's the difference between integer class and numeric class in R IsNumeric function in c# How to compare numbers in bash? Right way to convert data.frame to a numeric matrix, when df also contains strings? angularjs: allows only numbers to be typed into a text box How to convert Varchar to Double in sql? SQL Server : error converting data type varchar to numeric How do I convert certain columns of a data frame to become factors? How to create a numeric vector of zero length in R