From 5428ad6c7233477ccbb3d5c01ccae8f0c4d1877d Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sat, 20 Aug 2022 18:09:19 +0200 Subject: [PATCH 1/4] Remove `coerce_to_numeric()` Closes #206 --- NAMESPACE | 1 - R/data_reshape.R | 2 +- R/data_restoretype.R | 4 ++-- R/to_numeric.R | 20 -------------------- man/coerce_to_numeric.Rd | 22 ---------------------- 5 files changed, 3 insertions(+), 46 deletions(-) delete mode 100644 man/coerce_to_numeric.Rd diff --git a/NAMESPACE b/NAMESPACE index 79473847d..c378bc2e9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -158,7 +158,6 @@ export(center) export(centre) export(change_code) export(change_scale) -export(coerce_to_numeric) export(colnames_to_row) export(column_as_rownames) export(compact_character) diff --git a/R/data_reshape.R b/R/data_reshape.R index df6bce0f9..5a78bfc70 100644 --- a/R/data_reshape.R +++ b/R/data_reshape.R @@ -148,7 +148,7 @@ data_to_long <- function(data, # Reshaping --------------------- # Create Index column as needed by reshape - data[["_Row"]] <- coerce_to_numeric(row.names(data)) + data[["_Row"]] <- as.numeric(row.names(data)) # Create a new index for cases with length(names_to) > 1 names_to_2 <- paste(names_to, collapse = "_") diff --git a/R/data_restoretype.R b/R/data_restoretype.R index 881b84cec..4596f51d0 100644 --- a/R/data_restoretype.R +++ b/R/data_restoretype.R @@ -26,7 +26,7 @@ data_restoretype <- function(data, reference = NULL, ...) { # No reference data (regular fixing) ---------------- if (is.null(reference)) { if (is.character(data[[col]])) { - data[[col]] <- coerce_to_numeric(data[[col]]) + data[[col]] <- as.numeric(data[[col]]) } } else { if (is.factor(reference[[col]]) && !is.factor(data[[col]])) { @@ -35,7 +35,7 @@ data_restoretype <- function(data, reference = NULL, ...) { } if (is.numeric(reference[[col]]) && !is.numeric(data[[col]])) { - data[[col]] <- coerce_to_numeric(as.character(data[[col]])) + data[[col]] <- as.numeric(as.character(data[[col]])) } if (is.character(reference[[col]]) && !is.character(data[[col]])) { diff --git a/R/to_numeric.R b/R/to_numeric.R index 39d55a9e4..940659a2c 100644 --- a/R/to_numeric.R +++ b/R/to_numeric.R @@ -251,23 +251,3 @@ to_numeric.character <- function(x, out } - - -#' Convert to Numeric (if possible) -#' -#' Tries to convert vector to numeric if possible (if no warnings or errors). -#' Otherwise, leaves it as is. -#' -#' @param x A vector to be converted. -#' -#' @examples -#' coerce_to_numeric(c("1", "2")) -#' coerce_to_numeric(c("1", "2", "A")) -#' @return Numeric vector (if possible) -#' @export -coerce_to_numeric <- function(x) { - tryCatch(as.numeric(as.character(x)), - error = function(e) x, - warning = function(w) x - ) -} diff --git a/man/coerce_to_numeric.Rd b/man/coerce_to_numeric.Rd deleted file mode 100644 index 553a16bfc..000000000 --- a/man/coerce_to_numeric.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/to_numeric.R -\name{coerce_to_numeric} -\alias{coerce_to_numeric} -\title{Convert to Numeric (if possible)} -\usage{ -coerce_to_numeric(x) -} -\arguments{ -\item{x}{A vector to be converted.} -} -\value{ -Numeric vector (if possible) -} -\description{ -Tries to convert vector to numeric if possible (if no warnings or errors). -Otherwise, leaves it as is. -} -\examples{ -coerce_to_numeric(c("1", "2")) -coerce_to_numeric(c("1", "2", "A")) -} From 2d7e441f36ba499e17af456f1002f517d5e658a0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 21 Aug 2022 09:47:39 +0200 Subject: [PATCH 2/4] make `coerce_to_numeric()` internal --- R/data_reshape.R | 9 +++++++-- R/data_restoretype.R | 7 +++---- R/to_numeric.R | 12 +++++++++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/R/data_reshape.R b/R/data_reshape.R index 5a78bfc70..3635152a0 100644 --- a/R/data_reshape.R +++ b/R/data_reshape.R @@ -148,7 +148,7 @@ data_to_long <- function(data, # Reshaping --------------------- # Create Index column as needed by reshape - data[["_Row"]] <- as.numeric(row.names(data)) + data[["_Row"]] <- .coerce_to_numeric(row.names(data)) # Create a new index for cases with length(names_to) > 1 names_to_2 <- paste(names_to, collapse = "_") @@ -363,7 +363,12 @@ data_to_wide <- function(data, # Create an id for stats::reshape if (is.null(id_cols)) { - data[["_Rows"]] <- apply(data[, !names(data) %in% c(values_from, names_from), drop = FALSE], 1, paste, collapse = "_") + data[["_Rows"]] <- apply( + data[, !names(data) %in% c(values_from, names_from), drop = FALSE], + MARGIN = 1, + paste, + collapse = "_" + ) id_cols <- "_Rows" } diff --git a/R/data_restoretype.R b/R/data_restoretype.R index 4596f51d0..db8d1f22b 100644 --- a/R/data_restoretype.R +++ b/R/data_restoretype.R @@ -20,13 +20,12 @@ #' fixed <- data_restoretype(data, reference = iris) #' summary(fixed) #' @export - data_restoretype <- function(data, reference = NULL, ...) { for (col in names(data)) { # No reference data (regular fixing) ---------------- if (is.null(reference)) { if (is.character(data[[col]])) { - data[[col]] <- as.numeric(data[[col]]) + data[[col]] <- .coerce_to_numeric(data[[col]]) } } else { if (is.factor(reference[[col]]) && !is.factor(data[[col]])) { @@ -35,11 +34,11 @@ data_restoretype <- function(data, reference = NULL, ...) { } if (is.numeric(reference[[col]]) && !is.numeric(data[[col]])) { - data[[col]] <- as.numeric(as.character(data[[col]])) + data[[col]] <- .coerce_to_numeric(as.character(data[[col]])) } if (is.character(reference[[col]]) && !is.character(data[[col]])) { - data[[col]] <- as.character(data[[col]]) + data[[col]] <- .coerce_to_numeric(data[[col]]) } } } diff --git a/R/to_numeric.R b/R/to_numeric.R index 6cb759aec..2437daa45 100644 --- a/R/to_numeric.R +++ b/R/to_numeric.R @@ -45,7 +45,9 @@ to_numeric <- function(x, ...) { #' @export to_numeric.default <- function(x, verbose = TRUE, ...) { if (isTRUE(verbose)) { - message(insight::format_message(sprintf("Converting into numeric values currently not possible for variables of class '%s'.", class(x)[1]))) + message(insight::format_message( + sprintf("Converting into numeric values currently not possible for variables of class '%s'.", class(x)[1]) + )) } x } @@ -243,3 +245,11 @@ to_numeric.character <- function(x, out } + +#' @keywords internal +.coerce_to_numeric <- function(x) { + tryCatch(as.numeric(as.character(x)), + error = function(e) x, + warning = function(w) x + ) +} From a25dc06cd2f2e49936e141195cfc0c3ca4a4c611 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Thu, 15 Sep 2022 14:28:23 +0200 Subject: [PATCH 3/4] Fix build failures --- DESCRIPTION | 2 +- NAMESPACE | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f4a734492..d8b21cfac 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -63,7 +63,7 @@ VignetteBuilder: Encoding: UTF-8 Language: en-US Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.1.9000 +RoxygenNote: 7.2.1 Config/testthat/edition: 3 Config/Needs/website: rstudio/bslib, diff --git a/NAMESPACE b/NAMESPACE index 4b0589891..4d73ec2c3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -166,7 +166,6 @@ export(centre) export(change_code) export(change_scale) export(coef_var) -export(coerce_to_numeric) export(colnames_to_row) export(column_as_rownames) export(compact_character) From 1bc807d69371e0f257465f89b419e5344d951fff Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 27 Oct 2022 18:57:24 +0200 Subject: [PATCH 4/4] use interal --- R/data_to_long.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/data_to_long.R b/R/data_to_long.R index 49cd66e52..a72ddcefa 100644 --- a/R/data_to_long.R +++ b/R/data_to_long.R @@ -163,7 +163,7 @@ data_to_long <- function(data, # create a temp id so that we know how to rearrange the rows once the data is # stacked not_stacked <- data[, not_selected, drop = FALSE] - not_stacked[["_Rows"]] <- coerce_to_numeric(row.names(data)) + not_stacked[["_Rows"]] <- .coerce_to_numeric(row.names(data)) # stack the selected columns stacked_data <- .stack(data[, cols, drop = FALSE])[, 2:1]