Exercise

NHANES dataset construction

As downloaded from the NHANES website, the NHANES datasets are available only as separate .XPT files, a native format to SAS. Luckily for us, the haven package exists.

Let's combine the NHANES Demographics, Medical Conditions, and Body Measures datasets, available in their raw .XPT format and accessible through the variables DEMO_file, MCQ_file, and BMX_file. Join all 3 datasets using the SEQN variable. A good way to do this is using Reduce(), which allows you to combine elements in a helpful way.

The joining code, which is provided for you does the following:

  • Creates a list of all 3 datasets (nhanes_demo, nhanes_medical, nhanes_bodymeasures).
  • Uses a custom function inside of Reduce() to inner join all 3 datasets with the "SEQN" variable.
  • Saves this as the nhanes_combined dataset.

Instructions

100 XP
  • Load the haven package.
  • Import the three data files with separate calls to read_xpt(), where the inputs to these 3 calls to read_xpt() are DEMO_file, MCQ_file, and BMX_file and saved as the datasets as nhanes_demo, nhanes_medical, and nhanes_bodymeasures, respectively.
  • Create nhanes_combined by merging the 3 datasets you just imported, using the provided code.