R/columns2numeric.R

Defines functions columns2numeric

Documented in columns2numeric

#' Check Whether a Scalar is Convertible to Numeric
#'
#' @param x A \code{\link{data.frame}}.
#' @return A \code{\link{data.frame}}.
#' @export
#' @family logical helpers
#' @family data.frame helpers
#' @examples
#' df <- data.frame(a = 1:24,
#'                  b = as.character(1:24),
#'                  c = paste(" ", 1:24, " "),
#'                  d = LETTERS[1:24]
#'                  )
#' df[2, 2]  <- df[3, 3] <- ""
#' df[3, 2]  <- df[4, 3] <- " "
#' columns2numeric(df)
columns2numeric <- function(x) {
    conv <- function(x) {
        if (all(sapply(x, is.numeric))) {
            res <- as.numeric(x)
        } else {
            res <- x
        }
        return(res)
    }
    res <- as.data.frame(lapply(x, conv))
    return(res)
}

Try the fritools package in your browser

Any scripts or data that you put into this service are public.

fritools documentation built on Nov. 3, 2026, 5:07 p.m.