【问题标题】:How to set tolerance of expect_equal in testthat when comparing data_frames?比较data_frames时如何在testthat中设置expect_equal的容差?
【发布时间】:2016-10-10 20:03:50
【问题描述】:

当我检查两个数据帧是否相等时,我无法使用 testthat 的容差参数。这是一个包含两个数据框的简单示例:

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

当我检查相等性时,testthat 会引发错误,即使容差值足够高:

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

比较单个向量时不会引发错误:

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

有什么建议吗?我假设我误用了 expect_equal 函数,但除了在数据框的各个列上运行 expect_equal 之外,我不确定如何解决。

这是我正在使用的软件包版本:

packageVersion("dplyr")
# [1] ‘0.4.3’
packageVersion("testthat")
# [1] ‘0.11.0’

【问题讨论】:

  • 我可以重现您的错误。如果没有dplyr(只需使用data.frame),所有测试都会按预期通过。

标签: r dplyr testthat


【解决方案1】:

看来这个问题已经解决了。

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)

现在测试结果很干净。

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 2011-08-26
    • 1970-01-01
    • 2014-11-10
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多