D Merge Downloads into a Master file

Examples for downloading and merging NHANES .XPT data files from the following data:

Documentation links:

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",
  tf <- tempfile(), mode="wb")
# Create Data Frame From Temporary File
DEMO_I <- foreign::read.xport(tf)
############################################
# REPEAT For:
# BMX_I
download.file(
  "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/BMX_I.XPT", 
  tf2 <- tempfile(), mode="wb")
BMX_I <- foreign::read.xport(tf2) # TMP file
# PFAS_I   ##################################
download.file(
  "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/PFAS_I.XPT", 
  tf3 <- tempfile(), mode="wb")
PFAS_I <- foreign::read.xport(tf3) # TMP file
# TCHOL_I  ##################################
download.file(
  "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/TCHOL_I.XPT", 
  tf4 <- tempfile(), mode="wb")
TCHOL_I <- foreign::read.xport(tf4) # TMP file
# ALB_CR_I  ##################################
download.file(
  "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/ALB_CR_I.XPT", 
  tf5 <- tempfile(), mode="wb")
ALB_CR_I <- foreign::read.xport(tf5) # TMP file

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.

Master1 <- merge(DEMO_I, BMX_I, all.x=TRUE)
Master2 <- merge(Master1, PFAS_I, all.x=TRUE)
Master3 <- merge(Master2, TCHOL_I, all.x=TRUE)
Master4 <- merge(Master3, ALB_CR_I, all.x=TRUE)

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)
#
DEMO_I <- read_xpt(url(
  "https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT"))
#
BMX_I <- read_xpt(url(
  "https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/BMX_I.XPT"))
#
PFAS_I <- read_xpt(url(
  "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/PFAS_I.XPT"))
#
TCHOL_I <- read_xpt(url(
  "https://wwwn.cdc.gov/nchs/nhanes/2015-2016/TCHOL_I.XPT"))
#
ALB_CR_I  <- read_xpt(url(
  "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")