# ----------------------------------------------------------------------------- # R programming statements for h205 data # # This file contains programming statements needed to import the ASCII data # file (.dat) into R. The R programming language has the capability to produce # appropriate standard errors for estimates from a survey with a complex sample # design such as the Medical Expenditure Panel Survey (MEPS). # # The input file is the ASCII data file (h205.dat) supplied in this PUF # release, which can be extracted from the .zip file supplied at the MEPS # website: https://meps.ahrq.gov/mepsweb/data_stats/download_data_files.jsp # # This code imports the MEPS data into R as a data frame called 'h205'. # # Note that additional packages are needed to successfully run this code. To # install these packages, run the 'install.packages' function (shown below). # Once installed, the packages can be called using the 'library' function. # Packages only need to be installed once, but they must be called using the # 'library' function every time a new R session is started. # # Two options are available to run this code: # # 1. Copy and paste the code into an interactive R session. # # The user must first download the ASCII (.dat) file from the MEPS website # and save it to a local directory, which must be defined in the # 'meps_path' variable below. In this example, the local directory is # called "C:/MEPS". Note that the path structure will differ on Mac and PC. # # # 2. Call this code directly from an interactive R session. # # (a) If the ASCII (.dat) file has already been downloaded from the MEPS # website and saved to a local directory, the following code can be run # (after re-defining the 'meps_path' variable to point to the location # of the h205.dat file.) # # meps_path <- "C:/MEPS/h205.dat" # source("https://meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h205/h205ru.txt") # head(h205) # view data # # # (b) Alternatively, the ASCII (.dat) file can be downloaded directly from # the MEPS website. The following code can be used to download and # import the h205 data into R without having to manually download, # unzip, and store the file on your local computer. # # url <- "https://meps.ahrq.gov/mepsweb/data_files/pufs/h205dat.zip" # download.file(url, temp <- tempfile()) # # meps_path <- unzip(temp, exdir = tempdir()) # source("https://meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h205/h205ru.txt") # # unlink(temp) # Unlink to delete temporary file # # head(h205) # view data # # ----------------------------------------------------------------------------- # DEFINE 'meps_path' ----------------------------------------------------------- # 'meps_path' should point to the file path of the ASCII file (h205.dat) # Here, the 'exists' function checks whether meps_path is already defined. This # feature is useful if calling this file from an external source. if(!exists("meps_path")) meps_path = "C:/MEPS/h205.dat" # INSTALL PACKAGES ------------------------------------------------------------ # Uncomment and run this portion if packages are not yet installed # # install.packages("readr") # LOAD PACKAGES --------------------------------------------------------------- # Run this for every new R session library(readr) # DATA FILE INFO -------------------------------------------------------------- # Define start and end positions to read fixed-width file pos_start <- c(1, 8, 11, 21, 23, 25, 27, 29, 30, 32, 33, 36, 37, 38, 40, 44, 46, 50, 51, 52, 54, 56, 57, 61, 63, 65, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 81, 84, 87, 90, 93, 95, 97, 99, 102, 104, 106, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 156, 162, 164, 167, 170, 173, 176, 179, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 209, 210, 222, 234, 238) pos_end <- c(7, 10, 20, 22, 24, 26, 28, 29, 31, 32, 35, 36, 37, 39, 43, 45, 49, 50, 51, 53, 55, 56, 60, 62, 64, 68, 69, 70, 72, 73, 74, 75, 76, 77, 78, 80, 83, 86, 89, 92, 94, 96, 98, 101, 103, 105, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 155, 161, 163, 166, 169, 172, 175, 178, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 208, 209, 221, 233, 237, 238) # Define variable names and types ('c' = character, 'n' = 'numeric') var_names <- c("DUID", "PID", "DUPERSID", "PANEL", "FAMID13", "RULETR13", "RUSIZE13", "RUCLAS13", "FAMSIZ13", "REGION13", "RNDREF13", "RDRESP13", "PROXY13", "BEGRFM13", "BEGRFY13", "ENDRFM13", "ENDRFY13", "KEYNESS", "INSCOP13", "PSTAT13", "RURSLT13", "RUENDM13", "RUENDY13", "AGE13X", "DOBMM", "DOBYY", "SEX", "RACEV1X", "RACEV2X", "RACEAX", "RACEBX", "RACEWX", "RACETHX", "HISPANX", "HISPNCAT", "MARRY13X", "SPOUID13", "SPOUIN13", "EDUCYR", "HIDEG", "FTSTD13X", "ACTDTY13", "REFRL13X", "OTHLGSPK", "WHTLGSPK", "HWELLSPK", "BORNUSA", "YRSINUS", "RTHLTH13", "MNHLTH13", "IADLHP13", "ADLHLP13", "AIDHLP13", "WLKLIM13", "LFTDIF13", "STPDIF13", "WLKDIF13", "MILDIF13", "STNDIF13", "BENDIF13", "RCHDIF13", "FNGRDF13", "ACTLIM13", "WRKLIM13", "HSELIM13", "SCHLIM13", "UNABLE13", "SOCLIM13", "COGLIM13", "EMPST13", "HRWAG13X", "HRWGRD13", "HRWAY13", "HOUR13", "HELD13X", "OFFER13X", "NUMEMP13", "SELFCM13", "IHS13", "TRIAT13", "VAPROG13", "MCARE13", "MCARE13X", "GOVTA13", "GOVTB13", "GOVTC13", "MCAID13", "MCAID13X", "PUB13X", "PRIEU13", "PRIEUO13", "PRINEO13", "PRIDK13", "PRING13", "PRIOG13", "PRSTX13", "PRIV13", "HPRIEU13", "HPRIDK13", "HPRING13", "HPRIOG13", "HPRSTX13", "HPRIV13", "VERFLG13", "INSRD13X", "WGTSP13", "WGTRU13", "VARSTR", "VARPSU") var_types <- c("n", "n", "c", "n", "c", "c", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "c", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n") var_types <- setNames(var_types, var_names) # IMPORT ASCII (.dat) file ---------------------------------------------------- h205 <- read_fwf( meps_path, col_positions = fwf_positions( start = pos_start, end = pos_end, col_names = var_names), col_types = var_types) # OPTIONAL: save as .Rdata file for easier loading ---------------------------- # Run this to save a permanent .Rdata file in the local working directory # # save(h205, file = "h205.Rdata") # ----------------------------------------------------------------------------- # NOTES: # # 1. This program has been tested on R version 3.6.0 # # 2. This program will create a temporary data frame in R called 'h205'. # You must run the 'save' command to permanently save the data to a local # folder # -----------------------------------------------------------------------------