Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dplyr

testthat

I'm having trouble using testthat's tolerance parameter when I check whether two data_frames are equal. Here's 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

No error is thrown when I compare the individual vectors:

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

Any suggestions? I assume I'm misusing the expect_equal function, but am not sure how to resolve other than running expect_equal on individual columns of the data frame.

Here's the package versions I'm using:

packageVersion("dplyr")
# [1] ‘0.4.3’
packageVersion("testthat")
# [1] ‘0.11.0’
like image 801
Eric Avatar asked Jun 10 '16 00:06

Eric


1 Answers

It looks like this issue has since been resolved.

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))

expect_equal(d1, d2, tolerance = 0.01)

The test comes out clean now.

like image 174
Kenneth Schackart Avatar answered Oct 14 '22 15:10

Kenneth Schackart