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

Default values of to_numeric() #547

Merged
merged 1 commit into from
Oct 2, 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
18 changes: 9 additions & 9 deletions R/to_numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
#'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions man/datawizard-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/to_numeric.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/data_to_numeric.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-data_to_numeric.R
Original file line number Diff line number Diff line change
@@ -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))
})

Expand Down Expand Up @@ -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", {
Expand All @@ -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"
Expand Down
Loading