0

I'm running into some problems with the R function as.character() and paste(): they do not give back what they're being fed...

as.character(1415584236544311111)
## [1] "1415584236544311040"

paste(1415584236544311111)
## [1] "1415584236544311040"

what could be the problem or a workaround to paste my number as a string?

update

I found that using the bit64 library allowed me to retain the extra digits I needed with the function as.integer64().

  • 1
    Why not just `"1415584236544311111"`? – Sven Hohenstein Nov 12 '14 at 21:16
  • 2
    It's not `as.character` or `paste`. That number is too big to be represented exactly as an integer by your computer. – joran Nov 12 '14 at 21:21
  • then, how can I force R to store it a double or a float? – not.so.quanty Nov 12 '14 at 21:23
  • 1
    That _is_ what it's doing. It's still too big. If you really want to deal with numbers that large exactly, you'll need some sort of arbitrary precision arithmetic package (there are several, I believe). – joran Nov 12 '14 at 21:24
  • ok, good hint, thanks joran. – not.so.quanty Nov 12 '14 at 21:26
  • 1
    Where are your big integers coming from? If you're just trying to enter them manually, then enter them as character strings. If they come from elsewhere, then you need to figure out how they were able to be represented in the first place. – Carl Witthoft Nov 12 '14 at 21:42

1 Answers1

1

Remember that numbers are stored in a fixed number of bytes based upon the hardware you are running on. Can you show that your very big integer is treated properly by normal arithmetic operations? If not, you're probably trying to store a number to large to store in your R install's integer # of bytes. The number you see is just what could fit.

You could try storing the number as a double which is technically less precise but can store larger numbers in scientific notation.

EDIT

Consider the answers in long/bigint/decimal equivalent datatype in R which list solutions including arbitrary precision packages.

Community
  • 1
  • 1
mobiusklein
  • 1,403
  • 9
  • 12
  • Poor answer-- I would advise instead to use one of the arbitrary precision packages. – Carl Witthoft Nov 12 '14 at 21:41
  • I agree, I should have done more research. I don't work with arbitrary precision types routinely. I've linked a relevant thread which lists three options. – mobiusklein Nov 12 '14 at 22:31