-
Notifications
You must be signed in to change notification settings - Fork 1
/
01_load_prescriptions_MH.R
118 lines (89 loc) · 3.48 KB
/
01_load_prescriptions_MH.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Set working directory
setwd("~/GitHub/NHANES_depression/Data")
# load necessary libraries
library(foreign)
library(dplyr)
# LOAD AND MERGE ALL NECESSARY FILES PER SURVEY FOR PRESCRIPTION DRUGS
# dowload drug classes
drugclasses_file <- paste0("https://wwwn.cdc.gov/nchs/nhanes/1999-2000/RXQ_DRUG.XPT")
## MEDICATIONS
download.file(drugclasses_file, tf <- tempfile(), mode="wb")
drugclasses <- foreign::read.xport(tf)
keep_vars <- c("RXDDRGID", "RXDDCN1A", "RXDDCN1B")
drugclasses <- drugclasses[,keep_vars]
######################################
########## NHANES 2007-2008 ##########
######################################
years <- "2007-2008"
letter <- "E"
med_file <- paste0("https://wwwn.cdc.gov/nchs/nhanes/",years,"/RXQ_RX_",letter,".XPT")
## MEDICATIONS
download.file(med_file, tf <- tempfile(), mode="wb")
meds_2007_2008 <- foreign::read.xport(tf)
keep_vars <- c("SEQN", "RXDUSE", "RXDDRUG", "RXDDRGID")
meds_2007_2008 <- meds_2007_2008[,keep_vars]
######################################
########## NHANES 2009-2010 ##########
######################################
years <- "2009-2010"
letter <- "F"
med_file <- paste0("https://wwwn.cdc.gov/nchs/nhanes/",years,"/RXQ_RX_",letter,".XPT")
## MEDICATIONS
download.file(med_file, tf <- tempfile(), mode="wb")
meds_2009_2010 <- foreign::read.xport(tf)
keep_vars <- c("SEQN", "RXDUSE", "RXDDRUG", "RXDDRGID")
meds_2009_2010 <- meds_2009_2010[,keep_vars]
######################################
########## NHANES 2011-2012 ##########
######################################
years <- "2011-2012"
letter <- "G"
med_file <- paste0("https://wwwn.cdc.gov/nchs/nhanes/",years,"/RXQ_RX_",letter,".XPT")
## MEDICATIONS
download.file(med_file, tf <- tempfile(), mode="wb")
meds_2011_2012 <- foreign::read.xport(tf)
keep_vars <- c("SEQN", "RXDUSE", "RXDDRUG", "RXDDRGID")
meds_2011_2012 <- meds_2011_2012[,keep_vars]
######################################
########## NHANES 2013-2014 ##########
######################################
years <- "2013-2014"
letter <- "H"
med_file <- paste0("https://wwwn.cdc.gov/nchs/nhanes/",years,"/RXQ_RX_",letter,".XPT")
## MEDICATIONS
download.file(med_file, tf <- tempfile(), mode="wb")
meds_2013_2014 <- foreign::read.xport(tf)
keep_vars <- c("SEQN", "RXDUSE", "RXDDRUG", "RXDDRGID")
meds_2013_2014 <- meds_2013_2014[,keep_vars]
######################################
########## NHANES 2015-2016 ##########
######################################
years <- "2015-2016"
letter <- "I"
med_file <- paste0("https://wwwn.cdc.gov/nchs/nhanes/",years,"/RXQ_RX_",letter,".XPT")
## MEDICATIONS
download.file(med_file, tf <- tempfile(), mode="wb")
meds_2015_2016 <- foreign::read.xport(tf)
keep_vars <- c("SEQN", "RXDUSE", "RXDDRUG", "RXDDRGID")
meds_2015_2016 <- meds_2015_2016[,keep_vars]
######################################
########## NHANES 2017-2018 ##########
######################################
years <- "2017-2018"
letter <- "J"
med_file <- paste0("https://wwwn.cdc.gov/nchs/nhanes/",years,"/RXQ_RX_",letter,".XPT")
## MEDICATIONS
download.file(med_file, tf <- tempfile(), mode="wb")
meds_2017_2018 <- foreign::read.xport(tf)
keep_vars <- c("SEQN", "RXDUSE", "RXDDRUG", "RXDDRGID")
meds_2017_2018 <- meds_2017_2018[,keep_vars]
# merging files
meds_df <- rbind(meds_2007_2008,
meds_2009_2010,
meds_2011_2012,
meds_2013_2014,
meds_2015_2016,
meds_2017_2018)
# adding drug class
merged <- left_join(meds_df, drugclasses, by = "RXDDRGID")
saveRDS(merged, "meds_df.rds")