R | How to compare complex data objects
While simple data such as integers and characters can be easily compared in R using the built-in comparison operators/functions, doing so for more complex data (such as nested lists) becomes more tedious.
A quick, user-friendly way to compare complex data is to use the compare
function from the waldo
package [1]:
waldo::compare(c(1, 2, 3), c(1, 2, 4))
# output:
# `old`: 1 2 3
# `new`: 1 2 4
(This is the same mechanism used by assertions in testthat
[2].)
For programming, the length
of the return value from waldo::compare
will be 0 when there is no difference, and 1 when there is some difference. Note that the actual differences themselves are not easily available programmatically, since they're in string format.
length(waldo::compare(5, 5)) # 0
length(waldo::compare(5, 6)) # 1
Comparing Dataframes:
waldo::compare
is useful for surfacing a brief summary of differences between two values. However, when comparing dataframes, a common use case is to programmatically identify the locations in which the differences occur, in which case compare
is not so useful.
For those cases, see [3], which lists out some options for comparing dataframes.