-1
[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.I use "as.numeric" in r, to change the data type, but it doesn't work

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
Xichen Yao
  • 15
  • 2

1 Answers1

2

Sometimes introducing as.character() in a class coercion chain seems to rectify such problems. I think the reason why your code exhibits this behaviour is because under the hood (mode-wise), factor vectors are numeric vectors that go from 1 to n where n is the number of levels.

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

> x <- as.character(as.factor("43.0678901152949"))
> as.numeric(x)
[1] 43.06789
12b345b6b78
  • 995
  • 5
  • 16