# ----------------------------------------------------------------------------- # R programming statements for h248D 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 (h248D.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 'h248D'. # # 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 h248D.dat file.) # # meps_path <- "C:/MEPS/h248D.dat" # source("https://meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h248D/h248Dru.txt") # head(h248D) # 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 h248D 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/h248Ddat.zip" # download.file(url, temp <- tempfile()) # # meps_path <- unzip(temp, exdir = tempdir()) # source("https://meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h248D/h248Dru.txt") # # unlink(temp) # Unlink to delete temporary file # # head(h248D) # view data # # ----------------------------------------------------------------------------- # DEFINE 'meps_path' ----------------------------------------------------------- # 'meps_path' should point to the file path of the ASCII file (h248D.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/h248D.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, 37, 38, 54, 66, 68, 69, 73, 75, 79, 81, 84, 85, 87, 90, 92, 94, 96, 105, 115, 124, 133, 142, 151, 160, 169, 177, 185, 193, 201, 210, 220, 227, 235, 243, 252, 259, 266, 270, 277, 284, 291, 300, 309, 310, 322, 326) pos_end <- c( 7, 10, 20, 36, 37, 53, 65, 67, 68, 72, 74, 78, 80, 83, 84, 86, 89, 91, 93, 95, 104, 114, 123, 132, 141, 150, 159, 168, 176, 184, 192, 200, 209, 219, 226, 234, 242, 251, 258, 265, 269, 276, 283, 290, 299, 308, 309, 321, 325, 326) var_names <- c( "DUID", "PID", "DUPERSID", "EVNTIDX", "EVENTRN", "ERHEVIDX", "FFEEIDX", "PANEL", "MPCDATA", "IPBEGYR", "IPBEGMM", "IPENDYR", "IPENDMM", "NUMNIGHX", "EMERROOM", "SPECCOND", "RSNINHOS", "ANYOPER", "DSCHPMED", "FFIPTYPE", "IPXP23X", "IPTC23X", "IPFSF23X", "IPFMR23X", "IPFMD23X", "IPFPV23X", "IPFVA23X", "IPFTR23X", "IPFOF23X", "IPFSL23X", "IPFWC23X", "IPFOT23X", "IPFXP23X", "IPFTC23X", "IPDSF23X", "IPDMR23X", "IPDMD23X", "IPDPV23X", "IPDVA23X", "IPDTR23X", "IPDOF23X", "IPDSL23X", "IPDWC23X", "IPDOT23X", "IPDXP23X", "IPDTC23X", "IMPFLAG", "PERWT23F", "VARSTR", "VARPSU") var_types <- c( "n", "n", "c", "c", "n", "c", "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") var_types <- setNames(var_types, var_names) # IMPORT ASCII file ----------------------- h248D <- 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(h248D, file ="h248D.Rdata") # ----------------------------------------------------------------------------- # NOTES: # # 1. This program has been tested on R version 4.4.0 # # 2. This program will create a temporary data frame in R called 'h248D'. # You must run the 'save' command to permanently save the data to a local # folder # -----------------------------------------------------------------------------