R中数据集的内存大小的经验法则

Car*_*lli 5 ram r data-analysis bigdata

有什么经验法则可以知道R何时会遇到问题来处理RAM中的给定数据集(给定PC配置)?

例如,我听说一个经验法则是你应该为每个单元格考虑8个字节.然后,如果我有1.000.000的1.000列的观察值接近8 GB - 因此,在大多数国内计算机中,我们可能必须将数据存储在HD中并以块的形式访问它.

以上是正确的吗?我们可以事先申请哪种内存大小和使用规则?我的意思是,不仅要加载对象,还要做一些基本的操作,如一些数据整理,一些数据可视化和一些分析(回归).

PS:解释经验法则如何工作会很好,所以它不仅仅是一个黑盒子.

Ric*_*ton 5

一些不同大小的向量的内存占用,以字节为单位。

n <- c(1, 1e3, 1e6)
names(n) <- n
one_hundred_chars <- paste(rep.int(" ", 100), collapse = "")

sapply(
  n,
  function(n)
  {
    strings_of_one_hundred_chars <- replicate(
      n,
      paste(sample(letters, 100, replace = TRUE), collapse = "")
    )
    sapply(
      list(
        Integers                                 = integer(n),
        Floats                                   = numeric(n),
        Logicals                                 = logical(n),
        "Empty strings"                          = character(n),
        "Identical strings, nchar=100"           = rep.int(one_hundred_chars, n),
        "Distinct strings, nchar=100"            = strings_of_one_hundred_chars,
        "Factor of empty strings"                = factor(character(n)),
        "Factor of identical strings, nchar=100" = factor(rep.int(one_hundred_chars, n)),
        "Factor of distinct strings, nchar=100"  = factor(strings_of_one_hundred_chars),
        Raw                                      = raw(n),
        "Empty list"                             = vector("list", n)
      ),
      object.size
    )
  }
)
Run Code Online (Sandbox Code Playgroud)

有些值在 64/32 位 R 下有所不同。

## Under 64-bit R
##                                          1   1000     1e+06
## Integers                                48   4040   4000040
## Floats                                  48   8040   8000040
## Logicals                                48   4040   4000040
## Empty strings                           96   8088   8000088
## Identical strings, nchar=100           216   8208   8000208
## Distinct strings, nchar=100            216 176040 176000040
## Factor of empty strings                464   4456   4000456
## Factor of identical strings, nchar=100 584   4576   4000576
## Factor of distinct strings, nchar=100  584 180400 180000400
## Raw                                     48   1040   1000040
## Empty list                              48   8040   8000040

## Under 32-bit R
##                                          1   1000     1e+06
## Integers                                32   4024   4000024
## Floats                                  32   8024   8000024
## Logicals                                32   4024   4000024
## Empty strings                           64   4056   4000056
## Identical strings, nchar=100           184   4176   4000176
## Distinct strings, nchar=100            184 156024 156000024
## Factor of empty strings                272   4264   4000264
## Factor of identical strings, nchar=100 392   4384   4000384
## Factor of distinct strings, nchar=100  392 160224 160000224
## Raw                                     32   1024   1000024
## Empty list                              32   4024   4000024
Run Code Online (Sandbox Code Playgroud)

请注意,当同一字符串有很多重复时(但不是当它们都是唯一的时),因子的内存占用量比字符向量要小。