What is 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 misunderstand? Shouldn't be the result of as.numeric 8 9 10 6 8?

How to get the right result?

+7
source share
2 answers

Your vector factor . This question has been asked several times, for example: here , here , here . To convert a coefficient to a numeric one, you first need to convert to a character . Try:

 as.numeric(as.character(my_vec)) 
+20
source

The documentation in ?factor states:

To convert the coefficient f approximately the original numerical values, as.numeric(levels(f))[f] recommended and slightly more efficient than as.numeric(as.character(f)).

So the following works:

 as.numeric(levels(my_vec))[my_vec] 
+2
source

All Articles