Introduction to ipumsr - IPUMS Data in R

Minnesota Population Center

2017-12-15

The ipumsr package allows you to read in data from your extract into R along with the associated metadata like variable labels, value labels and more. IPUMS is a great source of international census and survey data.

IPUMS provides census and survey data from around the world integrated across time and space. IPUMS integration and documentation makes it easy to study change, conduct comparative research, merge information across data types, and analyze individuals within family and community context. Data and services are available free of charge.

Basics

This vignette gives the basic outline of the ipumsr package. There are also vignettes that show how to use the value labels and geographic data provided by IPUMS and others that run through examples using CPS data and NHGIS data.

Getting Data from the IPUMS website

IPUMS data is downloaded from our website at https://www.ipums.org. The website provides an interactive extract system that allows you to select only the sample and variables that are relevant to your research question.

Currently we do not support the TerraPop, but we hope to add support soon!

For microdata projects (all supported projects except NHGIS), once you have created your extract, you should choose to download data as either fixed-width-files or comma separated ones.

Once your extract is complete, download the data file (.dat or .csv) and the DDI, by right-clicking on the file and selecting “Save As…”. Note that there is no R-specific syntax file, as there is for the other statistical packages like SAS, Stata and SPSS.

Annotated screenshot for downloading microdata

For NHGIS, download the table data as a csv, and, if you want the associated mapping data, the GIS data. NHGIS provides the option to download csvs with an extra header row, it does not matter which option you select.

Import Functions

Once your extract is downloaded, the ipumsr package functions read_*() help you load the data into R.

read_ipums_ddi(): Reads DDI files with metadata that are included alongside some extracts (mainly microdata)

read_ipums_codebook(): Reads the text codebook included alongside some extracts (mainly NHGIS and some TerraPop extracts)

Metadata Functions

Once the data is into R, you can learn information about the extract using the metadata function.

ipums_view(): Makes a webpage that displays in the Rstudio Viewer which provides information about the extract as a whole (like your extract notes or the citation information) and the specific variables included (like the variable label, description and value labels).

ipums_file_info(): Returns the file-level metadata contained in ipums_view() but as a R data structure.

ipums_var_info(): Returns the variable-level metadata contained in ipums_view() but as a R data structure.

Grab Bag

Survey Weights

The data from most projects contain some form of weighting variable that should be used to calculate estimates that are representative of the whole population. Many projects also provide specifications to help estimate variance given the complex design of the survey, such as replicate weights or design variables like STRATUM and PSU. The survey package provides functions that allow you to estimate variance taking this into account, and the srvyr package implements dplyr-like syntax for survey analysis, using the survey package’s functions.

For more information about what these variables mean and how to use them, see the website for the project you are interested in.

Non-Extract Data

Some projects have data that is not contained within the extract system and so no DDI is provided for this data. In this case, either use the csv if available, or the haven package to read one of the files intended for another statistical software (like Stata, SAS or SPSS).

Value Labels

The way that IPUMS treats value labels does not align with factors (the main way that R is able to store values associated with labels). R’s factor variables can only store values as an integer sequence (1, 2, 3, …), but IPUMS conventions are to store missing / not in universe codes as large numbers, to distinguish them from the normal values.

Therefore, the ipumsr package uses the labelled class from the haven package to store labelled values. See the “value-labels” vignette for more information (vignette('value-labels')).

In summary, it is generally best if you wish to use the labels, to convert from the labelled class to factor early on in data analysis workflow. This is because many data manipulation functions will lose the associated labels. The function as_factor() is the main function to create factors from labels, but often you will need to do more manipulation before that.

Other IPUMS attributes

Similarly, the other attributes that ipumsr stores about the data are often lost during an analysis. One way to deal with this is to load the DDI or codebook in addition to the actual data using the functions read_ipums_ddi() and read_ipums_codebook(). This way, when you wish to refer to variable labels or other metadata, you can use the DDI object, which does not get modified during your analysis.

“dplyr select-style” Syntax

Several functions within the ipumsr package allow for “dplyr select-style” syntax. This means that they accept either a character vector of values (eg c("YEAR", "AGE")), bare vectors of values (eg c(YEAR, AGE)) and the helper functions allowed in dplyr::select() (eg one_of(c("YEAR", "AGE"))).

Hierarchical data structures

For certain IPUMS projects, the data is hierarchical, multiple people are included in a single household, or multiple activities are performed by a single person. The ipumsr package provides two data structures for storing such data (for users who did not select the “rectangularize” option on the website). The data can be loaded as a "list" or "long".

List data loads each record type into a separate data.frame. The names of the recordtype data.frames are the value of the RECTYPE variable (eg “H” and “P”). Use the function read_ipums_micro_list() to load the data this way.

Long data has one row per unit, regardless of what type of record the unit is. Therefore, datasets loaded this way often contain variables with a large number of missings, for the variables that only apply to certain record types. Use the function read_ipums_micro() to load the data this way.

Geospatial Packages: sf vs sp

The ipumsr package allows for loading geospatial data in two formats (sf for Simple Features and sp for Spatial). The sf package is relatively new, and so does not have as widespread support as the sp package. However, (in my opinion) it does allow for easier analysis, and so may be a better place to start if you have not used GIS data in R before.

For more details about how to load geographic data using ipumsr, see the vignette “ipums-geography” (vignette("ipums-geography", package = "ipumsr"))