How to pivot a dataframe in R?

This recipe helps you pivot a dataframe in R

Recipe Objective

One of the major data manipulation operation is to compute cross tabulations of measuremens by categories. Excel has a new feature of pivot tables.

In this recipe, we will learn how to pivot a dataframe. There are two ways to pivot a table/dataframe using tidyverse package: ​

  1. pivot_wider() function (i.e to expand the columns to summarise the data well where each row corresponds to the unique factor levels from a column)
  2. pivot_longer() function (i.e to shrink the columns such that rows are increased to summarise the data well)

We will use the pivot_wider() function to explain this task ​

STEP 1: Loading the packages and dataset

# for data manipulation library(tidyverse) long_data = read.csv("R_329_wide_data.csv") attach(long_data) glimpse(long_data)

Rows: 16
Columns: 3
$ ï..Area  Mumbai, Mumbai, Mumbai, Mumbai, Kolkata, Kolkata, Kolkata, ...
$ Quarter  Q1_2012, Q2_2012, Q3_2012, Q4_2012, Q1_2012, Q2_2012, Q3_20...
$ Sales    150, 116, 150, 125, 116, 125, 116, 150, 125, 116, 125, 116,...

STEP 2: Pivoting the table

We use pivot_wider() function to carry out the task.

Syntax: pivot_wider(df, id_cols = , names_from = , values_from = )

where:

  1. df = dataframe
  2. id_cols = identifier columns not to pivot
  3. names_from = column to pivot into new column
  4. values_from = column with measurement values

pivot_wide_data = pivot_wider(long_data, id_cols = ï..Area, names_from = Quarter, values_from = Sales) pivot_wide_data

Area	Q1_2012	Q2_2012	Q3_2012	Q4_2012
Mumbai	150	116	150	125
Kolkata	116	125	116	150
Delhi	125	116	125	116
Chennai	116	150	116	116

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Build a Refund Request AI Agent using LangGraph and FAISS
In this AI Project, you will learn to build an intelligent, multi-agent refund management system that automates validation of customer requests, enforces business policies through semantic search, and provides consistent customer communication.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.