Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOn't check type of NA values #497

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.10.0
Version: 0.10.0.1
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 14 additions & 4 deletions R/recode_into.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,22 @@
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)
}
Expand Down Expand Up @@ -222,7 +232,7 @@
# don't show msg again
overwrite_NA_msg <- FALSE
insight::format_alert(
"Missing values in original variable are overwritten by default value. If you want to preserve missing values, set `preserve_na = TRUE`."

Check warning on line 235 in R/recode_into.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/recode_into.R,line=235,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 147 characters.
)
}
}
Expand Down
17 changes: 16 additions & 1 deletion tests/testthat/test-recode_into.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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)
})
Loading