如何在不更改值的情况下将 R 中的因子格式转换为数字格式? [英] How to convert factor format to numeric format in R without changing the values?

查看:69
本文介绍了如何在不更改值的情况下将 R 中的因子格式转换为数字格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是数据框 df1,我想在不更改当前值 (0 ; 0 ; 8,5 ; 3) 的情况下将列V2"从因子格式转换为数字.

Below is dataframe df1 of which I want to convert column "V2" from factor format to numeric without changing the current values (0 ; 0 ; 8,5 ; 3).

df1=

             V1  V2 V3       X2 X3
4470 2010-03-28   0  A 21.53675  0
4471 2010-03-29   0  A 19.21611  0
4472 2010-03-30 8,5  A 21.54541  0
4473 2010-03-31   3  A       NA NA

由于列V2"是因子格式,我首先将其转换为字符格式:df1[,2]=as.character(df1[,2])

Since column "V2" is in factor format I first convert it to character format: df1[,2]=as.character(df1[,2])

然后我尝试将V2"转换为数字格式:

Then I try to convert "V2" to numeric format:

df1[,2]=as.numeric(df1[,2])

导致此 R 消息:

警告信息:强制引入的 NAs

下面的数据框,其中 df[3,2] 已更改为NA"而不是保留为8,5"..

And the dataframe below where df[3,2] has changed into "NA" instead of remaining "8,5"..

             V1 V2 V3       X2 X3
4470 2010-03-28  0  A 21.53675  0
4471 2010-03-29  0  A 19.21611  0
4472 2010-03-30 NA  A 21.54541  0
4473 2010-03-31  3  A       NA NA 

这可能与 8,5 不是整数这一事实有关.我仍然不知道如何解决这个问题.将不胜感激!

It might have to do with the fact that 8,5 is not a whole number. Still I do not know how to solve this problem. Help would be much appreciated!

推荐答案

用点替换逗号,点代表 R 中的小数.否则 R 认为它是一个字符并将值强制转换为 NA.

Replace comma's with dots, which represent decimals in R. Otherwise R thinks it is a character and coerces the value to NA.

然后,提取值:

as.numeric(levels(df1[,2])[df[,2]])

(感谢@SimonO101 的更正)

(thanks @SimonO101 for the correction)

这篇关于如何在不更改值的情况下将 R 中的因子格式转换为数字格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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