gpt4 book ai didi

r - "Invalid .internal.selfref"警告 不涉及调用列表()

转载 作者:行者123 更新时间:2023-12-02 03:24:28 27 4
gpt4 key购买 nike

为什么我会收到烦人的警告?

initialize = function() {
table = data.table(1:10)
colnames(table) <- "old.col"
table
}
dt <- initialize()
dt[, new.col := 5]

最佳答案

警告消息会告诉您所需的一切:

Warning message: In [.data.table (dt, , `:=`(new.col, 5)) :
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 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: ?set, ?setnames and ?setattr. Also, in R<=v3.0.2, list(DT1,DT2) copied the entire DT1 and DT2 (R's list() used to copy named objects); please upgrade to R>v3.0.2 if that is biting. If this message doesn't help, please report to datatable-help so the root cause can be fixed.

.internal.selfref指针指的是data.table在内存中的位置。 。使用key<- , names<-attr<-显然会导致 R 复制 data.table它需要在内存中的另一个位置。

所以,不要使用 colnames你应该使用setnames :

initialize = function() {
table = data.table(1:10)
setnames(table,"V1","old.col")
table
}
dt <- initialize()
dt[, new.col := 5]

现在您不会收到警告,因为 data.table通过引用进行更新,无需制作副本,从而保持相同 .internal.selfref指向内存中位置的指针。

关于r - "Invalid .internal.selfref"警告 不涉及调用列表(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34413568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com