R/round_columns.R

Defines functions round_columns

Documented in round_columns

#' round columns
#' @param df data frame
#' @param col_list list of vectors of columns and number of digits to round each
#' @details 
#' col_list should contain a list of list tuples, where the first element
#' is a vector of column names to round, and the second value should be the number
#' of digits to round to
#' @examples
#' \dontrun{
#' col_list <- list(
#' list("AGE", 1), 
#' list(c("WEIGHT",
#'        "SCREATININE",
#'        "SERUMALT", 
#'        "DELDBP",
#'        "DELSBP"),
#'      2)
#'  )
#' aht_trial_rounded <- round_columns(aht_trial2, col_list)
#' }
#' @export
round_columns <- function(df, col_list) {
  round_col_group <- function(list_tuple) {
    if (!is.numeric(list_tuple[[2]])) {
      stop(paste("Rounding value must be numeric, instead of provided value of:", list_tuple[[2]]))
    }
    for (col in list_tuple[[1]])  {
      if (col %in% names(df)) {
        df[[col]] <<- round(df[[col]], list_tuple[[2]])
      } else {
        warning(paste("No column with name:", col, "detected"))
      }
    }
  }
  lapply(col_list, round_col_group)
  return(df)
}

Try the PKPDmisc package in your browser

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

PKPDmisc documentation built on April 14, 2020, 5:49 p.m.