From b036bce2afedbd564a49f0ca9e9e986050af2629 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 4 May 2024 11:24:18 +0200 Subject: [PATCH] DOn't check type of NA values --- DESCRIPTION | 2 +- NEWS.md | 8 ++++++++ R/recode_into.R | 18 ++++++++++++++---- tests/testthat/test-recode_into.R | 17 ++++++++++++++++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 91adb0d79..e5f85a0eb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.10.0 +Version: 0.10.0.1 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NEWS.md b/NEWS.md index 5c21e800b..0689046d2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +# datawizard 0.10.1 + +CHANGES + +* `recode_into()` is more relaxed regarding checking the type of `NA` values. + If you recode into a numeric variable, and one of the recode values is `NA`, + you no longer need to use `NA_real_` for numeric `NA` values. + # datawizard 0.10.0 BREAKING CHANGES diff --git a/R/recode_into.R b/R/recode_into.R index fc369a62a..308124e56 100644 --- a/R/recode_into.R +++ b/R/recode_into.R @@ -140,12 +140,22 @@ recode_into <- function(..., for (i in seq_len(n_params)) { # get type of all recode values if (is.null(data)) { - type <- typeof(.dynEval(dots[[i]][[3]], ifnotfound = NULL)) - len_matches <- length(.dynEval(dots[[i]][[2]], ifnotfound = NULL)) + value_type <- .dynEval(dots[[i]][[3]], ifnotfound = NULL) + value_length <- .dynEval(dots[[i]][[2]], ifnotfound = NULL) } else { - type <- typeof(with(data, eval(dots[[i]][[3]]))) - len_matches <- length(with(data, eval(dots[[i]][[2]]))) + value_type <- with(data, eval(dots[[i]][[3]])) + value_length <- with(data, eval(dots[[i]][[2]])) } + # if we have "NA", we don't want to check the type. Else, you cannot use + # "NA" for numeric recodes, but rather need to use "NA_real_", which is not + # user-friendly + if (is.na(value_type)) { + type <- NULL + } else { + type <- typeof(value_type) + } + len_matches <- length(value_length) + # save type and length of recode values all_recodes <- c(all_recodes, type) all_same_length <- c(all_same_length, len_matches) } diff --git a/tests/testthat/test-recode_into.R b/tests/testthat/test-recode_into.R index 90fabcd2f..b9b0d4da3 100644 --- a/tests/testthat/test-recode_into.R +++ b/tests/testthat/test-recode_into.R @@ -136,7 +136,7 @@ test_that("recode_into, works inside functions", { test <- function() { set.seed(123) d <- data.frame( - x = sample(1:5, 30, TRUE), + x = sample.int(5, 30, TRUE), y = sample(letters[1:5], 30, TRUE), stringsAsFactors = FALSE ) @@ -258,3 +258,18 @@ test_that("recode_into, make sure recode works with missing in original variable ) ) }) + +test_that("recode_into, NA doesn't need to be of exact type", { + data(mtcars) + x1 <- recode_into( + mpg > 10 ~ 1, + gear == 5 ~ NA_real_, + data = mtcars + ) + x2 <- recode_into( + mpg > 10 ~ 1, + gear == 5 ~ NA, + data = mtcars + ) + expect_identical(x1, x2) +})