diff --git a/R/aFIPC.R b/R/aFIPC.R index 62546519..3f336635 100644 --- a/R/aFIPC.R +++ b/R/aFIPC.R @@ -613,10 +613,10 @@ autoFIPC <- if (checkIPD == T) { # config IPDgroup <- - as.factor(c( + factor(c( rep('oldForm', nrow(oldformYDataK)), rep('newForm', nrow(newformXDataK)) - )) + ), levels = c('newForm', 'oldForm')) IPDItemCount <- 0 # IPD target item checking diff --git a/tests/testthat/test-ipdgroup-factor-equivalence.R b/tests/testthat/test-ipdgroup-factor-equivalence.R new file mode 100644 index 00000000..a7527366 --- /dev/null +++ b/tests/testthat/test-ipdgroup-factor-equivalence.R @@ -0,0 +1,19 @@ +test_that("explicit IPD group levels preserve the protected factor contract", { + cardinalities <- list( + c(old_form = 1L, new_form = 1L), + c(old_form = 3L, new_form = 5L), + c(old_form = 37L, new_form = 61L) + ) + + for (sizes in cardinalities) { + labels <- c( + rep("oldForm", sizes[["old_form"]]), + rep("newForm", sizes[["new_form"]]) + ) + protected <- as.factor(labels) + candidate <- factor(labels, levels = c("newForm", "oldForm")) + + expect_identical(candidate, protected) + expect_identical(levels(candidate), c("newForm", "oldForm")) + } +})