Skip to content

Commit

Permalink
Fix invers levels for to_numeric()
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 27, 2023
1 parent 2852033 commit c554120
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
7 changes: 7 additions & 0 deletions R/to_numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 14 additions & 5 deletions tests/testthat/test-data_to_numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ 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))
expect_identical(to_numeric(c("1L", "2e-3")), c(1, 0.002))
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),
Expand All @@ -66,7 +64,6 @@ test_that("convert factor to numeric, dummy factors", {
)
})


test_that("convert factor to numeric, append", {
data(efc)
expect_identical(
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c554120

Please sign in to comment.