long/bigint/decimal equivalent datatype in R
See help(integer)
:
Note that on almost all implementations of R the range of
representable integers is restricted to about +/-2*10^9: ‘double’s
can hold much larger integers exactly.
so I would recommend using numeric
(i.e. 'double') -- a double-precision number.
Updated in 2022: This issue still stands and will unlikely ever change: integer
in R is (signed) int32_t
(and hence range limited). double
in a proper double. Package int64
aimed to overcome this by using S4 and a complex
(integer) type to give us 64 bit resolution (as in int64_t
). Package bit64
does the same by using a double
internally and many packages from data.table
to database interfaces or JSON parsers (including our RcppSimdJson
) use it. Our package nanotime
relies on it to provide int64_t
based timestamps (i.e nanoseconds since epoch). In short there is not other way. Some JSON packages stick with string representation too ("expensive", need to convert later).
Conversion of long values into double in R
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
Are the as.character() and paste() limited by the size of the numeric vales they are given?
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.
Related Topics
How Does One Reorder Columns in a Data Frame
How to Assign from a Function Which Returns More Than One Value
Group by Multiple Columns in Dplyr, Using String Vector Input
Convert the Values in a Column into Row Names in an Existing Data Frame
What Is the Purpose of Setting a Key in Data.Table
Select First and Last Row from Grouped Data
Understanding the Order() Function
Read Multiple CSV Files into Separate Data Frames
Method to Extract Stat_Smooth Line Fit
Save Multiple Ggplots Using a For Loop
Using Data.Table Package Inside My Own Package
Expand Rows by Date Range Using Start and End Date
How to Extract a Single Column from a Data.Frame as a Data.Frame