'Is there a way to use 32-bit float instead of 64-bit in R dataframes?

The issue I'm having is related to memory. I'm doing financial calculations on dollar amounts and currently my float precision is 64-bits. I'd like to reduce the precision down to at least 32-bits but so far have not found a way to specify this in R. Ideally, this would just be applied to a dataframe that has a number of columns some are ints and some are floats.

r


Solution 1:[1]

No, there is not -- at least not in 'base R' which has only one integer and numeric (floating point) type each, and their sizes are fixed.

You can inspect them (and more) via .Machine -- see help(".Machine").

Now, for your dollar amounts you could of course resort to expressing things in cents instead in which case you could integer -- which is generally half the size of numeric.

Solution 2:[2]

The following converts all numeric columns in numeric_df to float32

library(float)
library(purrr)

float32_df <- purrr::modify_if(numeric_df, is.numeric, as.float)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 iamthem