4

I have four 20 digits characters.
I want to convert characters to numeric.
I just converted using as.numeric function.
But four characters all same.
Why?
I need numeric. Is there a way?

tmp <- c("11240690100051000001", "11240690100051000002", "11240690100051000003", "11240690100051000004")
tmp1 <- as.numeric(tmp)

tmp1[1] == tmp1[2]
[1] TRUE
tmp1[1] == tmp1[3]
[1] TRUE
tmp1[1] == tmp1[4]
[1] TRUE
Rokmc1050
  • 463
  • 1
  • 6
  • 16
  • This discussion might be helpful: http://stackoverflow.com/questions/2287616/controlling-digits-in-r – Ruthger Righart Feb 26 '15 at 12:10
  • 2
    I think you might have hit the limit of numerical precision in R, hence all the numbers are close enough that they are 'equal', e.g. `1.0000000000000001==1` – thelatemail Feb 26 '15 at 12:21
  • 1
    Conversion to scalars (with `as.numeric()`) hits the precion wall. Adding the digits does not rectify the situation. And converting into integers does not work either. If you need to store large integers, please see the package Brobgingnag and this thread: http://stackoverflow.com/questions/2053397/long-bigint-decimal-equivalent-datatype-in-r. –  Feb 26 '15 at 12:25
  • 1
    I mean package Brobdingnag. Typing it about 10 times makes it perfect... –  Feb 26 '15 at 12:31
  • `library("int64")` helps a little bit, but not enough: `as.int64("1000000000000000000")` works, but not one more digit – Ben Bolker Feb 26 '15 at 14:58

1 Answers1

3

You can use the Rmpfr package:

tmp <- c("11240690100051000001", 
 "11240690100051000002", "11240690100051000003", 
"11240690100051000004")
library("Rmpfr")
as.bigz(tmp)
##  Big Integer ('bigz') object of length 4:
## [1] 11240690100051000001 11240690100051000002 11240690100051000003
## [4] 11240690100051000004

... but this may limit severely what you can subsequently do with these values. More context in the question about why you need these as numeric/what you plan to do with them might give lead to more helpful answers ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453