Skip to content

Instantly share code, notes, and snippets.

@markdanese

markdanese/part_d.R

Last active Oct 8, 2015
Embed
What would you like to do?
script to read in Medicare Part D Prescriber data for 2013
# ---------- US Part D Drug prices 2013 ---------- #
library(data.table)
library(magrittr)
# data from http://download.cms.gov/Research-Statistics-Data-and-Systems/Statistics-Trends-and-Reports/Medicare-Provider-Charge-Data/Downloads/PartD_Prescriber_PUF_NPI_DRUG_13.zip
# 500 MB ZIP file download, 2.9 GB uncompressed
pde <- "http://download.cms.gov/Research-Statistics-Data-and-Systems/Statistics-Trends-and-Reports/Medicare-Provider-Charge-Data/Downloads/PartD_Prescriber_PUF_NPI_DRUG_13.zip"
tf <- tempfile()
download.file(pde, tf)
x <- unzip(tf, exdir = tempdir())
z <- fread(x[2], verbose = TRUE)
unlink(x) # removes the tempfiles
rm(x)
setkey(z, "GENERIC_NAME")
partd <-
z[, .(
claims = sum(TOTAL_CLAIM_COUNT, na.rm = TRUE),
days_supp = sum(TOTAL_DAY_SUPPLY, na.rm = TRUE),
tot_cost = sum(TOTAL_DRUG_COST, na.rm = TRUE)
),
by = .(drug = GENERIC_NAME)] %>%
.[, drug := tolower(drug)] %>%
.[, cost_per_day := round(tot_cost / days_supp, 2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment