R/round_numeric.R

Defines functions factor_coerce round_numeric

Documented in factor_coerce round_numeric

#' Function to round all numerical variables in a data frame.
#' 
#' \code{round_numeric} is useful when many variables are contained within a 
#' data frame  and all numerical values want to be rounded without individual 
#' naming.
#'
#' @param df A data frame containing numeric variables.
#' @param round Number of decimal places, used in \code{round}. Default is 1.
#' 
#' @author Stuart K. Grange
#' 
#' @examples
#' 
#' # Round all numeric variables to two decimal places
#' \dontrun{
#' data_tidy_round <- round_numeric(data_tidy, round = 2)
#' 
#' # or, make all numeric variables integers
#' data_tidy_integer <- round_numeric(data_tidy, round = 0)
#' 
#' }
#' 
#' @export
round_numeric <- function(df, round = 1) {
  
  .Deprecated("mutate_if", package = "dplyr")
  
  # Get index
  index <- sapply(df, is.numeric)
  
  # Apply function to all the numeric variables in data frame
  df[index] <- lapply(df[index], function (x) round(x, round))
  
  return(df)
  
}


#' @rdname round_numeric
#' 
#' @export
factor_coerce <- function(df) {
  
  .Deprecated("mutate_if", package = "dplyr")
  
  # Get index
  index <- sapply(df, is.factor)
  
  # Apply function to all the numeric variables in data frame
  df[index] <- lapply(df[index], as.character)
  
  return(df)
  
}
skgrange/threadr documentation built on May 11, 2024, 12:16 p.m.