12
> 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?

teGuy
  • 215
  • 1
  • 4
  • 9

2 Answers2

27

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))
Community
  • 1
  • 1
Arun
  • 116,683
  • 26
  • 284
  • 387
2

The documentation at ?factor states:

To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).

So the following works as well:

as.numeric(levels(my_vec))[my_vec]
Blue Magister
  • 13,044
  • 5
  • 38
  • 56