Yamazaki
Yamazaki

Reputation: 41

Having trouble understanding how "Identical" works

I have a probably very stupid question regarding the identical() function.

I was writting a script to test if some values come several time in my data.frame to regroup them. I compare values 2 by 2 over 4 columns.

I identified some in my table, and wanted to test my script. Here is part of the data.frame:

          Ret..Time  Mass       Ret..Time  Mass       deltaRT  deltaMZ
    178   3.5700     797.6324   3.4898     797.6018   0.0802   0.0306
    179   3.6957     797.6519   3.7502     797.5798   0.0545   0.0721
    180   3.3526     797.6655   3.2913     797.5980   0.0613   0.0675
    182   3.1561     797.7123   3.1650     797.5620   0.0089   0.1503
    182.1 3.1561     797.7123   3.0623     797.6174   0.0938   0.0949
    183   3.4495     797.8207   3.3526     797.6655   0.0969   0.1552

So here the elements of column 1 and 2 on row "180" are equal to those in 3 and 4 on row "183".

Here is what I get and what confuses me:

all.equal(result["180",1:2],result["183",3:4])
[1] "Attributes: < Component “row.names”: 1 string mismatch >"
identical(result["180",1:2],result["183",3:4])
[1] FALSE
identical(result["180",1],result["183",3]) & identical(result["180",2],result["183",4])
[1] TRUE

I get that all.equal reacts to the different rownames (although I don't really understand why, I'm asking to compare the values in specifice columns, not whole rows). But why does identical need to compare the values separately? It doesn't work any better if I use result[180,c(1,2)] and result[183,c(3,4)]. Does identical() start to use the rownames too if I compare more than 1 value? How to prevent that? In my case, I have only 2 values to compare to 2 other values, but what if the string to compare was spanned over 10 columns? Would I need to add & and identical() to compare each of the 10 columns individually?

Thanks in advance!

Upvotes: 1

Views: 193

Answers (1)

Carl Witthoft
Carl Witthoft

Reputation: 21502

Keep in mind that not only the value but also all attributes must match for identical to return TRUE . Consider:

 foo<-1
 bar<-1
 dim(foo)<-c(1,1)
identical(foo,bar)
[1] FALSE

Upvotes: 1

Related Questions