Difference between revisions of "Tidying Data"
Line 11: | Line 11: | ||
==The Simple Case: Reshaping== | ==The Simple Case: Reshaping== | ||
When all '''observations''' in a '''data table''' have the same unit of observation, reshaping is enough to make untidy data '''tidy'''. Reshaping is the transformation of the organization of rows and columns in a '''data table''' in such a way that the unit of observation represented by a row changes. This can be done in Stata using the <code>reshape</code> command. It is important to note that reshaping does not change the content of the data points in the table. | When all '''observations''' in a '''data table''' have the same unit of observation, reshaping is enough to make untidy data '''tidy'''. Reshaping is the transformation of the organization of rows and columns in a '''data table''' in such a way that the unit of observation represented by a row changes. This can be done in Stata using the <code>reshape</code> command. It is important to note that reshaping does not change the content of the data points in the table. | ||
Let's take a look at the following example, drawn from the e-book [https://github.com/moderndive/ModernDive_book A Modern Dive into R and the Tidyverse]. Look at the table below. | |||
{| class="wikitable" style="border-style: solid; border-width: 10px" | |||
|+ Stock Prices: Non-Tidy Format | |||
|- | |||
! Date !! Boeing Stock Price !! Amazon Stock Price !! Google Stock Price | |||
|- | |||
| 2009-01-01 || $173.55 || $174.90 || $174.34 | |||
|- | |||
| 2009-01-02 || $172.61 || $171.42 || $170.04 | |||
|} | |||
Note that there are three '''variables''' here: | |||
#Date | |||
#Stock Name | |||
#Stock Price | |||
While the data is neatly organized in a spreadsheet-type format, it is not in a '''tidy''' format since there are not three columns corresponding to the three '''variables'''. Remember, in a '''tidy''' format, each '''variable''' should be its own column. The table below shows the data reorganized into this '''tidy''' format. | |||
{| class="wikitable" style="border-style: solid; border-width: 10px" | |||
|+ Stock Prices: Tidy Format | |||
|- | |||
! Date !! Stock Name !! Stock Price | |||
|- | |||
| 2009-01-01 || Boeing || $173.55 | |||
|- | |||
| 2009-01-02 || Boeing || $172.61 | |||
|- | |||
|2009-01-01 || Amazon || $174.90 | |||
|- | |||
|2009-01-02 || Amazon || $171.42 | |||
|- | |||
|2009-01-01 || Google || $174.34 | |||
|- | |||
|2009-01-02 || Google || $170.04 | |||
|} | |||
Note that each '''variable''' (Date, Stock Price, and Stock Price) each have their own column. | |||
==More Complex Cases== | ==More Complex Cases== |
Revision as of 19:04, 27 July 2023
Data is often acquired in various shapes and sizes, but it is most commonly received in the form of data tables. In the context of statistics, the ideal format in which to analyze data tables is called tidy data. A tabular dataset is tidy when each column corresponds to one variable in the dataset, each row corresponds to one observation, and all variables in the dataset have the same unit of observation.
Read First
- In the context of development research, survey data is rarely acquired in a tidy format.
- A variable is a collection of data points that measure the same attribute across units, for example, name, age, income, etc. There are multiple variables represented in a single column.
- An observation is a collection of all values measured on the same unit across attributes, for example, in a survey of seasonal crop patterns for 1000 households in a district, each household is an observation. Each observation is separated into multiple rows.
- Each data point represents one variable and one observation.
- A dataset is a collection of data points and the most common form in which it is organized is a data table.
- Not all data tables are easy to work with, but work in database management has shown that the easiest format to work with these tables in is called tidied data.
The Simple Case: Reshaping
When all observations in a data table have the same unit of observation, reshaping is enough to make untidy data tidy. Reshaping is the transformation of the organization of rows and columns in a data table in such a way that the unit of observation represented by a row changes. This can be done in Stata using the reshape
command. It is important to note that reshaping does not change the content of the data points in the table.
Let's take a look at the following example, drawn from the e-book A Modern Dive into R and the Tidyverse. Look at the table below.
Date | Boeing Stock Price | Amazon Stock Price | Google Stock Price |
---|---|---|---|
2009-01-01 | $173.55 | $174.90 | $174.34 |
2009-01-02 | $172.61 | $171.42 | $170.04 |
Note that there are three variables here:
- Date
- Stock Name
- Stock Price
While the data is neatly organized in a spreadsheet-type format, it is not in a tidy format since there are not three columns corresponding to the three variables. Remember, in a tidy format, each variable should be its own column. The table below shows the data reorganized into this tidy format.
Date | Stock Name | Stock Price |
---|---|---|
2009-01-01 | Boeing | $173.55 |
2009-01-02 | Boeing | $172.61 |
2009-01-01 | Amazon | $174.90 |
2009-01-02 | Amazon | $171.42 |
2009-01-01 | $174.34 | |
2009-01-02 | $170.04 |
Note that each variable (Date, Stock Price, and Stock Price) each have their own column.
More Complex Cases
A more complex, often encountered problem is that a dataset consists of multiple units of observation stored in the same table. This is resolved in three steps:
- Identify all the variables that were measured at the same level of observation
- Create separate tables for each level of observation
- Reshape the data
At the end of this process, your dataset will consist of multiple data tables, one for each unit of observation. To give a concrete example, say we have a data table consisting of two variables, age and gender, where the variables in the table have two different units of observation: household and household member. Because age and gender are measured at two different levels, they are split into two columns and each row contains multiple observations.
There are two additional components that are crucial to successfully managing a set of tidy tables:
- Each table must be uniquely and fully identified by one or a set of ID variables
- You must be able to use these ID variables to link all data tables to each other
The tidying process gets more complex as the number of nested groups increases: the steps of identifying the unit of observation of each variable and reshaping the separated tables need to be repeated. However, the more nested groups a dataset includes, the more efficient it is to deal with tidy data as compared to untidy.
Relevance for Data Cleaning
When cleaning data, and survey data in particular, it is usually necessary to recode values, label variables and values, and explore variables. Cleaning tidy data is easier because in the tidy dataset, each variable corresponds to one survey question:
- Each question has the same set of options, you can apply them to a single column in a tidy dataset, instead of looping over multiple columns.
- Using a tidy dataset, you can summarize all answers to a question easily
Additional Resources
- DIME Analytics (World Bank) Motivations and Definitions for Tidy Data
- DIME Analytics (World Bank) Tidying Data