Skip to content

Instantly share code, notes, and snippets.

@peterdalle
Created December 6, 2018 08:57
Show Gist options
  • Save peterdalle/f45ac12a7a861aaf6533b4e9c86d2772 to your computer and use it in GitHub Desktop.
Save peterdalle/f45ac12a7a861aaf6533b4e9c86d2772 to your computer and use it in GitHub Desktop.
Compare two data frames for similarity (and if they are different, print out the differences)
# Create data.
set.seed(1341)
original <- data.frame(x = rnorm(10), y = rnorm(10))
change <- original
change[5, 1] <- 6
change[7, 2] <- 4
# Function to compare data frames for similarity.
compare_data_frames <- function(df1, df2) {
if (identical(df1, df2)) {
cat("Data frames are identical.")
} else {
cat("Data frames are not identical.\n\nDifferences:\n")
all.equal(df1, df2)
}
}
# Do comparison.
compare_data_frames(original, change)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment