R对象身份 [英] R object identity
问题描述
有没有一种方法可以测试两个对象在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屋!