简体   繁体   English

有没有办法在 R 数据帧中使用 32 位浮点数而不是 64 位浮点数?

[英]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.我遇到的问题与 memory 有关。我正在对美元金额进行财务计算,目前我的浮点精度是 64 位。 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.我想将精度降低到至少 32 位,但到目前为止还没有找到在 R 中指定它的方法。理想情况下,这只会应用于 dataframe,它有许多列,有些是整数,有些是花车。

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. 不,没有 - 至少没有'base R',每个只有一个integernumeric (浮点)类型,它们的大小是固定的。

You can inspect them (and more) via .Machine -- see help(".Machine") . 您可以通过.Machine检查它们(以及更多) - 请参阅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 . 现在,对于你的美元金额,你当然可以用美分表示事物而不是在这种情况下你可以integer - 这通常是numeric一半。

The following converts all numeric columns in numeric_df to float32以下将numeric_df中的所有数字列转换为 float32

library(float)
library(purrr)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM