-1

I'm assigning the value below in R to var x which I read from a file and when I prints it, it shows a double format. I want to be able to store this long number without decimal because when I do a REST post the data gets posted as double. I've tried as.character(x), but then when doing POST to REST API it is treated as a string. Is there any way to keep this value as Long and not decimal?

x <- 1426643216897

print(x)
[1] 1.426643e+12

Thranks

codeBarer
  • 2,238
  • 7
  • 44
  • 75
  • possible duplicate of [long/bigint/decimal equivalent datatype in R](http://stackoverflow.com/questions/2053397/long-bigint-decimal-equivalent-datatype-in-r) – Gregor Thomas Mar 20 '15 at 17:23
  • The nominated duplicate was written prior to the introduction of "semi-long integers" so isn't currently the best answer to link to. I also think it's more related to the input side of numeric/integer storage and this one is focused on the output. – IRTFM Mar 20 '15 at 18:01

2 Answers2

3

I'd suggest using quote=FALSE in the print function if working with characters that you do not want enclosed. Since print also accepts a digits argument you can go either way:

> print(as.character(x), quote=FALSE)
[1] 1426643216897
> print(x,  digits=20)
[1] 1426643216897

Versus:

> print(as.character(x) )
[1] "1426643216897"

There is no R long-integer mode. You should understand that numbers with more than 9 base-10 digits are being "stored as decimals", i.e. stored with abscissa+mantissa, but the apparent increase of integer length is accomplished through printing of the exact conversion of the abscissa of the "double" to base-10 representation without the decimal point. Notice what happens if you explicitly attempt to "store as integer":

> x <- 1426643216897L
Warning message:
non-integer value 1426643216897L qualified with L; using numeric value 

If you needed to store a number with greater length than the 53 binary digits could handle, you would need to go with character storage, and then use the quote=FALSE option or use cat for output:

>  cat("test")
test
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Hi @BondedDust, if I wanted to apply x <- 1426643216897L to a value that I read from a file, how would I do it? – codeBarer Mar 20 '15 at 18:44
  • Not sure what you mean. Is `1426643216897L` the contents of the file, or do you want to read in `x <- 1426643216897L` as R code? Possible useful help pages for options 1 or 2 would be `?readlines` or `?source`. – IRTFM Mar 20 '15 at 18:46
  • 1426643216897 is content of a file. I want to do something like a cast by adding L at the end. – codeBarer Mar 20 '15 at 18:48
  • 1
    There is no point in appending "L". It's going to become a double anyway. Nothing helpful would result from that action. If you insist that it become an integer with `as.integer`, then NA would be the result. And note that I misspelled `?readLines` above but just missed my 5 min editing window. – IRTFM Mar 20 '15 at 18:51
0

Set the digits option first in your R environment:

options(digits=22)

tcash21
  • 4,880
  • 4
  • 32
  • 39
  • Even when you set the digits and try and write the value to the file it writes it as 1.4266e+12 rather than 1426643216897 – codeBarer Mar 20 '15 at 18:36