是否有任何规则可以知道R在RAM中处理给定数据集(给定PC配置)时会遇到问题?
例如,我听说一个经验法则是,每个单元格应该考虑8个字节。然后,如果我对接近8GB的1.000列进行了1.000.000次观察--因此,在大多数国内计算机中,我们可能不得不将数据存储在HD中并以块的形式访问它。
以上内容正确吗?对于内存大小和使用,我们可以事先应用哪种拇指规则?我指的是足够的内存,不仅可以加载对象,还可以进行一些基本操作,比如数据整理、数据可视化和一些分析(回归)。
PS:很好地解释一下经验法则是如何工作的,所以它不仅仅是一个黑匣子。
发布于 2014-02-13 13:29:34
一些不同大小的向量的内存占用量,以字节为单位。
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
)
}
)有些值在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注意,当有大量相同字符串的重复时(但不是当它们都是唯一的)时,因素的内存占用比字符向量要小。
发布于 2014-02-13 13:29:21
经验法则对数字向量是正确的。数字向量使用40个字节存储关于向量的信息,加上向量中每个元素的8个。您可以使用object.size()函数查看以下内容:
object.size(numeric()) # an empty vector (40 bytes)
object.size(c(1)) # 48 bytes
object.size(c(1.2, 4)) # 56 bytes在你的分析中,你可能不会只是有数字向量。矩阵增长类似于向量(这是预期的,因为它们只是带有dim属性的向量)。
object.size(matrix()) # Not really empty (208 bytes)
object.size(matrix(1:4, 2, 2)) # 216 bytes
object.size(matrix(1:6, 3, 2)) # 232 bytes (2 * 8 more after adding 2 elements)Data.frames更复杂(它们比简单的向量具有更多的属性),因此它们增长得更快:
object.size(data.frame()) # 560 bytes
object.size(data.frame(x = 1)) # 680 bytes
object.size(data.frame(x = 1:5, y = 1:5)) # 840 bytes内存的一个很好的参考是Hadley Wickhams 高级R程序设计。
所有这一切,请记住,为了在R中进行分析,您需要在内存中有一些缓冲,以允许R复制您可能正在处理的数据。
发布于 2014-02-13 13:28:18
我不能完全回答您的问题,我强烈怀疑有几个因素会影响实际工作的内容,但是如果您只是查看给定数据集的单个副本所占用的原始内存数量,您可以查看R内件文档。
您将看到所需的内存量取决于所保存的数据类型。如果您正在谈论数字数据,这些数据通常是integer或numeric/real数据。R内部类型INTSXP和REALSXP分别描述了这些术语,描述如下:
INTSXPlength,truelength,后面是一个Cint块(在所有R平台上都是32位)。REALSXPlength,truelength后面跟着一个Cdouble的块
double的长度为64位(8字节),因此对于独占包含numeric值的数据集,您的“经验法则”似乎大致正确。同样,对于整数数据,每个元素将占用4个字节。
https://stackoverflow.com/questions/21754319
复制相似问题