Compare if two dataframe objects in R are equal?

前端 未结 4 609
后悔当初
后悔当初 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 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.

提交回复
热议问题