Converting large integers from R to PostgreSQL without scientific notation

Hey everyone, I’m having trouble with R and PostgreSQL. When I try to write big numbers (integer64) to my database, they often end up in scientific notation. For instance, 99999679872 becomes 1.9277322e-315. This happens about 90% of the time.

I’ve tested this on two setups:

  1. Windows 64-bit with R 3.5
  2. Ubuntu 64-bit with R 3.3

Does anyone know a fix for this? Maybe there’s a different library I could use or some setting I’m missing? I really need these numbers to stay in their original format when they hit the database.

Any help would be awesome. Thanks!

I’ve wrestled with this issue in my data analysis work. One effective solution I found was using the format function in R with scientific = FALSE. For example:

format(99999679872, scientific = FALSE)

This preserves the full numeric representation. Another trick that worked for me was setting options(scipen = 999) at the start of my R script. This globally discourages scientific notation.

If those don’t cut it, you might consider using a custom SQL function in PostgreSQL to handle the conversion on the database side. This offloads the work from R and ensures consistency.

Remember to thoroughly test whichever method you choose with your specific dataset. Different approaches can have varying performance impacts depending on your data volume and structure.

I encountered this issue before and found that using the bit64 package in R significantly helped address the problem. It provides improved support for 64-bit integers, which in my experience prevented numbers from being converted into scientific notation.

Instead of using numbered lists, the solution was to install and load the bit64 package and then convert large numbers to the integer64 type before writing them to PostgreSQL. Additionally, I found that using the DBI package for managing database connections handled large integers more reliably. It is also important to ensure that your PostgreSQL column is defined as bigint so that it can properly accommodate these values. If these steps do not resolve the issue, another approach is to convert the numbers to strings before writing them to the database and then convert them back to integers during retrieval. This method has worked well in my experience.

hey there! i’ve dealt with this before. try using the as.character() function in R before sending the numbers to PostgreSQL. it converts the integers to strings, preserving the original format. then u can convert them back to integers in PostgreSQL if needed. hope this helps!