【问题标题】:data.table making a copy of table in Rdata.table 在 R 中制作表的副本
【发布时间】:2013-07-31 07:24:59
【问题描述】:

我正在这样做:

myfun <- function(inputvar_vec){
# inputvar_vec is input vector
# do something
# result = output vector
return(result)
}

DT[, result := lapply(.SD, myfun), by = byvar, .SDcols = inputvar]

我收到以下警告:

Warning message:
`In `[.data.table`(df1, , `:=`(prop, lapply(.SD, propEventInLastK)),  :
Invalid .internal.selfref detected and fixed by taking a copy of the whole table, 
so that     := can add this new column by reference. At an earlier point, this 
data.table has been copied by R (or been created manually using structure() 
or similar). (and then some more stuff) .... `

我的猜测是因为我正在堆叠result 向量(在通过操作之后),正在制作副本?

任何人都可以建议一种方法来消除此警告吗?我已经使用 apply 函数完成了这项工作,并认为它也应该可以在这里扩展。

我的另一个问题是:您可以从数据框中传递一大块行(通过使用 by 语句进行子集),然后调用函数 myfun 对其进行操作吗?

按要求添加示例

# generate data
N = 10000
default=NA
value = 1
df = data.table(id = sample(1:5000, N, replace=TRUE),
                trial = sample(c(0,1,2), N, replace=TRUE),
                ts = sample(1:200, N, replace=TRUE))

#set keys
setkeyv(df, c("id", "ts"))

df[["trial"]] = as.numeric(df[["trial"]]==value)

testfun <- function(x){
  L=length(x)
  x = x[L:1]
  x = fts(data=x)
  y = rep(default, L)
  if(L>=K){
    y1 = as.numeric(moving.sum(x,K))
    y = c(y1, rep(default,L-length(y1)))
  } 
  return(y[L:1]/K)
}

df[, prop:= lapply(.SD, testfun), by = id, .SDcols = "trial"]

仍然收到相同的警告消息:

Warning message:
In `[.data.table`(df, , `:=`(prop, lapply(.SD, testfun)), by = id,  :
  Invalid .internal.selfref detected and fixed by taking a copy of the whole table, so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or been created manually using structure() or similar). Avoid key<-, names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: setkey(), setnames() and setattr(). Also, list(DT1,DT2) will copy the entire DT1 and DT2 (R's list() copies named objects), use reflist() instead if needed (to be implemented). If this message doesn't help, please report to datatable-help so the root cause can be fixed.

【问题讨论】:

  • 你是如何创建data.table的? do something 到底是什么?请提供reproducible example
  • 道歉。它是一个包含数百万行的数据集。请给我一些时间。我将很快发布一个可重现的示例。数据表创建为:DT = data.table(readRDS(fileAsDataFrame))
  • 您是否尝试过DF &lt;- readRDS(fileAsDataFrame); DT &lt;- data.table(DF) 来排除此步骤造成的问题?
  • @Roland - 是的。这并没有解决问题。

标签: r data.table lapply


【解决方案1】:

问题出现在

df[["trial"]] = as.numeric(df[["trial"]]==value)

这不是data.table 方法

data.table 方法是使用:=

 df[, trial := as.numeric(trial == value)]

应该避免这个问题。

了解复制的原因(因此内部自我引用可能无效)请参阅Understanding exactly when a data.table is a reference to (vs a copy of) another data.table

重要的是要认识到data.tables 没有[[&lt;- 方法,因此会调用[[&lt;-.data.frame,这将复制整个对象,而且不会做任何data.table 那样的小心操作方法(例如[&lt;-.data.table)确实(返回一个有效的data.table

【讨论】:

  • 是的,这解决了问题。非常感谢你。我仍然以列表格式看到它,这就是我努力摆脱基于列表的命令的原因。猜猜必须改变这一点。
  • 您能否指出一些可以帮助我理解为什么出现复制警告的内容?该声明是否被解释为:.internal.selfref
  • @user1971988 查看我的编辑并阅读包装小插曲——它们非常出色且富有启发性
猜你喜欢
  • 1970-01-01
  • 2021-05-25
  • 2021-11-18
  • 2013-04-22
  • 2018-06-01
  • 1970-01-01
  • 2016-01-29
  • 2017-07-03
  • 2013-01-10
相关资源
最近更新 更多