不能将 dput 用于 R 中的 data.table [英] Cannot use dput for data.table in R
问题描述
我有以下 data.table 我不能使用 dput 命令的输出来重新创建它:
I have following data.table for which I cannot use output of dput command to recreate it:
> ddt
Unit Anything index new
1: A 3.4 1 1
2: A 6.9 2 1
3: A1 1.1 1 2
4: A1 2.2 2 2
5: B 2.0 1 3
6: B 3.0 2 3
>
>
> str(ddt)
Classes ‘data.table’ and 'data.frame': 6 obs. of 4 variables:
$ Unit : Factor w/ 3 levels "A","A1","B": 1 1 2 2 3 3
$ Anything: num 3.4 6.9 1.1 2.2 2 3
$ index : num 1 2 1 2 1 2
$ new : int 1 1 2 2 3 3
- attr(*, ".internal.selfref")=<externalptr>
- attr(*, "sorted")= chr "Unit" "Anything"
>
>
> dput(ddt)
structure(list(Unit = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("A",
"A1", "B"), class = "factor"), Anything = c(3.4, 6.9, 1.1, 2.2,
2, 3), index = c(1, 2, 1, 2, 1, 2), new = c(1L, 1L, 2L, 2L, 3L,
3L)), .Names = c("Unit", "Anything", "index", "new"), row.names = c(NA,
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x8948f68>, sorted = c("Unit",
"Anything"))
>
粘贴时出现以下错误:
> dt = structure(list(Unit = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("A",
+ "A1", "B"), class = "factor"), Anything = c(3.4, 6.9, 1.1, 2.2,
+ 2, 3), index = c(1, 2, 1, 2, 1, 2), new = c(1L, 1L, 2L, 2L, 3L,
+ 3L)), .Names = c("Unit", "Anything", "index", "new"), row.names = c(NA,
+ -6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x8948f68>, sorted = c("Unit",
Error: unexpected '<' in:
"3L)), .Names = c("Unit", "Anything", "index", "new"), row.names = c(NA,
-6L), class = c("data.table", "data.frame"), .internal.selfref = <"
> "Anything"))
Error: unexpected ')' in ""Anything")"
问题出在哪里,如何解决?感谢您的帮助.
Where is the problem and how can it be corrected? Thanks for your help.
推荐答案
问题是 dput
打印出外部指针地址(这是 data.table
使用的东西内部,并在需要时重建),你不能真正使用.
The problem is that dput
prints out external pointer address (this is something that data.table
uses internally, and will reconstruct when required), which you can't really use.
如果您手动删除 .internal.selfref
部分,它会正常工作,除了 data.table
对某些操作的一次性投诉.
If you manually cut out the .internal.selfref
part, it will work just fine, except for a one-time complaint from data.table
for some operations.
你可以在 data.table
中添加一个关于这个的 FR,但这需要修改 data.table
的基本函数,类似于 rbind
目前正在处理中.
You could add an FR to data.table
about this, but it will require modifying the base function from data.table
, similar to how rbind
is currently handled.
这篇关于不能将 dput 用于 R 中的 data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!