Function for DRG codes in Medicare file

This markdown document downloads the CMS Medicare data and separates the first column into DRG codes. This will later be merged with the organ system to allow for analysis by organ system.

library(stringr)
library(purrr)
library(dplyr)
library(ggplot2)

url <- "https://www.cms.gov/Research-Statistics-Data-and-Systems/Statistics-Trends-and-Reports/Medicare-Provider-Charge-Data/Downloads/Inpatient_Data_2014_CSV.zip"
temp <- tempfile()
download.file(url, temp)
df <- read.csv(unz(temp, "Medicare_Provider_Charge_Inpatient_DRGALL_FY2014.csv"))
unlink(temp)

sep_col <- function(s)
{
  return(str_extract(s, "\\d+"))
}

df$DRG_code <- map_chr(df$DRG.Definition, sep_col)

df %>% group_by(DRG_code) %>% summarise(n=n()) %>%
  top_n(20,n) %>%
  ggplot(aes(x=reorder(DRG_code, -n), y=n))+geom_bar(stat='identity') +
  coord_flip()