R/statistics.R

Defines functions DRG_statistic

#' Calculate Statsitics for all DRG Codes
#'
#' @param df
#' @param statistic
#'
#' @return data frame
#' @export
#'
#' @import dplyr
#' @importFrom purrr map_df
#'
#' @examples DRG_statistic(drg_data, 'mean')

DRG_statistic <- function(df, ## dataframe
                          statistic ##statistic to be calculated
){

  ##use the spread function to turn the data into wide format
  drg.data.wide <- df %>%
    select(DRG.Definition,
           Provider.Id,
           Provider.State,
           Average.Medicare.Payments) %>%
    spread(DRG.Definition, Average.Medicare.Payments)

  ##calculate the statsitic of interest over each of the columns of the dataframe
  ap.map <- drg.data.wide %>%
    select(-Provider.Id,
           -Provider.State) %>%
    map_df(get(statistic), na.rm = TRUE)

  ##return the results
  return(ap.map)

}
HongzheZhang/lab2 documentation built on Oct. 30, 2019, 6:47 p.m.