加载data.table对象后立即使用get(data.table)生成新变量 [英] Generating a new variable with get(data.table) directly after loading data.table object

查看:102
本文介绍了加载data.table对象后立即使用get(data.table)生成新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在data.table中生成一个新变量,该变量已再次生成,保存和加载。加载后,我通过get()间接处理了data.table,这对于生成新变量不起作用,只要我之前不直接为变量创建而处理。
可能是某种环境问题吗?

I am trying to generate a new variable in a data.table which I generated, saved and loaded again. After loading I adress the data.table indirectly through get() and this does not work for generating a new variable as long as I dont adress it directly for variable creation before. Possibly it is some kind of environment issue?

# Generate data.table
t<-data.table(x=c(1,2,3,4))
tStr<-"t"
names(t)

# Generate Variable a -> ok
get(tStr)[, a:=1]
names(t)

# Generate Variable b -> ok
t[, b:=1]
names(t)

# Save
save(t, file="test.Robj")
load("test.Robj", .GlobalEnv)

# Generate Variable c -> fails 
get(tStr)[, c:=1] 
names(t)

# Generate Variable d -> ok
t[, d:=1]
names(t)

# Generate Variable e -> ok again !?
get(tStr)[, e:=1]
names(t)

感谢您的帮助

推荐答案

这是因为重要的元数据无法在存储操作中幸存:

This is because important meta data does not survive the storage action:

> t<-data.table(x=c(1,2,3,4))
> attr(t, ".internal.selfref")
<pointer: 0x0000000000100788>
> save(t, file="test.Robj")
> load("test.Robj", .GlobalEnv)
> attr(t, ".internal.selfref")
<pointer: (nil)>
> t[, d:=1]
> attr(t, ".internal.selfref")
<pointer: 0x0000000000100788>

注意如何丢失内存指针。我不确定这是不是一个错误,因为 data.table 是什么与保存。为了使此方法正常工作,我们需要一个特殊的 load 方法,该方法在加载 data.table 对象。

Notice how you lose the memory pointer. I'm not sure this is so much a bug as an inherent conflict between what a data.table is and what save does. It seems in order for this to work properly we would need a special load method that re-assigns the internal pointer on loading data.table objects.

在这种情况下,使用按引用修改似乎会重置指针。

In this case, using the modify by reference seems to reset the pointer.

编辑:作为使用案例的一种解决方法,您可以尝试:

EDIT: as a workaround in your use case, you can try:

t <- data.table(x=c(1,2,3,4))
save(t, file="test.Robj")
load("test.Robj", .GlobalEnv)
assign("t", get("t")[, c:=3])
t

可按预期工作:

   x c
1: 1 3
2: 2 3
3: 3 3
4: 4 3

也请注意:

get("t")[, c:=3]

将正常工作,就像期望的那样:

will work is a little bit like expecting that:

get("x") <- 5

即可。 data.table 将来可能会添加此功能,但是您正在涉足 data.table 的阴暗区域code>确实开始与R语义发生冲突。

will work. data.table might in the future add this feature, but it you're treading in this murky area where the reference nature of data.table really starts conflicting with the R semantics.

这篇关于加载data.table对象后立即使用get(data.table)生成新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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