如何将整个数据框转换为数字,同时保留小数? [英] How to convert entire dataframe to numeric while preserving decimals?

查看:255
本文介绍了如何将整个数据框转换为数字,同时保留小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个混合类数据帧(数字和因子),我试图将整个数据帧转换为数字。以下说明了我正在使用的数据类型以及我遇到的问题:

 > 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

当我尝试转换整个数据帧变为数字,它改变了数值。例如:

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

此网站上的帖子建议使用 as.numeric(as.character(df1 $ a)),这对于一列。但是,我需要将这种方法应用于可能包含数百列的数据帧。



在保留数字十进制值的同时,将整个数据帧从因数转换为数字的方式是什么?



以下是我想生成的输出,其中 a b 是数字:

  ab 
1 0.01 2
2 0.02 4
3 0.03 5
4 0.04 7






我已经阅读了以下相关内容帖子,虽然没有一个直接适用于这种情况:


  1. 如何将因子变量转换为数字,同时保留R $中的
    数字, a>这引用数据框中的一列。

  2. 从字符转换为数字数据帧。这个帖子
    不考虑十进制值。

  3. 如何将包含十进制数的因子列转换为
    numeric?
    。这仅适用于数据框架中的一列。


解决方案

您可能需要做一些检查。您不能将因素直接转换为数字。必须首先应用 as.character 。否则,这些因素将被转换为其数值存储值。我会用 is.factor 检查每列,然后根据需要强制转换为数字。

  df1 []<  -  lapply(df1,function(x){
if(is.factor(x))as.numeric(as.character(x))else x
})
sapply(df1,class)
#ab
#numericnumeric


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.

解决方案

You might need to do some checking. You cannot convert factors straight to numeric. as.character must be applied first. Otherwise the factors will be converted to their numeric storage values. I would check each column with is.factor then coerce to numeric as necessary.

df1[] <- lapply(df1, function(x) {
    if(is.factor(x)) as.numeric(as.character(x)) else x
})
sapply(df1, class)
#         a         b 
# "numeric" "numeric" 

这篇关于如何将整个数据框转换为数字,同时保留小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆