From fa5916df4b477db07d4f6e148203d7ee3be5a706 Mon Sep 17 00:00:00 2001 From: chrisfacer Date: Wed, 8 Jun 2022 14:03:22 +1000 Subject: [PATCH] Fix typo and improve test for CombineVariableSetsAsBinary --- R/combinevariablesetsasbinary.R | 4 ++-- tests/testthat/test-combinevariablesetsasbinary.R | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/R/combinevariablesetsasbinary.R b/R/combinevariablesetsasbinary.R index 459b697..d0fbc1c 100644 --- a/R/combinevariablesetsasbinary.R +++ b/R/combinevariablesetsasbinary.R @@ -124,11 +124,11 @@ questionToBinary <- function(x) { if (question.type %in% c("PickAny", "PickAnyCompact")) { # Identify and remove the NET column basedon the codeframe attribute - cf = attr(x, "codeframe") + cf <- attr(x, "codeframe") if (!is.null(cf)) { unique.codes = unique(unlist(cf)) net.cols = vapply(cf, isDefaultNet, FUN.VALUE = logical(1), unique.codes = unique.codes) - x[, !net.cols] + x <- x[, !net.cols] } return(x) } diff --git a/tests/testthat/test-combinevariablesetsasbinary.R b/tests/testthat/test-combinevariablesetsasbinary.R index 0deb25b..efe8999 100644 --- a/tests/testthat/test-combinevariablesetsasbinary.R +++ b/tests/testthat/test-combinevariablesetsasbinary.R @@ -38,6 +38,8 @@ attr(Q4.pepsi.max, "questiontype") <- "PickOne" attr(Q4.pepsi.max, "dataset") <- "colas" Q4.binary = CombineVariableSetsAsBinary(Q4) +Q4.binary = cbind(Q4.binary, "NET" = rep(TRUE, nrow(Q4.binary))) +attr(Q4.binary, "codeframe") <- list("Hate" = 1, "Dislike" = 2, "Neither like not dislike" = 3, "Love" = 4, "Like" = 5, "NET" = c(1,2,3,4,5)) attr(Q4.binary, "questiontype") <- "PickAny" attr(Q4.binary, "dataset") <- "colas" @@ -79,14 +81,14 @@ test_that("Many PickOnes are equivalent to a PickOneMulti", { test_that("Combining PickOnes and Pick Any", { - expect_equal(Q4.binary, CombineVariableSetsAsBinary(Q4.binary.small, Q4.pepsi.light, Q4.pepsi.max), check.attributes = FALSE) + expect_equal(Q4.binary[, -ncol(Q4.binary)], CombineVariableSetsAsBinary(Q4.binary.small, Q4.pepsi.light, Q4.pepsi.max), check.attributes = FALSE) }) test_that("Pick Any returns same data", { - expect_equal(CombineVariableSetsAsBinary(Q4.binary), Q4.binary, check.attributes = FALSE) - expect_equal(CombineVariableSetsAsBinary(Q4.binary, Q4.binary), Q4.binary, check.attributes = FALSE) + expect_equal(CombineVariableSetsAsBinary(Q4.binary), Q4.binary[, -ncol(Q4.binary)], check.attributes = FALSE) + expect_equal(CombineVariableSetsAsBinary(Q4.binary, Q4.binary), Q4.binary[, -ncol(Q4.binary)], check.attributes = FALSE) })