Skip to main content Accessibility help
×
Hostname: page-component-5b777bbd6c-gtgcz Total loading time: 0 Render date: 2025-06-21T10:56:24.763Z Has data issue: false hasContentIssue false

5 - Cleaning Data

from PART TWO - COLLECTING AND CLEANING DATA

Published online by Cambridge University Press:  15 September 2017

Jose Manuel Magallanes Reyes
Affiliation:
University of Washington and Pontificia Universidad Católica del Perú
Get access

Summary

Data cleaning is an iterative process. However, this chapter is more concerned with explaining the cleaning steps than the order in which you should do them. It reviews each data frame created in Chapter 4, performs a basic data exploration, and then cleans the “dirty” values, – those that have some anomalies caused during the process of importing the data into R or Python or because the original values were not ready to be imported into a different software. I focus on missing values, encoding, and, especially, symbols that were not imported correctly.

Dealing with Missing Values

Missing values are likely to be present in every data set. Data related to engineering activities can get lost, but, in general, a missing value indicates that a sensor did not record a particular data value. In social data, however, you will not only find an “NA” representing missing values but they may be coded with other values representing varieties of “missingness.” The coding of missing values is well covered by licensed software such as STATA or SPSS, but not explicitly covered in standard R or Python. As we learned in the last chapter, the memisc and foreign packages can import SPSS data very well into R (although there is less support from Python) and that Python and R can import without problems STATA files and the other formats we explored. Let's see how Python and R deal with missing data.

Identifying and Setting Missing Values: Codebooks

Finding a data set without missing values is rare. The problem is knowing whether the missing values are clearly identifiable or not. In general, if your data set comes with a codebook, then that means that you will know all the metadata needed. If you scrape the data, you may not know if you are missing values or not.

The SPSS data from ANES is a good example of the varieties of missing data in social science research. Table 5.1 from ANES time series for 2012 shows clearly which values should not enter into the calculations, but are nevertheless informative. That table was created using the codebook provided by ANES:

Table 5.1 has many values that should be considered missing values, and we have the codebook to identify them.

Type
Chapter
Information
Introduction to Data Science for Social and Policy Research
Collecting and Organizing Data with R and Python
, pp. 126 - 160
Publisher: Cambridge University Press
Print publication year: 2017

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Book purchase

Temporarily unavailable

Save book to Kindle

To save this book to your Kindle, first ensure no-reply@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×