diff --git a/NAMESPACE b/NAMESPACE index 9519896df..04f75d303 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -181,7 +181,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) diff --git a/R/data_restoretype.R b/R/data_restoretype.R index 9b5eb71a9..fa3ee9c7d 100644 --- a/R/data_restoretype.R +++ b/R/data_restoretype.R @@ -21,13 +21,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]] <- coerce_to_numeric(data[[col]]) + data[[col]] <- .coerce_to_numeric(data[[col]]) } } else { if (is.factor(reference[[col]]) && !is.factor(data[[col]])) { @@ -36,11 +35,11 @@ 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]] <- .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/data_to_long.R b/R/data_to_long.R index 0811f426c..7c828262f 100644 --- a/R/data_to_long.R +++ b/R/data_to_long.R @@ -166,7 +166,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] diff --git a/R/to_numeric.R b/R/to_numeric.R index 687a83ca6..849bef830 100644 --- a/R/to_numeric.R +++ b/R/to_numeric.R @@ -266,20 +266,8 @@ to_numeric.character <- function(x, } - -#' 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) { +#' @keywords internal +.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")) -}