From 5428ad6c7233477ccbb3d5c01ccae8f0c4d1877d Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sat, 20 Aug 2022 18:09:19 +0200 Subject: [PATCH] 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")) -}