tchakravarty
tchakravarty

Reputation: 10984

R: test_that multiple objects are equal

I am trying to include a test using testthat::test_that in a script, and would like to write a test that multiple objects are equal:

dfX1 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

dfX2 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

dfX3 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

# something like this?
stopifnot(all(row.names(dfX1) == row.names(dfX2) == row.names(dfX3)))

I am fully aware that the last line makes no sense. I am looking for a compact solution that fits the testthat semantics.

Upvotes: 0

Views: 1570

Answers (2)

RBA
RBA

Reputation: 933

Two solutions with testthat:

library(testthat);library(purrr)
# example data
dfX1 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))
dfX2 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))
dfX3 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

Testthat solution 1 - does not identify where the problem is, but is a one-liner

expect_equal(unlist(unique(map(list(dfX1, dfX2, dfX3), row.names))), row.names(dfX1))

With error

olddfX3<-dfX3
row.names(dfX3) <- paste0(row.names(dfX3), 1)

expect_equal(unlist(unique(map(list(dfX1, dfX2, dfX3), row.names))), row.names(dfX1))
# Error: unlist(unique(map(list(dfX1, dfX2, dfX3), row.names))) not equal to row.names(dfX1).
# Lengths differ: 20 vs 10

dfX3<-olddfX3

Testthat solution 2 - 2x2 tests - will give you long error texts, but you can identify the problems

tests_2x2 <- function(names){

  lss <- setNames(lapply(names,get),names)
  
  len <- length(lss)
  
  
  for(x in 1:len){
    y=x+1
    while(y<=len){
      test_that(paste0("test that row.names(", 
        names(lss)[x], ") == row.names(", names(lss)[y], ")"), {
          expect_equal(row.names(lss[[x]]), row.names(lss[[y]]))})
      y=y+1
    }
  }
}

Input df names to get specific results on the tests

names<-c("dfX1", "dfX2", "dfX3")
tests_2x2(names)

One with error:

olddfX3<-dfX3
row.names(dfX3) <- paste0(row.names(dfX3), 1)
tests_2x2(names)
# Error: Test failed: 'test that row.names(dfX2) == row.names(dfX3)'
# * row.names(lss[[i]]) not equal to row.names(lss[[i + 1]]).
# 10/10 mismatches
# x[1]: "team1"
# y[1]: "team11"
# 
# x[2]: "team2"
# y[2]: "team21"
# 
# x[3]: "team3"
# y[3]: "team31"
# 
# x[4]: "team4"
# y[4]: "team41"
# 
# x[5]: "team5"
# y[5]: "team51"
# 
# x[6]: "team1"
# y[6]: "team11"
# 
# x[7]: "team2"
# y[7]: "team21"
# 
# x[8]: "team3"
# y[8]: "team31"
# 
# x[9]: "team4"
# y[9]: "team41"
# 
# x[10]: "team5"
# y[10]: "team51" 

Upvotes: 0

grrgrrbla
grrgrrbla

Reputation: 2589

try this:

dfX1 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

dfX2 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

dfX3 = data.frame(x1 = rnorm(10), 
                  x2 = rnorm(10), 
                  row.names = paste0("team", 1:10))

# something like this?
stopifnot(identical(union(row.names(dfX1) ,row.names(dfX2)),
                row.names(dfX3)) == TRUE)

for more than 2 vectors use:

stopifnot(identical(Reduce(union, list(row.names(dfX1) ,row.names(dfX2),
row.names(dfX3, ... , row.names(dfX_Nminus1))),
                row.names(dfX_N)) == TRUE)

this also works, given that the length of all the vectors is equal:

l.d <- list(row.names(dfX1) ,row.names(dfX2),
        row.names(dfX3))
stopifnot(length(Reduce(intersect, l.d)) == length(l.d[[1]]))

Upvotes: 1

Related Questions