如果data.table是继承的类,则失败

时间:2019-07-09 14:57:32

标签: r oop data.table

在将继承结构与data.table一起使用时,我遇到了一些困难。当我将新的子类应用于data.table对象时,该对象在调用执行data.table操作时无法还原为data.table类。这是一个示例:

library('data.table')

dat <- data.table(x = 1:10, y = LETTERS[1:10])
dat <- structure(dat, class = c('new_class', 'data.table'))

dat[, .(x)]
dat[, z:= 5 * x]

上面两个data.table操作中的每一个都返回以下错误消息:

Error in setnames(x, value) : x is not a data.table or data.frame
Error in setnames(x, value) : x is not a data.table or data.frame
In addition: Warning message:
In `[.data.table`(dat, , `:=`(z, 5 * x)) :
  Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or was created manually using structure() or similar). Avoid names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. If this message doesn't help, please report your use case to the data.table issue tracker so the root cause can be fixed or this message improved.

似乎没有引用data.table类来执行.:=方法。为什么会这样?

0 个答案:

没有答案