D Merge Downloads into a Master file
Examples for downloading and merging NHANES .XPT
data files from the following data:
Documentation links:
DEMO_I
: https://wwwn.cdc.gov/nchs/nhanes/2015-2016/DEMO_I.htmBMI_I
: https://wwwn.cdc.gov/nchs/nhanes/2015-2016/BMX_I.htmPFAS_I
: https://wwwn.cdc.gov/nchs/nhanes/2015-2016/PFAS_I.htmTCHOL_I
: https://wwwn.cdc.gov/nchs/nhanes/2015-2016/TCHOL_I.htmALB_CR_I
: https://wwwn.cdc.gov/nchs/nhanes/2015-2016/ALB_CR_I.htm
D.1 Download into R object with NHANES code
Downloaded data is saved in a temporary file in a temporary directory and tf
simply holds the name/path to that data. Example on a Mac:
> tf
[1] "/var/folders/zg/9hl9fx_n7b970gcj51t8tkq1xx62d5/T//RtmpL6j3Wp/file5d87195178e"
# Download NHANES 2015-2016 to temporary file: DEMO_I
download.file(
"https://wwwn.cdc.gov/nchs/nhanes/2015-2016/DEMO_I.XPT",
<- tempfile(), mode="wb")
tf # Create Data Frame From Temporary File
<- foreign::read.xport(tf)
DEMO_I ############################################
# REPEAT For:
# BMX_I
download.file(
"https://wwwn.cdc.gov/nchs/nhanes/2015-2016/BMX_I.XPT",
<- tempfile(), mode="wb")
tf2 <- foreign::read.xport(tf2) # TMP file
BMX_I # PFAS_I ##################################
download.file(
"https://wwwn.cdc.gov/nchs/nhanes/2015-2016/PFAS_I.XPT",
<- tempfile(), mode="wb")
tf3 <- foreign::read.xport(tf3) # TMP file
PFAS_I # TCHOL_I ##################################
download.file(
"https://wwwn.cdc.gov/nchs/nhanes/2015-2016/TCHOL_I.XPT",
<- tempfile(), mode="wb")
tf4 <- foreign::read.xport(tf4) # TMP file
TCHOL_I # ALB_CR_I ##################################
download.file(
"https://wwwn.cdc.gov/nchs/nhanes/2015-2016/ALB_CR_I.XPT",
<- tempfile(), mode="wb")
tf5 <- foreign::read.xport(tf5) # TMP file ALB_CR_I
D.2 Combine files into Master
Use merge()
function. The SEQN
column is common to all and merge()
will automatically identify it. all.x=TRUE
will keep all rows and fill non existent data with NA
so that all data are kept.
<- merge(DEMO_I, BMX_I, all.x=TRUE)
Master1 <- merge(Master1, PFAS_I, all.x=TRUE)
Master2 <- merge(Master2, TCHOL_I, all.x=TRUE)
Master3 <- merge(Master3, ALB_CR_I, all.x=TRUE) Master4
D.3 Save/Write Master file to disk
If available use write_csv()
function (dplyr
package) which is “twice as fast as write.csv()
, and never writes row names. For example to export the data as .csv
within the current directory:
library(dplyr)
write_csv(Master4, "Master4.csv")
Base R
version:
write.csv(Master4, "Master4.csv")
D.4 Alternate download to R object with haven
Using the haven
package the code may look and feel easier as it only requires one line per file .
library(haven)
#
<- read_xpt(url(
DEMO_I "https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT"))
#
<- read_xpt(url(
BMX_I "https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/BMX_I.XPT"))
#
<- read_xpt(url(
PFAS_I "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/PFAS_I.XPT"))
#
<- read_xpt(url(
TCHOL_I "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/TCHOL_I.XPT"))
#
<- read_xpt(url(
ALB_CR_I "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/ALB_CR_I.XPT"))
D.5 Download, save XPT files to hard drive
To just download the .XPT
files on your hard drive:
download.file("https://wwwn.cdc.gov/nchs/nhanes/2015-2016/DEMO_I.XPT",
"DEMO_I.XPT")
download.file("https://wwwn.cdc.gov/nchs/nhanes/2015-2016/BMX_I.XPT",
"BMX_I.XPT")
download.file("https://wwwn.cdc.gov/nchs/nhanes/2015-2016/PFAS_I.XPT",
"PFAS_I.XPT")
download.file("https://wwwn.cdc.gov/nchs/nhanes/2015-2016/ALB_CR_I.XPT",
"ALB_CR_I.XPT")
download.file("https://wwwn.cdc.gov/nchs/nhanes/2015-2016/TCHOL_I.XPT",
"TCHOL_I.XPT")