当我使用as.numeric时,我所有的经度和纬度都变为“ 1”

时间:2018-12-01 20:55:03

标签: r

[enter image description here][1]
$ latitude : Factor w/ 2 levels "43.0678901152949",..: 1 1 1
$ longitude: Factor w/ 2 levels "-76.1734713936105",..: 1 1 1    
current data type is factor, but I want to change them to Numeric, so I use 
no3$latitude <- as.numeric(no3$latitude)
no3$longitude <- as.numeric(no3$longitude)

1。我在r中使用“ as.numeric”来更改数据类型,但这不起作用

it becomes to
[enter image description here][2]
$ latitude : num  1 1 1 1 1 1 1 1 1 1 ...
$ longitude: num  1 1 1 1 1 1 1 1 1 1 ...
[1]: https://i.stack.imgur.com/HMj9C.png
[2]: https://i.stack.imgur.com/egcsx.png

1 个答案:

答案 0 :(得分:2)

有时在类强制链中引入as.character()似乎可以纠正此类问题。我认为您的代码之所以会显示这种行为,是因为在底层(模态下),因子向量是从1到n的数字向量,其中n是级别数。

> x <- as.factor("43.0678901152949")
> as.numeric(x)
[1] 1

> x <- as.character(as.factor("43.0678901152949"))
> as.numeric(x)
[1] 43.06789