从刚刚从磁盘加载的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