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

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

Recipe Objective

What is the use of unite function in tidyr package.

An unite () function in the tidyr package, unites / combines multiple columns and returns a single column in the dataframe. The following example consists of two columns, one with the birth date and month and the second column containing the year of birth. These two columns are united in order to get the full-birthday date of students in the defined dataframe. This recipe demonstrates example of unite() in R.

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"), birth_date = c('15/10','26/02','24/09','06/04','14/12'), birth_year = c(1998,1997,1995,1994,1998)) print(df)

"Dataframe is :"
  student_name birth_date birth_year
1            A      15/10       1998
2            B      26/02       1997
3            C      24/09       1995
4            D      06/04       1994
5            E      14/12       1998

Step 3 - Use unite()

Syntax - unite(data,"new_col",columns,sep="") where, data - input dataframe new_col - column name for the new united columns columns - columns to be united sep - a seperator used between the united columns

x <- unite(df,"student_birthday",birth_date,birth_year,sep="/") print(x)

"Output of the code is :"
  student_name student_birthday
1            A       15/10/1998
2            B       26/02/1997
3            C       24/09/1995
4            D       06/04/1994
5            E       14/12/1998

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

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.