5

I know similar questions have been asked before, however I cannot find a way to convert a factor to numeric without losing any of the decimals.

Here a brief example:

My vector looks like this:

january$pickup_latitude[1] = 40.751231

after doing:

as.numeric(as.character(january$pickup_latitude))

the result is:

january$pickup_latitude[1] = 40.75123

Is there any way in which I can preserve all the decimals even though each case has a different length?

Estefy
  • 424
  • 2
  • 6
  • 20
  • 6
    First, see what `print(as.numeric(as.character(january$pickup_latitude)), digits=15)` looks like. – Hong Ooi Jun 18 '15 at 05:25
  • 4
    R doesn't print all decimals places by default. Look at `dput(january$pickup_latitude[1])`. Also be sure you are aware of how [floating point](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) arithmetic works in general in R. If you need to keep digits exactly, then a factor/character is not a bad solution. – MrFlick Jun 18 '15 at 05:25
  • 3
    In other words, you didn't lose any precision, if I am following @MrFlick and HongOoi correctly. – Tim Biegeleisen Jun 18 '15 at 05:26
  • Correct. You don't lose any precision, but not all digits show on the screen, unless you specify them in the way Hong Ooi shows. – RHA Jun 18 '15 at 08:06

0 Answers0