Factor with comma and percentage to numeric

2020-05-07 01:55发布

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

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

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))

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

3条回答
神经病院院长
2楼-- · 2020-05-07 02:12

I assume the levels of your initial factor are chars. Then you need to do both replacements at the same time:

rates=as.numeric(gsub(",", ".", gsub("%", "e-2", rates)))
查看更多
倾城 Initia
3楼-- · 2020-05-07 02:13

Use gsub:

# Example vector
vec <- as.factor(c("-0,186%", "-0,229%", "-0,326%"))

# Convert vector to numeric
vec <- as.numeric(gsub(",", ".", gsub("%", "", as.character(vec))))
查看更多
戒情不戒烟
4楼-- · 2020-05-07 02:28

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 = ','))

which gives:

[1] -0.186 -0.229 -0.326

Used data:

rates <- as.factor(c("-0,186%", "-0,229%", "-0,326%"))
查看更多
登录 后发表回答