All.equal on tibbles ignores attributes?

dat1 <- mtcars
dat2 <- mtcars
attr(dat2, "description") <- "# MTCARS\nData description ..." 
all.equal(dat1, dat2) ## not TRUE

Returns:

[1] "Attributes: < Names: 1 string mismatch >"                                       
[2] "Attributes: < Length mismatch: comparison on first 2 components >"              
[3] "Attributes: < Component 2: Lengths (32, 1) differ (string compare on first 1) >"
[4] "Attributes: < Component 2: 1 string mismatch >"
dat1 <- tibble::as_tibble(mtcars)
dat2 <- tibble::as_tibble(mtcars)
attr(dat2, "description") <- "# MTCARS\nData description ..." 
all.equal(dat1, dat2)            ## TRUE, ignores attribute
dplyr::all_equal(dat1, dat2) ## TRUE, ignores attribute

Both base::all.equal and dplyr::all_equal return TRUE here. Is this expected? Checked using both 1.3.4 and 1.4.1 on macOS Sierra

2 Likes

Just turning this into a proper reprex. There are known issues with losing attributes (see @davis' issue here) , but I'm not sure if they're related.

library(tidyverse)

dat1 <- mtcars
dat2 <- mtcars
attr(dat2, "description") <- "# MTCARS\nData description ..." 
all.equal(dat1, dat2)
#> [1] "Attributes: < Names: 1 string mismatch >"                                       
#> [2] "Attributes: < Length mismatch: comparison on first 2 components >"              
#> [3] "Attributes: < Component 2: Lengths (32, 1) differ (string compare on first 1) >"
#> [4] "Attributes: < Component 2: 1 string mismatch >"

dat1 <- tibble::as_tibble(mtcars)
dat2 <- tibble::as_tibble(mtcars)
attr(dat2, "description") <- "# MTCARS\nData description ..." 
all.equal(dat1, dat2)
#> [1] TRUE
dplyr::all_equal(dat1, dat2)
#> [1] TRUE

Created on 2018-01-18 by the reprex package (v0.1.1.9000).

1 Like

Two things here:

  1. all.equal() is a generic function, and dplyr defines:
    all.equal.tbl_df <- all_equal
    so when you use all.equal on two tibbles it just calls all_equal. See here.

  2. A quick look at the C++ function that all_equal() calls, equal_data_frame(), tells me that it doesn't seem like attributes are being checked (but I may be wrong). See here.

So this seems like the intended behavior, however it might be useful for attributes to also be checked since base R's all.equal() specifically has an argument check.attributes that defaults to TRUE. Going off this, as a user I'd expect all_equal() to follow the same defaults.

4 Likes

Thanks so much, @davis! (I knew you'd know what was up.)

So, @vnijs, I think this boils down to possibly opening a new issue, though you should probably browse the issues first to make sure it's not a duplicate. If you've never done this before, GitHub has a helpful guide:
https://help.github.com/articles/creating-an-issue/

And, also be sure to create a reprex.

https://www.tidyverse.org/help/

1 Like

Seems like I missed multiple issues:

@mara Thanks for the comments and the regex. I should have looked more closely at the posted issues
@davis Thanks for the detailed evaluation. Hopefully the option to check attributes will be added in a future release of dplyr

1 Like