2

从刚刚从磁盘加载的:=data.table 上的函数调用时,似乎不起作用。用第一个做一些事情data.table确实有帮助。这是一个错误data.table,还是我错过了什么(即滥用 data.table 和:=)?

示例代码:

# Generate a dummy data.table and save to disk
library(data.table)
DT <- data.table(a=c(1:10), b=c(1:10))
save(DT, file="c:/temp/DT.rData")

# Now define and use a function which is supposed to change the data.table
> library(data.table)
> 
> myfn <- function(data="DT") {
+ DD <- get(x=data, inherits=TRUE, mode="list")
+ DD[, c:="a", verbose=TRUE] }
> load(file="c:/temp/DT.rData")
> myfn(data="DT")
.internal.selfref ptr is NULL. This is expected and normal for a data.table loaded from disk. If not, please report to datatable-help.
Growing vector of column pointers from truelength  0  to  102 . A shallow copy has been taken, see ?alloc.col. Only a potential issue if two variables point to the same data (we can't yet detect that well) and if not you can safely ignore this. To avoid this message you could alloc.col() first, deep copy first using copy(), wrap with suppressWarnings() or increase the 'datatable.alloccol' option.
.internal.selfref ptr is NULL. This is expected and normal for a data.table loaded from disk. If not, please report to datatable-help.
Detected that j uses these columns: <none> 
Assigning to all 10 rows
     a  b c
 1:  1  1 a
 2:  2  2 a
 3:  3  3 a
 4:  4  4 a
 5:  5  5 a
 6:  6  6 a
 7:  7  7 a
 8:  8  8 a
 9:  9  9 a
10: 10 10 a
# This seems to be the correct (desired) result, but does not "stick":
> DT
     a  b
 1:  1  1
 2:  2  2
 3:  3  3
 4:  4  4
 5:  5  5
 6:  6  6
 7:  7  7
 8:  8  8
 9:  9  9
10: 10 10

使用额外的行运行相同的代码DT <- copy(DT)确实会按预期更改 DT:

> myfn <- function(data="DT") {
+ DD <- get(x=data, inherits=TRUE, mode="list")
+ DD[, c:="a"] }
> 
> load(file="c:/temp/DT.rData")
> DT <- copy(DT)
> myfn(data="DT")
     a  b c
 1:  1  1 a
 2:  2  2 a
 3:  3  3 a
 4:  4  4 a
 5:  5  5 a
 6:  6  6 a
 7:  7  7 a
 8:  8  8 a
 9:  9  9 a
10: 10 10 a
> DT
     a  b c
 1:  1  1 a
 2:  2  2 a
 3:  3  3 a
 4:  4  4 a
 5:  5  5 a
 6:  6  6 a
 7:  7  7 a
 8:  8  8 a
 9:  9  9 a
10: 10 10 a

我在:=上面使用的方式是否存在一些基本问题?我尝试了 , 的替代定义myfn()set()但结果是一样的。谢谢。

> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.8.9
4

0 回答 0