From ea2f16f98927c8265b3302eafbe0298484bc5622 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 2 Oct 2024 16:32:57 +0200 Subject: [PATCH] Default values of `to_numeric()` (#547) Fixes #544 --- R/to_numeric.R | 18 +++++++++--------- man/datawizard-package.Rd | 10 +++++----- man/to_numeric.Rd | 16 ++++++++-------- tests/testthat/_snaps/data_to_numeric.md | 4 ++-- tests/testthat/test-data_to_numeric.R | 8 ++++---- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/R/to_numeric.R b/R/to_numeric.R index e38e12e80..3e75bccbd 100644 --- a/R/to_numeric.R +++ b/R/to_numeric.R @@ -17,11 +17,11 @@ #' @inheritParams extract_column_names #' @inheritParams categorize #' -#' @note By default, `to_numeric()` converts factors into "binary" dummies, i.e. +#' @note When factors should be converted into multiple "binary" dummies, i.e. #' each factor level is converted into a separate column filled with a binary -#' 0-1 value. If only one column is required, use `dummy_factors = FALSE`. If -#' you want to preserve the original factor levels (in case these represent -#' numeric values), use `preserve_levels = TRUE`. +#' 0-1 value, set `dummy_factors = TRUE`. If you want to preserve the original +#' factor levels (in case these represent numeric values), use +#' `preserve_levels = TRUE`. #' #' @section Selection of variables - `select` argument: #' For most functions that have a `select` argument the complete input data @@ -34,12 +34,12 @@ #' #' @examples #' to_numeric(head(ToothGrowth)) -#' to_numeric(head(ToothGrowth), dummy_factors = FALSE) +#' to_numeric(head(ToothGrowth), dummy_factors = TRUE) #' #' # factors #' x <- as.factor(mtcars$gear) -#' to_numeric(x, dummy_factors = FALSE) -#' to_numeric(x, dummy_factors = FALSE, preserve_levels = TRUE) +#' to_numeric(x) +#' to_numeric(x, preserve_levels = TRUE) #' # same as: #' coerce_to_numeric(x) #' @@ -69,7 +69,7 @@ to_numeric.default <- function(x, verbose = TRUE, ...) { to_numeric.data.frame <- function(x, select = NULL, exclude = NULL, - dummy_factors = TRUE, + dummy_factors = FALSE, preserve_levels = FALSE, lowest = NULL, append = FALSE, @@ -191,7 +191,7 @@ to_numeric.POSIXlt <- to_numeric.Date #' @export to_numeric.factor <- function(x, - dummy_factors = TRUE, + dummy_factors = FALSE, preserve_levels = FALSE, lowest = NULL, verbose = TRUE, diff --git a/man/datawizard-package.Rd b/man/datawizard-package.Rd index db38bc334..d389df6ac 100644 --- a/man/datawizard-package.Rd +++ b/man/datawizard-package.Rd @@ -33,16 +33,16 @@ Useful links: Authors: \itemize{ - \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) (@patilindrajeets) - \item Dominique Makowski \email{dom.makowski@gmail.com} (\href{https://orcid.org/0000-0001-5375-9967}{ORCID}) (@Dom_Makowski) - \item Daniel Lüdecke \email{d.luedecke@uke.de} (\href{https://orcid.org/0000-0002-8895-3206}{ORCID}) (@strengejacke) + \item Indrajeet Patil \email{patilindrajeet.science@gmail.com} (\href{https://orcid.org/0000-0003-1995-6531}{ORCID}) + \item Dominique Makowski \email{dom.makowski@gmail.com} (\href{https://orcid.org/0000-0001-5375-9967}{ORCID}) + \item Daniel Lüdecke \email{d.luedecke@uke.de} (\href{https://orcid.org/0000-0002-8895-3206}{ORCID}) \item Mattan S. Ben-Shachar \email{matanshm@post.bgu.ac.il} (\href{https://orcid.org/0000-0002-4287-4801}{ORCID}) - \item Brenton M. Wiernik \email{brenton@wiernik.org} (\href{https://orcid.org/0000-0001-9560-6336}{ORCID}) (@bmwiernik) + \item Brenton M. Wiernik \email{brenton@wiernik.org} (\href{https://orcid.org/0000-0001-9560-6336}{ORCID}) } Other contributors: \itemize{ - \item Rémi Thériault \email{remi.theriault@mail.mcgill.ca} (\href{https://orcid.org/0000-0003-4315-6788}{ORCID}) (@rempsyc) [contributor] + \item Rémi Thériault \email{remi.theriault@mail.mcgill.ca} (\href{https://orcid.org/0000-0003-4315-6788}{ORCID}) [contributor] \item Thomas J. Faulkenberry \email{faulkenberry@tarleton.edu} [reviewer] \item Robert Garrett \email{rcg4@illinois.edu} [reviewer] } diff --git a/man/to_numeric.Rd b/man/to_numeric.Rd index 7478c9579..634906e4a 100644 --- a/man/to_numeric.Rd +++ b/man/to_numeric.Rd @@ -11,7 +11,7 @@ to_numeric(x, ...) x, select = NULL, exclude = NULL, - dummy_factors = TRUE, + dummy_factors = FALSE, preserve_levels = FALSE, lowest = NULL, append = FALSE, @@ -107,11 +107,11 @@ either numeric levels or dummy variables. The "counterpart" to convert variables into factors is \code{to_factor()}. } \note{ -By default, \code{to_numeric()} converts factors into "binary" dummies, i.e. +When factors should be converted into multiple "binary" dummies, i.e. each factor level is converted into a separate column filled with a binary -0-1 value. If only one column is required, use \code{dummy_factors = FALSE}. If -you want to preserve the original factor levels (in case these represent -numeric values), use \code{preserve_levels = TRUE}. +0-1 value, set \code{dummy_factors = TRUE}. If you want to preserve the original +factor levels (in case these represent numeric values), use +\code{preserve_levels = TRUE}. } \section{Selection of variables - \code{select} argument}{ @@ -126,12 +126,12 @@ to also include the original variables in the returned data frame. \examples{ to_numeric(head(ToothGrowth)) -to_numeric(head(ToothGrowth), dummy_factors = FALSE) +to_numeric(head(ToothGrowth), dummy_factors = TRUE) # factors x <- as.factor(mtcars$gear) -to_numeric(x, dummy_factors = FALSE) -to_numeric(x, dummy_factors = FALSE, preserve_levels = TRUE) +to_numeric(x) +to_numeric(x, preserve_levels = TRUE) # same as: coerce_to_numeric(x) diff --git a/tests/testthat/_snaps/data_to_numeric.md b/tests/testthat/_snaps/data_to_numeric.md index 42cb00b67..e963890a5 100644 --- a/tests/testthat/_snaps/data_to_numeric.md +++ b/tests/testthat/_snaps/data_to_numeric.md @@ -1,7 +1,7 @@ # convert data frame to numeric Code - to_numeric(head(ToothGrowth)) + to_numeric(head(ToothGrowth), dummy_factors = TRUE) Output len supp.OJ supp.VC dose 1 4.2 0 1 0.5 @@ -27,7 +27,7 @@ # convert factor to numeric Code - to_numeric(f) + to_numeric(f, dummy_factors = TRUE) Output a c i s t 1 0 0 0 1 0 diff --git a/tests/testthat/test-data_to_numeric.R b/tests/testthat/test-data_to_numeric.R index 464c35e8d..816591ac0 100644 --- a/tests/testthat/test-data_to_numeric.R +++ b/tests/testthat/test-data_to_numeric.R @@ -1,5 +1,5 @@ test_that("convert data frame to numeric", { - expect_snapshot(to_numeric(head(ToothGrowth))) + expect_snapshot(to_numeric(head(ToothGrowth), dummy_factors = TRUE)) expect_snapshot(to_numeric(head(ToothGrowth), dummy_factors = FALSE)) }) @@ -41,7 +41,7 @@ test_that("convert character to numeric lowest", { test_that("convert factor to numeric", { f <- factor(substring("statistics", 1:10, 1:10)) - expect_snapshot(to_numeric(f)) + expect_snapshot(to_numeric(f, dummy_factors = TRUE)) }) test_that("convert factor to numeric", { @@ -67,12 +67,12 @@ test_that("convert factor to numeric, dummy factors", { test_that("convert factor to numeric, append", { data(efc) expect_identical( - colnames(to_numeric(efc)), + colnames(to_numeric(efc, dummy_factors = TRUE)), c("c12hour", "e16sex", "e42dep.1", "e42dep.2", "e42dep.3", "e42dep.4", "c172code", "neg_c_7"), ignore_attr = TRUE ) expect_identical( - colnames(to_numeric(efc, append = TRUE)), + colnames(to_numeric(efc, dummy_factors = TRUE, append = TRUE)), c( "c12hour", "e16sex", "e42dep", "c172code", "neg_c_7", "e42dep_n", "e42dep_n.1", "e42dep_n.2", "e42dep_n.3", "e42dep_n.4"