diff --git a/DESCRIPTION b/DESCRIPTION index e83fcafc2..d930f6fe8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.9.0.4 +Version: 0.9.0.5 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/R/to_numeric.R b/R/to_numeric.R index 80fb7db27..e948d7cc1 100644 --- a/R/to_numeric.R +++ b/R/to_numeric.R @@ -226,6 +226,13 @@ to_numeric.factor <- function(x, } names(out) <- levels(x) } else if (preserve_levels) { + if (is.unsorted(levels(x))) { + x_inverse <- rep(NA_real_, length(x)) + for (i in 1:nlevels(x)) { + x_inverse[x == levels(x)[i]] <- as.numeric(levels(x)[nlevels(x) - i + 1]) + } + x <- factor(x_inverse) + } out <- .set_back_labels(as.numeric(as.character(x)), x) } else { out <- .set_back_labels(as.numeric(x), x) diff --git a/tests/testthat/test-data_to_numeric.R b/tests/testthat/test-data_to_numeric.R index df43f401f..3e0a9d095 100644 --- a/tests/testthat/test-data_to_numeric.R +++ b/tests/testthat/test-data_to_numeric.R @@ -44,7 +44,6 @@ test_that("convert factor to numeric", { expect_snapshot(to_numeric(f)) }) - test_that("convert factor to numeric", { expect_identical(to_numeric(c("abc", "xyz")), c(1, 2)) expect_identical(to_numeric(c("123", "789")), c(123, 789)) @@ -52,7 +51,6 @@ test_that("convert factor to numeric", { expect_identical(to_numeric(c("1L", "2e-3", "ABC")), c(1, 2, 3)) }) - test_that("convert factor to numeric, dummy factors", { expect_identical( to_numeric(c("abc", "xyz"), dummy_factors = TRUE), @@ -66,7 +64,6 @@ test_that("convert factor to numeric, dummy factors", { ) }) - test_that("convert factor to numeric, append", { data(efc) expect_identical( @@ -94,13 +91,11 @@ test_that("convert factor to numeric, append", { ) }) - test_that("convert factor to numeric, all numeric", { data(mtcars) expect_identical(to_numeric(mtcars), mtcars) }) - test_that("convert factor to numeric, dummy factors, with NA", { x1 <- factor(rep(c("a", "b"), 3)) x2 <- factor(c("a", NA_character_, "a", "b", "a", "b")) @@ -153,6 +148,20 @@ test_that("convert factor to numeric, dummy factors, with NA", { expect_identical(nrow(to_numeric(x7, dummy_factors = TRUE)), length(x7)) }) +test_that("to_numeric, inverse factor levels", { + f <- c(0, 0, 1, 1, 1, 0) + x1 <- factor(f, levels = c(0, 1)) + x2 <- factor(f, levels = c(1, 0)) + out <- to_numeric(x1, dummy_factors = FALSE, preserve_levels = FALSE) + expect_identical(out, c(1, 1, 2, 2, 2, 1)) + out <- to_numeric(x2, dummy_factors = FALSE, preserve_levels = FALSE) + expect_identical(out, c(2, 2, 1, 1, 1, 2)) + out <- to_numeric(x1, dummy_factors = FALSE, preserve_levels = TRUE) + expect_identical(out, c(0, 0, 1, 1, 1, 0)) + out <- to_numeric(x2, dummy_factors = FALSE, preserve_levels = TRUE) + expect_identical(out, c(1, 1, 0, 0, 0, 1)) +}) + # select helpers ------------------------------ test_that("to_numeric regex", { expect_identical(