Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function to show differences between two or more objects

Tags:

r

When using R, I normally use identical(a,b) to check if the objects a and b are the same. If this returns FALSE I would like to be able to see where these differences are...is there a function that would show me this?

like image 251
h.l.m Avatar asked Dec 20 '22 14:12

h.l.m


2 Answers

Your are looking for all.equal()

a <- data.frame(A = 1)
b <- data.frame(B = 1)
all.equal(a, b)

[1] "Names: 1 string mismatch"

d <- data.frame(B = 2)
all.equal(b, d)

"Component 1: Mean relative difference: 1"

all.equal(a, d)

[1] "Names: 1 string mismatch"                
[2] "Component 1: Mean relative difference: 1"
like image 141
Thierry Avatar answered Jan 11 '23 22:01

Thierry


This doesn't exactly answer your question, but you may also be interested in the "compare" package.

In particular, there is an argument "allowAll=TRUE" to the compare() function which seems to try to transform the comparison object to match the input, and it says what transformations were required and whether the objects were then the same.

First, some data. "a", "b", "d", and "e" are very similar.

a <- data.frame(A = 1:10, B = LETTERS[1:10])
b <- data.frame(C = 1:10, B = LETTERS[1:10])
d <- data.frame(A = 1:10, B = letters[1:10])
e <- data.frame(A = 1:10, B = letters[1:10])
f <- data.frame(A = c(1, 3, 4, 5, 6, 7, 8, 11, 12, 13), 
                B = LETTERS[c(1, 3, 2, 4, 5, 6, 9, 8, 7, 10)])

Now, use compare to see if it was able to make the data the same. For "b", "d", and "e", it was able to apply certain transformations to make them the same as "a", but it was not able to do so when comparing "f" and "a".

(w <- compare(a, b, allowAll=TRUE))
# TRUE
#   renamed
#   dropped names
(x <- compare(a, d, allowAll=TRUE))
# TRUE 
#   [B] dropped attributes
(y <- compare(a, e, allowAll=TRUE))
# TRUE 
#   [B] dropped attributes
(z <- compare(a, f, allowAll=TRUE))
# FALSE [FALSE, FALSE]
#   [A] coerced from <numeric> to <integer>

You can also see more details about the compare object.

str(x)
# List of 8
#  $ result          : logi TRUE
#  $ detailedResult  : Named logi [1:2] TRUE TRUE
#   ..- attr(*, "names")= chr [1:2] "A" "B"
#  $ transform       : Named chr "[B] dropped attributes"
#   ..- attr(*, "names")= chr "B"
#  $ tM              :'data.frame':  10 obs. of  2 variables:
#   ..$ A: int [1:10] 1 2 3 4 5 6 7 8 9 10
#   ..$ B: int [1:10] 1 2 3 4 5 6 7 8 9 10
#  $ tC              :'data.frame': 10 obs. of  2 variables:
#   ..$ A: int [1:10] 1 2 3 4 5 6 7 8 9 10
#   ..$ B: int [1:10] 1 2 3 4 5 6 7 8 9 10
#  $ tMpartial       :'data.frame': 10 obs. of  2 variables:
#   ..$ A: int [1:10] 1 2 3 4 5 6 7 8 9 10
#   ..$ B: Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10
#  $ tCpartial       :'data.frame': 10 obs. of  2 variables:
#   ..$ A: int [1:10] 1 2 3 4 5 6 7 8 9 10
#  ..$ B: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10
#  $ partialTransform: chr(0) 
#  - attr(*, "class")= chr [1:2] "multipleComparison" "comparison"

The package also includes several other functions, and many arguments that can restrict or extend the types of transformations permitted before comparison.

like image 32
A5C1D2H2I1M1N2O1R2T1 Avatar answered Jan 11 '23 22:01

A5C1D2H2I1M1N2O1R2T1