I want to compare two data frames and check if both have identical set of columns, is there any built-in function or any library in R? Values of these data frames might be different but both the data frames will have same type and named columns.
I tried running identical
and all_equal
on mtcars and a replica dataframe:
duplicate <- mtcars
identical(mtcars, duplicate)
[1] TRUE
all_equal(mtcars, duplicate)
[1] TRUE
Then I updated the mpg
column of data.frame duplicate to have different values than mtcars:
duplicate$mpg <- as.numeric(scale(duplicate$mpg))
Again run the same commands:
identical(mtcars, duplicate)
[1] FALSE
all_equal(mtcars, duplicate)
[1] "Rows in x but not y: 23, 1, 6, 14, 10, 12, 13, 17, 28, 32, 7[...]. Rows in y but not x: 12, 25, 1, 20, 30, 5, 14, 7, 11, 29, 21[...]. "
Now they appear as not identical dataframes.
I want to compare and check in this second case where values are different but column names and their types are same. Basically if both have same schema.