R starts changing numbers (numeric) after the 16th digit

Hi,

I've tried to search StackOverflow and on here. I've also tried my hand at the R documentation but seem to find no answer to a seemingly basic question. I'm not sure if I'm looking in the right places.

R seems to be capable of handling numeric digits only up until 16 digits. After that it starts changing the numbers. I suspect it is because R understands it as a particular data type (e.g. float vs int ) . Is there a way around this besides converting to character? My use case is a bunch of numeric ids that are imported from a database.

options(scipen=999)
1111111111111111
#> [1] 1111111111111111
11111111111111111
#> [1] 11111111111111112
111111111111111111
#> [1] 111111111111111104

Created on 2022-05-25 by the reprex package (v2.0.1)

If you are dealing with IDs, then convert them to characters. This will also preserve leading zeroes if you have them.

1 Like

Following library may help

library(gmp)

x <- as.bigz("11111111111111111")
# Big Integer ('bigz') :
#   [1] 11111111111111111

y <- as.bigz("11111111111111111")
# Big Integer ('bigz') :
#   [1] 11111111111111111

x+y
# Big Integer ('bigz') :
#   [1] 22222222222222222

x-y
# Big Integer ('bigz') :
#   [1] 0

x*y
# Big Integer ('bigz') :
#   [1] 123456790123456787654320987654321

x==y
# TRUE

x<x+y
# TRUE

This was my initial thought. These Ids are imported via RMySQL as output from a stored procedure. I was hoping to avoid touching the procedure.

If there is a way to convert to string before R sees it as numeric that would be a solution.

I don't know specifically about RMySQL, but you should be able to preserve the data types from the database such that characters are not converted to numbers.

This package supercedes RMySQL:
GitHub - r-dbi/RMariaDB: An R interface to MariaDB

These packages may help you with interfacing R and your database:

Introduction to DBI • DBI (r-dbi.org)

A dplyr backend for databases • dbplyr (tidyverse.org)

This didn't solve my problem directly, but I found this super useful elsewhere. Thanks!

This is marked as the solution because I ended up going through a RestAPI (the spec for the project changed) and extracting the ids as string straight from the JSON response.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.