当我检查两个 data_frames 是否相等时,我在使用 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))
Run Code Online (Sandbox Code Playgroud)
当我检查相等性时,即使容差值足够高,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
Run Code Online (Sandbox Code Playgroud)
当我比较单个向量时不会抛出错误:
expect_equal(d1$a, d2$a,
tolerance = 0.01)
expect_equal(d1$b, d2$b,
tolerance = 0.01)
Run Code Online (Sandbox Code Playgroud)
有什么建议?我假设我滥用了 expect_equal 函数,但除了在数据框的各个列上运行 expect_equal 之外,我不确定如何解决。
这是我正在使用的软件包版本:
packageVersion("dplyr")
# [1] ‘0.4.3’
packageVersion("testthat")
# [1] ‘0.11.0’
Run Code Online (Sandbox Code Playgroud)
小智 2
看来这个问题已经解决了。
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)
Run Code Online (Sandbox Code Playgroud)
现在测试结果是干净的。
| 归档时间: |
|
| 查看次数: |
882 次 |
| 最近记录: |