Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:如何将因子向量转换为数值(其他问题的建议不起作用)_R_Type Conversion - Fatal编程技术网

R:如何将因子向量转换为数值(其他问题的建议不起作用)

R:如何将因子向量转换为数值(其他问题的建议不起作用),r,type-conversion,R,Type Conversion,我试图将因子向量转换为数值,而不改变数值 我查了一下,发现了一些函数,比如 bonds1 <- as.numeric(as.character(bonds$price)) Warning message: NAs introduced by coercion bonds1 <- as.numeric(levels(bonds$price)[bonds$price]) Warning mes

我试图将因子向量转换为数值,而不改变数值

我查了一下,发现了一些函数,比如

         bonds1 <- as.numeric(as.character(bonds$price)) 
         Warning message:
         NAs introduced by coercion 

         bonds1 <- as.numeric(levels(bonds$price)[bonds$price])
         Warning message:
         NAs introduced by coercion  

bonds1您可以使用
gsub()删除逗号。


就像@HurbtL提到的,似乎值是不可转换的

检查这些答案,了解如何避免此警告

用逗号解析数字:

N1 N2作为数值(N2)
[1]  1000  1000 10000 10000
超级警告


@HubertL该栏是债券的价格。它看起来是这样的
10045.26,10030.25….
我不知道为什么这些价格不能转换成数字..是逗号造成了警告。@DeshanR-ahh解决了,谢谢!
         bonds$price <- as.numeric(gsub(",","",bonds$price))
as.numeric(gsub(",","",as.character(factor(c("1,000.23","2.2", "one")))))
[1] 1000.23    2.20      NA
Warning message:
NAs introduced by coercion 
N1 <- c("1000", "1,000", "10000", "10,000")
as.numeric(N1)
##
[1]  1000    NA 10000    NA
Warning message:
NAs introduced by coercion
##
> N2 <- gsub(",","",N1)
> as.numeric(N2)
[1]  1000  1000 10000 10000