R/statistics_of_medicare_payments.R

Defines functions average_medicare_payments_statistics

Documented in average_medicare_payments_statistics

#' Average Medicare Payments Statistics
#'
#' This function is to calculate the \code{statistics} over all of the DRG codes for average medicare payments of data from Centers for Medicare & Medical Services.
#'
#' @param df a dataframe of data from Centers for Medicare & Medical Services. This dataframe must contain DRG codes and average medicare payments data.
#' And the column  names of these two variables should be "DRG.Definition" and  "Average.Medicare.Payments".
#' @param statistics a character string specifying the type of \code{statistics}, must be one of "mean","median" or "sd".
#'
#' @return a dataframe with DRG codes and \code{statistics} for average medicare payments.
#' @export
#'
#' @importFrom dplyr
#'
#' @examples
#' data(sample_data)
#'
#' average_medicare_payments_statistics(drg_data, 'sd')
#'
#'
average_medicare_payments_statistics <- function(df, statistics){

  result <- df %>%
    group_by(DRG.Definition) %>%
    summarise(mean = mean(Average.Medicare.Payments),
              median = median(Average.Medicare.Payments),
              sd = sd(Average.Medicare.Payments)) %>%
    select(DRG.Definition, all_of(statistics))

  return(result)
}
yinglia/A-Simple-R-Package documentation built on April 17, 2022, 12:08 a.m.