Sunday, 3 September 2017

What's wrong with as.numeric in R?





> X864291X8X74
[1] 8.0000000000 9.0000000000 10.0000000000 6.0000000000 8.0000000000
10 Levels: 0.0000000000 10.0000000000 12.0000000000 3.0000000000 4.0000000000 6.0000000000 ... NULL

> as.numeric(X864291X8X74)

[1] 8 9 2 6 8


what did I misunterstood? shouldn't be the result of as.numeric 8 9 10 6 8?




How do I get the correct result?


Answer



Your vector is a factor. This question has been asked quite a few times, ex: here, here, here. In order to convert a factor to numeric, you'll have to convert to character first. Try:



as.numeric(as.character(my_vec))

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...