Reputation:
I'm interested in writing a code that enables me to compare two rows of numbers that can also tell me if any numbers are missing from the second row.
Would anybody be able to help me get started or does anybody already have that kind of code?
Any help would be greatly appreciated.
Upvotes: 0
Views: 223
Reputation: 2826
Given the data example that you provided in the comments, in which you have two matrices of equal dimensions:
A <- matrix(c(210, 211, 212, 213), 2)
B <- matrix(c(210, 211, 212, 214), 2)
You can check the differences merely doing this (with the FALSE
being the values that are not equal:
A == B
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE FALSE
If you want to know where the difference is, you can do this (notice that a matrix is a special kind of vector, so you get just one index):
which(A != B)
## 4
Then, if you want to know what are the values in A
and B
that differ, you can do:
A[which(A != B)]
## 213
B[which(A != B)]
## 214
And you could subtract both lines if you want the actual difference in value:
A[which(A != B)] - B[which(A != B)]
## -1
Upvotes: 1