【问题标题】:Reaching memory limit in R达到 R 中的内存限制
【发布时间】:2013-05-22 13:27:48
【问题描述】:

我在 R 中遇到了一个奇怪的问题。

我有一个大的data.table dataTs1:

Classes ‘data.table’ and 'data.frame':  419172 obs. of  5 variables:
 $ TimeStamp: chr  "01MAR13:07:15:00" "01MAR13:07:16:00" "01MAR13:07:18:00" ...
 $ col1     : chr  "ALL1" "ALL1" "ALL1" "ALL1" ...
 $ col2     : int  NA NA NA NA NA NA NA NA NA NA ...
 $ col3     : int  4 4 4 4 4 4 4 4 4 4 ...
 $ col4     : int  621 810 4 4 8 1 3 1 1 1 ...

我使用fread 函数加载了这个表。

内存分配似乎没问题。

> memory.size(max=TRUE)
[1] 82.94

我尝试将第一行的类修改为 POSIX,所以我写道:

dataTs1$TimeStamp

通过这条线,我达到了 16G 的内存限制......但是当我写的时候:

test <- 1:length(dataTs1$TimeStamp)
dataTs1$TimeStamp <- test

它完美地工作,没有任何内存过载。

我对 R 很陌生,如果你能帮我弄清楚我在这里做错了什么,我将不胜感激。

谢谢


编辑:

实际上,当我没有内存过载时,有时我会收到一个奇怪的警告:

>dataTs1[,TimeStamp:=strptime(TimeStamp,"%d%b%y:%H:%M:%S")]
Warning messages:
1: In `[<-.data.table`(x, j = name, value = value) :
  Supplied 9 items to be assigned to 419172 items of column 'TimeStamp' (recycled leaving remainder of 6 items).
2: In `[<-.data.table`(x, j = name, value = value) :
  Coerced 'list' RHS to 'character' to match the column's type. Either change the target column to 'list' first (by creating a new 'list' vector length 419172 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce RHS to 'character' (e.g. 1L, NA_[real|integer]_, as.*, etc) to make your intent clear and for speed. Or, set the column type correctly up front when you create the table and stick to it, please.
> str(dataTs1)
Classes ‘data.table’ and 'data.frame':  419172 obs. of  5 variables:
 $ TimeStamp: chr  "c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N"| __truncated__ "c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N"| __truncated__ "c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N"| __truncated__ "c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N"| __truncated__ ...
 $ V6FCDSB  : chr  "ALL1" "ALL1" "ALL1" "ALL1" ...
 $ V6FCDTD  : int  NA NA NA NA NA NA NA NA NA NA ...
 $ _TYPE_   : int  4 4 4 4 4 4 4 4 4 4 ...
 $ N        : int  621 810 4 4 8 1 3 1 1 1 ...
 - attr(*, ".internal.selfref")=<externalptr> 

【问题讨论】:

  • 您使用的是什么版本的 R? strptime 曾经存在内存泄漏。
  • 您应该通过引用分配:dataTs1[,TimeStamp:=strptime(TimeStamp,"%d%b%y:%H:%M:%S")]
  • @James 我使用的是 3.0.0 版
  • @RickyBobby 我弄错了,错误是由format.POSIXlt 调用的strftimestrptime 调用单独的代码。该错误已在 2.11.1 中修复,但此处仅供参考:bugs.r-project.org/bugzilla3/show_bug.cgi?id=14267
  • @RickyBobby 您编辑中的错误是因为strptime 输出POSIXlt 对象。您可能想改用as.POSIXct

标签: r date memory memory-leaks


【解决方案1】:

data.table 不支持POSIXlt,以后也不会支持。 "The no-support for POSIXlt is set in stone"

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2017-12-10
    • 1970-01-01
    • 2019-12-28
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    相关资源
    最近更新 更多