Learn R Programming

RNHANES (version 1.1.0)

nhanes_load_data: Download NHANES data files.

Description

Download NHANES data files.

Usage

nhanes_load_data(file_name, year, destination = tempdir(), demographics = FALSE, cache = TRUE, recode = FALSE, recode_data = FALSE, recode_demographics = FALSE, allow_duplicate_files = FALSE)

Arguments

file_name
NHANES file name (e.g. "EPH") or a vector of filenames (e.g c("EPH", "GHB"))
year
NHANES cycle year (e.g. "2007-2008") or a vector of cycle years
destination
directory to download the files to
demographics
include demographics data into the dataset
cache
whether to cache the file to disk
recode
whether to recode the data and demographics (overrides other parameters)
recode_data
whether to recode just the data
recode_demographics
whether to recode just the demographics
allow_duplicate_files
how to handle a request that has duplicate file names/cycle years. By default duplicates will be removed.

Value

if file_name or year is a vector, returns a list containing a data frame for each file_name. If file_name and year are both singletons, then a data frame is returned.

Details

If you supply vectors for both file_name and year, then the vectors are paired and each file_name/year pair is downloaded. For example, file_name = c("EPH, GHB"), year = c("2009-2010", "2011-2012") will download "EPH_F.XPT" and "EPH_G.XPT". In other words, the function does not download every possible combination of file_name and year.

You can specify file names in several formats. In order of specificity: You can supply the complete filename: "EPH_F.XPT" You can supply the filename without an extension: "EPH_F" You can supply the filename without a suffix: "EPH", year = "2009-2010"

If you are loading the same file across multiple years, you must supply the filename without a suffix so that the correct suffix for each year can be used.

This function returns either a list or a data frame. If you load multiple files, the return value will always be a list. This is because the columns may not match in between files. If you load one file, the result will be a data frame.

Examples

Run this code

## Not run: 
# 
# nhanes_load_data("UHG", "2011-2012")
# 
# # Load data with demographics
# nhanes_load_data("UHG", "2011-2012", demographics = TRUE)
# 
# # Download to /tmp directory and overwrite the file if it already exists
# nhanes_load_data("HDL_E", "2007-2008", destination = "/tmp", cache = FALSE)
# ## End(Not run)

Run the code above in your browser using DataLab