我正在使用jsonlite
加载一些JSON数据,这导致一些嵌套数据类似于下面构建的玩具data.table
dt
。我希望能够使用rbindlist
将嵌套的data.table
绑定在一起。
设置:
> dt <- data.table(a=c("abc", "def", "ghi"), b=runif(3))
> dt[, c:=list(list(data.table(d=runif(4), e=runif(4))))]
> dt
a b c
1: abc 0.2623218 <data.table>
2: def 0.7092507 <data.table>
3: ghi 0.2795103 <data.table>
使用内置到data.table
的NSE,我可以做:
> rbindlist(dt[, c])
d e
1: 0.8420476 0.26878325
2: 0.1704087 0.59654706
3: 0.6023655 0.42590380
4: 0.9528841 0.06121386
5: 0.8420476 0.26878325
6: 0.1704087 0.59654706
7: 0.6023655 0.42590380
8: 0.9528841 0.06121386
9: 0.8420476 0.26878325
10: 0.1704087 0.59654706
11: 0.6023655 0.42590380
12: 0.9528841 0.06121386
这正是我所期望/想要的。此外,原始的dt
保持未修改:
> dt
a b c
1: abc 0.2623218 <data.table>
2: def 0.7092507 <data.table>
3: ghi 0.2795103 <data.table>
但是,当在函数中操作data.table
时,我通常希望使用get
和字符串列名:
> rbindlist(dt[, get("c")])
V1 V2
1: 0.8420476 0.26878325
2: 0.1704087 0.59654706
3: 0.6023655 0.42590380
4: 0.9528841 0.06121386
5: 0.8420476 0.26878325
6: 0.1704087 0.59654706
7: 0.6023655 0.42590380
8: 0.9528841 0.06121386
9: 0.8420476 0.26878325
10: 0.1704087 0.59654706
11: 0.6023655 0.42590380
12: 0.9528841 0.06121386
现在列名已经丢失,并被默认的"V1"one_answers"V2"值所取代。有办法保留这些名字吗?
在开发版本(v1.9.5)中,问题比简单地丢失名称更严重。在执行语句:rbindlist(dt[, get("c")])
之后,整个data.table
都损坏了:
> dt
Error in FUN(X[[3L]], ...) :
Invalid column: it has dimensions. Can't format it. If it's the result of data.table(table()), use as.data.table(table()) instead.
要清楚,丢失的名称问题发生在v1.9.4(从CRAN安装)和v1.9.5(从github安装),但腐败的data.table
问题似乎只影响v1.9.5(截至今天- 2015年7月8日)。
如果我能够坚持使用NSE版本的东西,一切都运行得很顺利。我的问题是,坚持使用NSE版本将涉及编写多个相互调用的NSE函数,这似乎很快就会变得混乱。
是否有任何(非基于nse的)已知的解决方案?另外,这是已知的问题吗?
这个问题必须在过去的5年里被修复。现在我得到了预期的结果。
> library(data.table)
data.table 1.13.3 IN DEVELOPMENT built 2020-11-17 18:11:47 UTC; jan using 4 threads (see ?getDTthreads). Latest news: r-datatable.com
> dt <- data.table(a=c("abc", "def", "ghi"), b=runif(3))
> dt[, c:=list(list(data.table(d=runif(4), e=runif(4))))]
> dt
a b c
1: abc 0.2416624 <data.table[4x2]>
2: def 0.0222938 <data.table[4x2]>
3: ghi 0.3510681 <data.table[4x2]>
> rbindlist(dt[, c])
d e
1: 0.5485731 0.32366420
2: 0.5457945 0.45173251
3: 0.6796699 0.03783026
4: 0.4442776 0.03121024
5: 0.5485731 0.32366420
6: 0.5457945 0.45173251
7: 0.6796699 0.03783026
8: 0.4442776 0.03121024
9: 0.5485731 0.32366420
10: 0.5457945 0.45173251
11: 0.6796699 0.03783026
12: 0.4442776 0.03121024
> rbindlist(dt[, get("c")])
d e
1: 0.5485731 0.32366420
2: 0.5457945 0.45173251
3: 0.6796699 0.03783026
4: 0.4442776 0.03121024
5: 0.5485731 0.32366420
6: 0.5457945 0.45173251
7: 0.6796699 0.03783026
8: 0.4442776 0.03121024
9: 0.5485731 0.32366420
10: 0.5457945 0.45173251
11: 0.6796699 0.03783026
12: 0.4442776 0.03121024
> dt
a b c
1: abc 0.2416624 <data.table[4x2]>
2: def 0.0222938 <data.table[4x2]>
3: ghi 0.3510681 <data.table[4x2]>