用逗号和百分数表示数值 [英] Factor with comma and percentage to numeric

查看:79
本文介绍了用逗号和百分数表示数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列(费率),它是因素,具有多个级别,例如:

I have a column ("rates")which is a factor with several levels such as:


16级:-0,186%-0,229%-0,326%...

16 Levels: -0,186% -0,229% -0,326% ...

当我尝试转换时到数字,引入了NA,我不知道该怎么做。

When I try to convert it to numeric, NAs are introduced and I can't figure out how to do it properly.

rates=as.numeric(gsub(",", ".", rates))
rates=as.numeric(sub("%", "e-2", rates))

我也尝试了以下方法,这是对类似问题的答案,但它不起作用要么。
rates = as.numeric(gsub( \\%,,rate))

I also tried the following, which was the answer to a similar question, but it does not work either. rates=as.numeric(gsub("\\%", "", rates))

推荐答案

另一种选择是使用读取器 parse_number 函数c> -package并指定逗号用作小数点:

Another option is to use the parse_number-function from the readr-package and specify that a comma is used as decimal mark:

library(readr)
parse_number(rates, locale = locale(decimal_mark = ','))

给出:


[1] -0.186 -0.229 -0.326







使用的数据:


Used data:

rates <- as.factor(c("-0,186%", "-0,229%", "-0,326%"))

这篇关于用逗号和百分数表示数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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