R/df.round.R

Defines functions df.round

Documented in df.round

#' Data Frame Rounding of Numbers
#'
#' This function rounds all numeric columns in a data frame.
#'
#' @param data   a data frame.
#' @param ...    an expression indicating the variable names in \code{data},
#'               e.g., \code{df.round(dat, x1, x2, x3)}. Note that the operators
#'               \code{+}, \code{-}, \code{~}, \code{:}, \code{::}, and \code{!}
#'               can also be used to select variables, see 'Details' in the
#'               \code{\link{df.subset}} function.
#' @param digits an integer value indicating the number of decimal places to be
#'               used.
#' @param drop   logical: if \code{TRUE}, non-numeric columns are dropped from
#'               the data frame.
#' @param check  logical: if \code{TRUE} (default), argument specification is checked.
#'
#' @author
#' Takuya Yanagida \email{takuya.yanagida@@univie.ac.at}
#'
#' @seealso
#' \code{\link{df.check}}, \code{\link{df.duplicated}}, \code{\link{df.unique}},
#' \code{\link{df.head}}, \code{\link{df.tail}}, \code{\link{df.long}},
#' \code{\link{df.wide}}, \code{\link{df.merge}}, \code{\link{df.move}},
#' \code{\link{df.rbind}}, \code{\link{df.rename}}, \code{\link{df.sort}},
#' \code{\link{df.subset}}
#'
#' @references
#' Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) \emph{The New S Language}.
#' Wadsworth & Brooks/Cole.
#'
#' @return
#' Returns data frame \code{data} with numeric columns rounded to the number of
#' decimal places according to the argument 'digits'.
#'
#' @export
#'
#' @examples
#' # Example 1: Round numeric columns with 0 digits
#' df.round(penguins, digits = 0)
#'
#' # Example 2: Round numeric columns with 0 digits and drop non-numeric columns
#' df.round(penguins, digits = 0, drop = TRUE)
df.round <- function(data, ..., digits = 2, drop = FALSE, check = TRUE) {

  #_____________________________________________________________________________
  #
  # Initial Check ——————————————————————————————————————————————————————————————

  # Check if input 'data' is missing, NULL, or not data frame
  if (isTRUE(missing(data) || is.null(data)) || !is.data.frame(data)) { stop("Please specify a data frame for the argument 'data'", call. = FALSE) }

  #_____________________________________________________________________________
  #
  # Data ———————————————————————————————————————————————————————————————————————

  .data(data, ...) |> (\(p) { assign("data", p$data, envir = parent.frame()) })()

  #_____________________________________________________________________________
  #
  # Input Check ————————————————————————————————————————————————————————————————

  .check.input(logical = "drop", args = "digits", envir = environment(), input.check = check)

  #_____________________________________________________________________________
  #
  # Main Function ——————————————————————————————————————————————————————————————

  # Round numeric columns
  if (isTRUE(any(sapply(data, is.numeric)))) { vapply(data, is.numeric, FUN.VALUE = logical(1L)) |> (\(p) data[, p] <<- round(data[, p], digits = digits))() }

  # Omit non-numeric columns
  if (isTRUE(drop)) { vapply(data, is.numeric, FUN.VALUE = logical(1L)) |> (\(p) data <<- data[, p])() }

  #_____________________________________________________________________________
  #
  # Output —————————————————————————————————————————————————————————————————————

  return(data)

}

#_______________________________________________________________________________

Try the misty package in your browser

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

misty documentation built on Nov. 24, 2026, 5:06 p.m.