What is the use of gather function in tidyr package in R

This recipe explains what is the use of gather function in tidyr package in R

Recipe Objective

What is the use of gather function in tidyr package?

A gather () function is used for collecting (gather) multiple columns and converting them into a key-value pair. The column names get duplicated while using the gather (), i.e., the data gets repeated and forms the key-value pairs. The basic logic behind the gather () is that it reduces the number of columns in the dataset and converts them into rows, leading to an increase in the number of rows in the dataset. This recipe demonstrates an example of gather () in R.

Master the Art of Data Cleaning in Machine Learning

Step 1 - Install the required library

install.packages("tidyr") library(tidyr)

Step 2 - Define a dataframe

df <- data.frame(student_name = c("A","B","C","D","E"), Math_marks = c(70,56,34,89,54), Sci_marks = c(35,55,87,12,63), Eng_marks = c(59,89,55,63,55)) print(df)

"Dataframe is:"
  student_name Math_marks Sci_marks Eng_marks
1            A         70        35        59
2            B         56        55        89
3            C         34        87        55
4            D         89        12        63
5            E         54        63        55

Step 3 - Use gather()

Syntax : gather(data, key, value, x:y) data - input dataframe key - name of the key column that is to be created value - name of the value column that is to be created x:y - Which columns to be gathered.

x <- gather(df,"subject","marks",2:4) print(x) # here, df-dataframe, key:subjects, values-marks, and 2:4 i.e column 2,3,4 must be gathered

"Output of the code:"
   student_name    subject marks
1             A Math_marks    70
2             B Math_marks    56
3             C Math_marks    34
4             D Math_marks    89
5             E Math_marks    54
6             A  Sci_marks    35
7             B  Sci_marks    55
8             C  Sci_marks    87
9             D  Sci_marks    12
10            E  Sci_marks    63
11            A  Eng_marks    59
12            B  Eng_marks    89
13            C  Eng_marks    55
14            D  Eng_marks    63
15            E  Eng_marks    55

{"mode":"full","isActive":false}

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in 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 Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.