R对象身份 [英] R object identity

查看:83
本文介绍了R对象身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以测试两个对象在R语言中是否相同?

is there a way to test whether two objects are identical in the R language?

为清楚起见:我的意思不是identical函数, 可以根据某些属性(例如,数值或逻辑值等)比较对象.

For clarity: I do not mean identical in the sense of the identical function, which compares objects based on certain properties like numerical values or logical values etc.

我真的对对象身份感兴趣,例如可以使用Python语言中的is运算符进行测试.

I am really interested in object identity, which for example could be tested using the is operator in the Python language.

推荐答案

更新: data.table v1.8.9 .来自新闻:

新函数address()返回其参数在RAM中的地址.有时对以编程方式确定值是否已被R复制有用.

New function address() returns the address in RAM of its argument. Sometimes useful in determining whether a value has been copied or not by R, programatically.


可能有一种更整洁的方法,但这似乎行得通.


There's probably a neater way but this seems to work.

address = function(x) substring(capture.output(.Internal(inspect(x)))[1],2,17)
x = 1
y = 1
z = x
identical(x,y)
# [1] TRUE
identical(x,z)
# [1] TRUE
address(x)==address(y)
# [1] FALSE
address(x)==address(z)
# [1] TRUE

您可以通过将17更改为9来修改它以在32位上工作.

You could modify it to work on 32bit by changing 17 to 9.

这篇关于R对象身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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