For example:
A <- 1:10 B <- A
Both A and B reference the same underlying vector.
Before I plow off and implement something in C... Is there a function in R which can test whether two variables reference the same underlying object?
Thanks.
setequal() function in R Language is used to check if two objects are equal. This function takes two objects like Vectors, dataframes, etc. as arguments and results in TRUE or FALSE, if the Objects are equal or not.
It's not possible, an if statement has no special scope, so you can't have two variables with the same name within the same scope and access both, the latter will overwrite the former, so they should have different names.
Yes, two or more references, say from parameters and/or local variables and/or instance variables and/or static variables can all reference the same object.
You can use the .Internal
inspect
function:
A <- 1:10 B <- A .Internal(inspect(A)) # @27c0cc8 13 INTSXP g0c4 [NAM(2)] (len=10, tl=0) 1,2,3,4,5,... .Internal(inspect(B)) # same # @27c0cc8 13 INTSXP g0c4 [NAM(2)] (len=10, tl=0) 1,2,3,4,5,... B[1] <- 21 .Internal(inspect(B)) # different # @25a7528 14 REALSXP g0c6 [NAM(1)] (len=10, tl=150994944) 21,2,3,4,5,...
Simon Urbanek has written a simple package with similar functionality. It's called... wait for it... inspect. You can get it from R-forge.net by running:
install.packages('inspect',repos='http://www.rforge.net/')
UPDATE: A word of warning:
I recommend you use Simon's package because I'm not going to recommend you call .Internal
. It certainly isn't intended to be used interactively and it may very well be possible to crash your R session by using it carelessly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With