Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R object identity

Tags:

object

r

identity

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

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.

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

like image 977
Sven Hager Avatar asked Jun 06 '12 10:06

Sven Hager


People also ask

How do I identify an object in R?

To get type of a value or variable or object in R programming, call typeof() function and pass the value/variable to it.

What do you mean by object identity?

Object identity is a fundamental object orientation concept. With object identity, objects can contain or refer to other objects. Identity is a property of an object that distinguishes the object from all other objects in the application.

What is object identity in Object Oriented Programming?

An identity in object-oriented programming, object-oriented design and object-oriented analysis describes the property of objects that distinguishes them from other objects.

What is meant by object identity in java?

Objects in java are characterized by three essential properties: state, identity, and behavior. The state of an object is a value from its data type. The identity of an object distinguishes one object from another. It is useful to think of an object's identity as the place where its value is stored in memory.


1 Answers

UPDATE: A more robust and faster implementation of address(x) (not using .Internal(inspect(x))) was added to data.table v1.8.9. From NEWS :

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

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

like image 179
Matt Dowle Avatar answered Nov 04 '22 12:11

Matt Dowle