无法将因子中的值转换为仅数字

时间:2021-12-01 09:10:26

I want to convert this variable into numeric, as you can see:

我想将此变量转换为数字,如您所见:

> class(DATA$estimate)
[1] "factor"
> head(DATA$estimate)
[1] 0,253001909 0,006235543 0,005285019 0,009080499 6,580140903 0,603060006
57 Levels: 0,000263863 0,000634365 0,004405696 0,005285019 0,006235543 0,009080499 0,009700147 0,018568434 0,253001909 ... 7,790580873
> 

But when I want to convert, look what I have got

但是当我想转换时,看看我得到了什么

> DATA$estimate<-as.numeric(DATA$estimate)
> DATA$estimate
 [1]  9  5  4  6 51 12  3 53 11  8  1  7 15 27 30 29 28 31 21 23 22 39 38 37 33 26 34 52 57 50 24 18 20 10  2 55 54 56 36 32 35 44 46
[44] 48 19 25 16 43 41 40 49 42 47 14 17 13 45

It's not numeric and I don't understand how the program gives these numbers!

它不是数字,我不明白程序如何给出这些数字!

2 个解决方案

#1


4  

data:

fac <- factor(c("0,253001909" ,"0,006235543" ,"0,005285019" ,"0,009080499" ,"6,580140903" ,"0,603060006"))

I convert to character, then turn the "," into ".", then convert to numeric.

我转换为字符,然后将“,”转换为“。”,然后转换为数字。

as.numeric(sub(",",".",as.character(fac)))

in your case its:

在你的情况下:

DATA$estimate<-as.numeric(sub(",",".",as.character(DATA$estimate)))

#2


0  

You can also scan() your factor variable and specify , as decimal separator

您还可以扫描()您的因子变量并指定为小数分隔符

fac <- factor(c("0,253001909" ,"0,006235543" ,"0,005285019" ,"0,009080499" ,
                "6,580140903" ,"0,603060006"))
scan(text = as.character(fac), dec = ",")
#output
[1] 0.253001909 0.006235543 0.005285019 0.009080499 6.580140903
[6] 0.603060006

#1


4  

data:

fac <- factor(c("0,253001909" ,"0,006235543" ,"0,005285019" ,"0,009080499" ,"6,580140903" ,"0,603060006"))

I convert to character, then turn the "," into ".", then convert to numeric.

我转换为字符,然后将“,”转换为“。”,然后转换为数字。

as.numeric(sub(",",".",as.character(fac)))

in your case its:

在你的情况下:

DATA$estimate<-as.numeric(sub(",",".",as.character(DATA$estimate)))

#2


0  

You can also scan() your factor variable and specify , as decimal separator

您还可以扫描()您的因子变量并指定为小数分隔符

fac <- factor(c("0,253001909" ,"0,006235543" ,"0,005285019" ,"0,009080499" ,
                "6,580140903" ,"0,603060006"))
scan(text = as.character(fac), dec = ",")
#output
[1] 0.253001909 0.006235543 0.005285019 0.009080499 6.580140903
[6] 0.603060006