r - data.table 在 R 中复制表

标签 r data.table lapply

我正在这样做:

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 向量(在 by 操作之后),正在制作副本?

谁能建议一种方法来消除此警告?我已经使用 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.

最佳答案

问题出现在

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.table因此[[<-.data.frame被调用,它将复制整个对象,而且不会做 data.table 那样的任何小心的事情。方法(例如 [<-.data.table )确实(返回有效的 data.table

关于r - data.table 在 R 中复制表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17964112/

相关文章:

r - 是否有解决方法来处理 data.table 中以数字开头的列?

r - 满足条件时跟踪最后一次观察的组特定变量

r - 在 R dplyr 中,为什么在 count() 之后需要 ungroup() ?

r - 如何使用应用函数(包括 ggplot2)组合多个 R 函数

r - 根据条件使用另一个值的值更新数据框中的整个列

r - 将列名称作为点-点-点传递,以在 qplot() 上进行非标准评估

r - ggplot2 在使用 scale_x_sqrt 时删除零

r - 如何将数据框变量名移动到第一行并将新变量名添加到列表中的多个数据框?

r - 使用 R 按组对数据进行标准化/白化/重新缩放

R通过lapply命令从乘法回归中提取回归系数