R Under development (unstable) (2023-07-19 r84711 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(reclin2) Loading required package: data.table Attaching package: 'reclin2' The following object is masked from 'package:base': identical > > message("Testing compare_vars()") Testing compare_vars() > > x <- data.table(foo = 1:3, bar = c("aa", "bb", "cc")) > y <- data.table(foo = 3:4, bar = c("cc", "dd")) > pairs <- pair(x, y) > > tmp <- compare_vars(pairs, "foo") > tmp2 <- x$foo[pairs$.x] == y$foo[pairs$.y] > stopifnot(is.logical(tmp$foo)) > stopifnot(isTRUE(all.equal(as.logical(tmp$foo), tmp2))) > > tmp <- copy(pairs) > compare_vars(tmp, "foo", inplace = TRUE) > tmp2 <- x$foo[pairs$.x] == y$foo[pairs$.y] > stopifnot(is.logical(tmp$foo)) > stopifnot(isTRUE(all.equal(as.logical(tmp$foo), tmp2))) > > tmp <- compare_vars(pairs, "fooc", "foo") > tmp2 <- x$foo[pairs$.x] == y$foo[pairs$.y] > stopifnot(is.logical(tmp$fooc)) > stopifnot(isTRUE(all.equal(as.logical(tmp$fooc), tmp2))) > > tmp <- compare_vars(pairs, "fooc", "foo", "foo") > tmp2 <- x$foo[pairs$.x] == y$foo[pairs$.y] > stopifnot(is.logical(tmp$fooc)) > stopifnot(isTRUE(all.equal(as.logical(tmp$fooc), tmp2))) > > > # Comparing on one variable generates two resulting comparison > # vectors > testfun <- function(a, b) { + data.table( + jw = jaro_winkler()(a,b), + ja = jaccard()(a,b) + ) + } > tmp <- compare_vars(pairs, "foo", "bar", "bar", testfun) Warning message: In deprecated_warn(paste0("identical, jaro_winkler, lcs, jaccard", : identical, jaro_winkler, lcs, jaccard are deprecated. Use the cmp_ variants (see ?cmp_identical). This warning is shown only once. Set the option reclin2_deprecate_warn to FALSE to disable these warnings. > tmp2 <- 1*(x$bar[pairs$.x] == y$bar[pairs$.y]) > stopifnot(isTRUE(all.equal(tmp$foo_jw, tmp2))) > stopifnot(isTRUE(all.equal(tmp$foo_ja, tmp2))) > > > # Multiple variables are compared resulting in one comparison > # vector > testfun <- function(x, y) { + cmp <- cmp_identical() + c1 <- cmp(x[[1]], y[[1]]) + cmp(x[[2]], y[[2]]) + c2 <- cmp(x[[1]], y[[2]]) + cmp(x[[2]], y[[1]]) + pmax(c1, c2) + } > tmp <- compare_vars(pairs, "foo", c("foo", "bar"), comparator = testfun) > tmp2 <- (x$foo[pairs$.x] == y$foo[pairs$.y]) + + (x$bar[pairs$.x] == y$bar[pairs$.y]) > stopifnot(is.numeric(tmp$foo)) > stopifnot(isTRUE(all.equal(as.numeric(tmp$foo), tmp2))) > > # Pairs has zero records > x <- data.table(foo = 1:3, bar = c("aa", "bb", "cc")) > y <- data.table(foo = 3:4, bar = c("cc", "dd")) > y <- y[FALSE,] > pairs <- pair(x, y) > tmp <- compare_vars(pairs, "foo") > tmp2 <- x$foo[pairs$.x] == y$foo[pairs$.y] > stopifnot(is.logical(tmp$foo)) > stopifnot(isTRUE(all.equal(as.logical(tmp$foo), tmp2))) > > message("Testing compare_vars() successful") Testing compare_vars() successful > > proc.time() user system elapsed 0.25 0.10 0.34