# last updated 2022-05-03 # This is example R code to link event and condition records from an IPUMS MEPS # hierarchical extract # Load the ipumsr package. If you do not ipumsr installed already you will need # to use the command #install.packages("ipumsr") library(ipumsr) # Read your IPUMS MEPS extract into R using the ipumsr package - UPDATE HERE setwd("C:\user\directory") # file where your extract is saved ddi <- ipumsr::read_ipums_ddi("meps_00001.xml") # your extract name data <- ipumsr::read_ipums_micro(ddi) # Split the hierarchical extract into different objects for each record type for (i in unique(data$RECTYPE)) { data_rectype <- data[data$RECTYPE == i, ] cols_keep <- vapply(data_rectype, FUN.VALUE = logical(1), FUN = function (x) sum(!is.na(x)) != 0) cols_keep <- names(cols_keep)[cols_keep] data_rectype <- data_rectype[, cols_keep] assign(paste0("data_", i), data_rectype) rm(i, data_rectype, cols_keep) } # Link the condition and event records together using the condition-event link record link_condition_events <- dplyr::inner_join(x = data_X, y = data_J, by = c("MEPSCONDID" = "MEPSCONDIDJ", "YEARX" = "YEARJ")) link_condition_events <- dplyr::inner_join(x = link_condition_events, y = data_E, by = c("MEPSEVNTIDJ" = "MEPSEVNTID", "YEARX" = "YEARE")) # The resulting link_condition_events data frame will have a record for each # event-condition pair and will have filtered out all conditions that don't # link to an event and all events that don't link to a condition