Comparing two matrices for equality in R

Table of contents

Method 1: Use all.equal() to compare whether two R objects are approximately equal

Method 2: Use identical to compare whether two R objects are exactly equal.


Method 1: Use all.equal() to compare whether two R objects are approximately equal

Use the function: all.equal(x,y) to compare whether two R objects x and y are approximately equal

> M1<-matrix(1:100,ncol=10)
> M1
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11   21   31   41   51   61   71   81    91
 [2,]    2   12   22   32   42   52   62   72   82    92
 [3,]    3   13   23   33   43   53   63   73   83    93
 [4,]    4   14   24   34   44   54   64   74   84    94
 [5,]    5   15   25   35   45   55   65   75   85    95
 [6,]    6   16   26   36   46   56   66   76   86    96
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,]   10   20   30   40   50   60   70   80   90   100
> M2<-matrix(1:100,ncol=10)
> M2
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11   21   31   41   51   61   71   81    91
 [2,]    2   12   22   32   42   52   62   72   82    92
 [3,]    3   13   23   33   43   53   63   73   83    93
 [4,]    4   14   24   34   44   54   64   74   84    94
 [5,]    5   15   25   35   45   55   65   75   85    95
 [6,]    6   16   26   36   46   56   66   76   86    96
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,]   10   20   30   40   50   60   70   80   90   100
> all.equal(M1,M2)
[1] TRUE

Explanation in the help file:

 From the explanation in the help file, we can know that all.equal(x,y) is used to compare whether x and y are approximately equal. If they differ, the comparison is somehow continued and a difference report is returned in the console. It is recommended that in the if expression, do not use the all.equal function directly, but use isTRUE(all.equal(...)) or use the identical() function.

Method 2: Use identical to compare whether two R objects are exactly equal.

identical(x,y) is used to compare whether two R objects are exactly equal (exactly equal)

 The description in the help file of the identical() function:

 From this, it follows that there is a safe and reliable way to test whether two objects are exactly equal. Returns TRUE in this case, FALSE otherwise.

> M1<-matrix(1:100,ncol=10)
> M1
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11   21   31   41   51   61   71   81    91
 [2,]    2   12   22   32   42   52   62   72   82    92
 [3,]    3   13   23   33   43   53   63   73   83    93
 [4,]    4   14   24   34   44   54   64   74   84    94
 [5,]    5   15   25   35   45   55   65   75   85    95
 [6,]    6   16   26   36   46   56   66   76   86    96
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,]   10   20   30   40   50   60   70   80   90   100
> M2<-matrix(1:100,ncol=10)
> M2
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11   21   31   41   51   61   71   81    91
 [2,]    2   12   22   32   42   52   62   72   82    92
 [3,]    3   13   23   33   43   53   63   73   83    93
 [4,]    4   14   24   34   44   54   64   74   84    94
 [5,]    5   15   25   35   45   55   65   75   85    95
 [6,]    6   16   26   36   46   56   66   76   86    96
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,]   10   20   30   40   50   60   70   80   90   100
> identical(M1,M2)
[1] TRUE
> typeof(M1)
[1] "integer"
> typeof(M2)
[1] "integer"

Note: When using the identical() function to compare whether two R objects are completely equal, if they are not equal, pay attention to check whether the data types are different, for example, one data type is double, and the other data type is integer.

reference:

How to check if two matrices are equal in R? -Mianshigee.com (mianshigee.com)  (introduces related examples of all.equal function)

How to use the identical() function in R - Zhihu (zhihu.com)  (introduces related examples of the identical function)

Guess you like

Origin blog.csdn.net/u011375991/article/details/132167087