How to set validity of expect_equal in testthat when comparing data_frames?

I am having problems using the testthat tolerance parameter when I check if two data_frames are equal. Here is a simple example with two data frames:

library(dplyr)
library(testthat)
d1 = data_frame(a = c(1, 1),
                b = c(2, 2))
d2 = data_frame(a = c(1, 1.00001),
                b = c(2, 2))

When I check for equality, testthat throws an error even with a sufficiently high tolerance value:

expect_equal(d1, d2,
             tolerance = 0.01)

# Error: d1 not equal to d2
# Rows in y but not x: 2. Rows with difference occurences in x and y: 1

When comparing individual vectors, an error does not occur:

expect_equal(d1$a, d2$a,
             tolerance = 0.01)
expect_equal(d1$b, d2$b,
             tolerance = 0.01)

Any suggestions? I assume that I am using the expect_equal function incorrectly, but I donโ€™t know how to solve it, other than running expect_equal on separate columns of the data frame.

Here are the versions of the package I'm using:

packageVersion("dplyr")
# [1] โ€˜0.4.3โ€™
packageVersion("testthat")
# [1] โ€˜0.11.0โ€™
+4
source share

All Articles