0

I have two matrices as given below. When I wanted to check whether these two are identical or not, I get FALSE results. I do not understand where I am wrong?

I have checked class(A) and class(B), both are matrices.

>A
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    0    0   -1    0    0   -1   -1
[2,]    0    1    0    1    0    0    0    0
[3,]    0    0    1    1    0    0    1    1
[4,]    0    0    0    0    1    0    1    0
[5,]    0    0    0    0    0    1    0    1

>B
  1 2 3  4 5 6  7  8
1 1 0 0 -1 0 0 -1 -1
2 0 1 0  1 0 0  0  0
3 0 0 1  1 0 0  1  1
4 0 0 0  0 1 0  1  0
5 0 0 0  0 0 1  0  1

> A ==B
     1    2    3    4    5    6     7     8
1 TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
2 TRUE TRUE TRUE TRUE TRUE TRUE  TRUE  TRUE
3 TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
4 TRUE TRUE TRUE TRUE TRUE TRUE FALSE  TRUE
5 TRUE TRUE TRUE TRUE TRUE TRUE  TRUE FALSE

>identical(A,B)
[1] FALSE
Dihan
  • 311
  • 1
  • 7
  • Can you provide `dput(A)` and `dput(B)` ? – Ronak Shah Jun 25 '21 at 04:40
  • 1
    R rounds values when printing. Numbers that look the same might actually have different decimal points. It's not safe to use `==` on numbers that could potentially have decimals places. Maybe try `all.equal(A,B)` – MrFlick Jun 25 '21 at 04:40
  • Looks like `A` doesn't have row or column names but `B` does. For `identical` to work, the two objects have to be exactly same. Try `all(A == B)` – d.b Jun 25 '21 at 04:41
  • 1
    Good point @d.b. The two objects to seem to have different attributes, but also in the output for `A==B` there are some `FALSE` values. – MrFlick Jun 25 '21 at 04:44
  • 1
    Maybe `dplyr::near(A, B)` if there are rounding issues. – jpdugo17 Jun 25 '21 at 04:48

0 Answers0