将数据保存在data.table列表中 [英] Saving plots in a data.table list column

查看:184
本文介绍了将数据保存在data.table列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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]])]

来自 DT [...] 的图是正确的:





虽然重播是错误的:

x 的最终值显示在$ p

中。 y
神奇地应用于所有三个图表,可能是因为data.table处理指向 by 组中的列的指针。



我不是真实的但我确定我明白发生了什么,因为我可以在表面上改变这个例子,编写 v = id2 * 1:10 而不是 v = id * 10 + id2 * 1:10 和 - poof - 问题消失。



我想知道这是怎么回事?(ii)我可以做些什么简单的调整来解决它? 解决方案

j like

  with(copy(.SD),{yada yada} )

或类似的,这可以确保引用到 .SD ,不要混淆。


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:

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.

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.

I'd like to know (i) why is this happening and (ii) what simple tweak can I make to get around it?

解决方案

One working hack is to write j like

with(copy(.SD), {yada yada})

or similar, which ensures that references are to the per-group copy of .SD and don't get conflated.

这篇关于将数据保存在data.table列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆