Manage Datasets: Reshaping Data Structure

Learn about how to reshape data structure using R.

We'll cover the following

We often need to reshape how a dataset is structured for analytical purposes. Suppose we’re interested in comparing the values of GDP per capita, measured by rgdpl, between two countries over time. The most intuitive structure is to have their rgdpl values listed side by side for each year. This requires reorganizing the dataset structure. We’ll look at examples by using some functions from the add-on package reshape2.

Create new dataset

To illustrate how reshaping works, we first create a new dataset called pwt7.ip, which contains six years and three variables for two countries: India and Pakistan. It’s organized by country and year, and the data is displayed in the long format below. The R code and output are as follows:

Get hands-on with 1400+ tech skills courses.