Compare if two dataframe objects in R are equal?

前端 未结 4 607
后悔当初
后悔当初 2020-12-23 19:33

How do I check if two objects, e.g. dataframes, are value equal in R?

By value equal, I mean the value of each row of each column of one dataframe is equal to the va

相关标签:
4条回答
  • 2020-12-23 19:59

    Without the need to rely on another package, but to compare structure (class and attributes) of two data sets:

    structure_df1 <- sapply(df1, function(x) paste(class(x), attributes(x), collapse = ""))
    structure_df2 <- sapply(df2, function(x) paste(class(x), attributes(x), collapse = ""))
    
    all(structure_df1 == structure_df2)
    
    0 讨论(0)
  • 2020-12-23 20:02

    In addition, identical is still useful and supports the practical goal:

    identical(a[, "x"], b[, "y"]) # TRUE
    
    0 讨论(0)
  • 2020-12-23 20:11

    It is not clear what it means to test if two data frames are "value equal" but to test if the values are the same, here is an example of two non-identical dataframes with equal values:

    a <- data.frame(x = 1:10)
    b <- data.frame(y = 1:10)
    

    To test if all values are equal:

    all(a == b) # TRUE
    

    To test if objects are identical (they are not, they have different column names):

    identical(a,b) # FALSE: class, colnames, rownames must all match.
    
    0 讨论(0)
  • 2020-12-23 20:18

    We can use the R package compare to test whether the names of the object and the values are the same, in just one step.

    a <- data.frame(x = 1:10)
    b <- data.frame(y = 1:10)
    
    library(compare)
    compare(a, b)
    #FALSE [TRUE]#objects are not identical (different names), but values are the same.
    

    In case we only care about equality of the values, we can set ignoreNames=TRUE

    compare(a, b, ignoreNames=T)
    #TRUE
    #  dropped names
    

    The package has additional interesting functions such as compareEqual and compareIdentical.

    0 讨论(0)
提交回复
热议问题