When I run...
当我运行……
library(data.table)
DT = CJ(id = 1, id2 = 1:3)[, .(
d = as.IDate("2017-01-01")+1:10, v = id*10 + id2*1:10
), by=.(id, id2)]
plotDT = DT[, {
par(mfrow = c(uniqueN(id2), 1), mar = c(2,2,.5,2))
.SD[,{
plot(d, v, type="l")
}, by=id2]
.(p = .(recordPlot()))
}, by=id]
x11()
plotDT[id == 1L, replayPlot(p[[1]])]
The plot from DT[...]
is correct:
情节从DT[…是正确的:
While the replay is wrong:
而重播是错误的:
In the recordedplot, the final values of x
and y
are magically applied to all three graphs, probably because of how data.table handles pointers to columns defined in by=
groups.
在recordedplot中,x和y的最终值会神奇地应用到这三个图上,这可能是因为数据的缘故。表处理由=组定义的列的指针。
I'm not really sure I understand what's going on, though, because I can change the example superficially, writing v = id2*1:10
instead of v = id*10 + id2*1:10
and -- poof -- the problem disappears.
我不太确定我是否理解发生了什么,因为我可以简单地改变例子,写v = id2*1:10而不是v = id*10 + id2*1:10,然后——噗——问题就消失了。
I'd like to know (i) why is this happening and (ii) what simple tweak can I make to get around it?
我想知道(I)为什么会这样,(ii)我能做些什么简单的调整来绕过它?
1 个解决方案
#1
2
One working hack is to write j
like
一个工作骇客就是写j。
with(copy(.SD), {yada yada})
or similar, which ensures that references are to the per-group copy of .SD
and don't get conflated.
或者类似的,这样可以确保每个组的引用都是. sd的副本,并且不会被合并。
#1
2
One working hack is to write j
like
一个工作骇客就是写j。
with(copy(.SD), {yada yada})
or similar, which ensures that references are to the per-group copy of .SD
and don't get conflated.
或者类似的,这样可以确保每个组的引用都是. sd的副本,并且不会被合并。