{"id":75903,"date":"2020-01-28T14:24:10","date_gmt":"2020-01-28T08:54:10","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75903"},"modified":"2020-01-28T14:24:10","modified_gmt":"2020-01-28T08:54:10","slug":"data-reshaping-in-r","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/","title":{"rendered":"Data Reshaping in R \u2013 Popular Functions to Organise Data"},"content":{"rendered":"<p><strong>In this TechVidvan tutorial, discover why data reshaping is required in R and\u00a0learn how to use different functions in R to do this.<\/strong><\/p>\n<p>For an analysis project, the gathered data is messy and unstructured most of the time. It is collected from different sources, has different variables, and has irregular formats.<\/p>\n<p>With this tutorial, learn how to change the gathered data and conform it to our desired format in R. We will take a look at the functions in R that allow us to do this and much more. So, get ready for the ride!<\/p>\n<h2>What is Data Reshaping in R?<\/h2>\n<p>Before we can perform any kind of analysis, we first need to shape the gathered data into a regular and processable format. We need to ensure that all of the data fits into proper variables.<\/p>\n<p>We also need to take care of missing values and put placeholders in their place that our analysis tools can understand.<\/p>\n<p>This is the first step in any analysis project. We call this process as <strong>data reshaping.<\/strong><\/p>\n<p>Now let&#8217;s learn popular functions used for data reshaping in R.<\/p>\n<h3>The cbind(), rbind(), and t() Functions<\/h3>\n<p>There are many functions in R that allow us to manipulate data objects in many ways.<\/p>\n<p><strong>cbind()<\/strong>, <strong>rbind()<\/strong>, and <strong>t()<\/strong> are the most commonly used functions for data reshaping. We will be going through them one-by-one:<\/p>\n<p><strong>1. cbind():<\/strong> The cbind() function allows us to <strong>join objects as column<\/strong>. We can combine matrices, data-frames,<a href=\"https:\/\/techvidvan.com\/tutorials\/r-vector\/\"><strong> vectors<\/strong><\/a> or any combination of these.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">vec1 &lt;- c(1,2,3,4,5)\nvec2 &lt;- c(6,7,8,9,10)\nmat1 &lt;- matrix(c(1:15),c(5,3))\nmat2 &lt;- matrix(c(16:30),c(5,3))\ndf1 &lt;- data.frame(matrix(c(1:30),nrow=5))\ndf2 &lt;- data.frame(matrix(c(31:60),nrow=5))\ncbind(vec1,vec2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-cbind-vector-n-vector.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75926 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-cbind-vector-n-vector.png\" alt=\"cbind() vector n vector - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cbind(mat1,mat2)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cbind(df1,df2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-cbind-mat-mat-and-df-df.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75927 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-cbind-mat-mat-and-df-df.png\" alt=\"cbind() mat mat and df df - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cbind(vec1,mat2)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cbind(vec2,df1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-cbind-vec-mat-and-vec-df3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75943 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-cbind-vec-mat-and-vec-df3.png\" alt=\"data reshaping in r - cbind() vec mat and vec df3\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cbind(mat1,df2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-cbind-mat-df4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75939 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-cbind-mat-df4.png\" alt=\"cbind() mat df4 - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Note:<\/strong> The number of rows should be the same for the <strong><code>cbind()<\/code><\/strong> function to work.<\/p>\n<p>Wait! before proceeding ahead get a clear understanding of <a href=\"https:\/\/techvidvan.com\/tutorials\/r-functions\/\"><strong>user-defined functions in R<\/strong><\/a>.<\/p>\n<p><strong>2. rbind():<\/strong> The <strong><code>rbind()<\/code><\/strong> function <strong>allows us to join objects as rows<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">rbind(vec1,vec2)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">rbind(vec1,df2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-rbind-vec-vec-and-vec-df5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75940 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-rbind-vec-vec-and-vec-df5.png\" alt=\"data reshaping in r - rbind() vec vec and vec df5\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p><strong>Note:<\/strong> The number of columns should be the same for the <strong><code>rbind()<\/code><\/strong> function to work.<\/p>\n<p><strong>3. t()<\/strong>: The<strong><code>t()<\/code><\/strong> function transposes a <a href=\"https:\/\/techvidvan.com\/tutorials\/r-matrix\/\"><strong>matrix<\/strong><\/a> that is it <strong>turns the rows into columns and columns into rows.<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">t(mat1)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">t(mat2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r6-t-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75944 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r6-t-1.png\" alt=\"t() - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>The Tidyr package<\/h3>\n<p>The <strong><code>tidyr<\/code><\/strong> package is the most <strong>commonly used<\/strong> R package for data reshaping in R. <strong><code>tidyr<\/code><\/strong> helps you tidy your data.<\/p>\n<p>It allows you to convert it into the desired format and make it easier to process and analyze. <a href=\"https:\/\/blog.rstudio.com\/2014\/07\/22\/introducing-tidyr\/\">Tidyr<\/a> simplifies the process of data reshaping.<\/p>\n<p>To install <strong><code>tidyr<\/code><\/strong>, use the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">install.packages(\"tidyr\")<\/pre>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-installing-tidyr7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75942 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-installing-tidyr7.png\" alt=\"installing tidyr7 - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p>Once installed, include it into your current R session by using the <strong><code>library()<\/code><\/strong> command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">library(tidyr)<\/pre>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-librarytidyr8-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75945 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-librarytidyr8-1.png\" alt=\"library(tidyr)8 - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>These functions of <strong><code>tidyr<\/code><\/strong> are very useful in data reshaping and keeping your data tidy.<\/p>\n<ol>\n<li>gather()<\/li>\n<li>spread()<\/li>\n<li>unite()<\/li>\n<li>separate()<\/li>\n<\/ol>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-R.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75954 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-R.jpg\" alt=\"data reshaping in R\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<p>Let\u2019s look at these <strong>functions<\/strong> and their usage:<\/p>\n<h4>1. gather() Function<\/h4>\n<p>The <strong><code>gather()<\/code><\/strong> function helps us in reshaping wide-format data-frames to long-format.<\/p>\n<p>Sometimes, datasets have attributes of common concern spread across different columns. This creates unnecessary variables. Such a dataset is said to be in the wide-format.<\/p>\n<p>It would be more efficient to stack similar attributes together and turning the dataset into long-format. The <strong><code>gather()<\/code><\/strong> function allows us to do that.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">month &lt;- month.abb[1:3]\ndelhi &lt;- sample(seq(-5,47,by=0.01),3,rep=TRUE)\nmumbai &lt;-sample(seq(-5,47,by=0.01),3,rep=TRUE)\nchennai &lt;-sample(seq(-5,47,by=0.01),3,rep=TRUE)\nbangalore &lt;- sample(seq(-5,47,by=0.01),3,rep=TRUE)\nkolkata &lt;- sample(seq(-5,47,by=0.01),3,rep=TRUE)\ndata &lt;- data.frame(month,delhi,mumbai,bangalore,chennai,kolkata)\ndata<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-gather1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75936 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-gather1.png\" alt=\"tidyr gather()1 - data reshaping in r\" width=\"1299\" height=\"740\" \/><\/a><\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">gathered_data &lt;- gather(data,key=\"city\",value=\"avg.temp\",-month)\ngathered_data<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><span style=\"color: #ff0000\"><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-gather210.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75946 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-gather210.png\" alt=\"tidyr gather() - data reshaping in r\" width=\"1299\" height=\"742\" \/><\/a><\/strong><\/span><\/p>\n<h4>2. spread() Function<\/h4>\n<p>The <strong><code>spread()<\/code><\/strong> function is the complement to the <strong><code>gather()<\/code><\/strong> function. It spreads long-format data-frames to wide-format.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">spread_data &lt;- spread(gathered_data,key=\"city\",value=\"avg.temp\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-spread.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75935 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-spread.png\" alt=\"tidyr spread() - data reshaping in R\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h4>3. unite() Function<\/h4>\n<p>Take a look at the following dataset:<\/p>\n<table class=\"tv-table-center\">\n<tbody>\n<tr>\n<td><b>S.no<\/b><\/td>\n<td><b>Month<\/b><\/td>\n<td><b>Year<\/b><\/td>\n<td><b>Temp.<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">jan<\/span><\/td>\n<td><span style=\"font-weight: 400\">2018<\/span><\/td>\n<td><span style=\"font-weight: 400\">4.64<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><span style=\"font-weight: 400\">feb<\/span><\/td>\n<td><span style=\"font-weight: 400\">2018<\/span><\/td>\n<td><span style=\"font-weight: 400\">19.68<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><span style=\"font-weight: 400\">jan<\/span><\/td>\n<td><span style=\"font-weight: 400\">2019<\/span><\/td>\n<td><span style=\"font-weight: 400\">2.56<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">4<\/span><\/td>\n<td><span style=\"font-weight: 400\">mar<\/span><\/td>\n<td><span style=\"font-weight: 400\">2019<\/span><\/td>\n<td><span style=\"font-weight: 400\">36.74<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the dataset, the month and year have separate columns. It looks inefficient, doesn\u2019t it?<\/p>\n<p>The two variables month and year can be in the same column without affecting the information conveyed by the data. This is exactly what the <strong><code>unite()<\/code><\/strong> function does.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">months &lt;- c(\"jan\",\"feb\",\"jan\",\"mar\")\nyear &lt;- c(\"2018\",\"2018\",\"2019\",\"2019\")\ntemp &lt;- c(4.64,19.68,2.56,36.74)\ndelhi_temp &lt;- data.frame(months,year,temp)\ndelhi_temp<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-unite1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75933 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-unite1.png\" alt=\"tidyr unite()1 - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">united_delhi_temp &lt;- unite(delhi_temp,\"interval\",months,year)\nunited_delhi_temp<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-unite211.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75947 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-unite211.png\" alt=\"tidyr unite() - data reshaping in r \" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>4. separate() Function<\/h4>\n<p>The <strong><code>separate()<\/code><\/strong> function is the complement to the <strong><code>unite()<\/code><\/strong> function. It separates values into separate columns.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sep_delhi_temp &lt;- separate(united_delhi_temp,\ninterval,c(\"month\",\"year\"))\nsep_delhi_temp<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-separate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75932 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-tidyr-separate.png\" alt=\"tidyr separate() - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>The Reshape2 Package<\/h3>\n<p>Reshape2 is another R package that is used for data reshaping. Reshape2 can be considered as an older version of the <strong><code>tidyr<\/code><\/strong> package.<\/p>\n<p>The development of the reshape2 package has stopped. The most commonly used reshape2 functions are the <strong><code>melt()<\/code><\/strong> and <strong><code>merge()<\/code><\/strong> functions.<\/p>\n<h4>1. melt() Function<\/h4>\n<p>The <strong><code>melt()<\/code><\/strong> function is very similar to the <strong><code>gather()<\/code><\/strong> function from the <strong><code>tidyr<\/code><\/strong> package. It melts the input <a href=\"https:\/\/techvidvan.com\/tutorials\/r-data-frames\/\"><strong>data frame<\/strong><\/a> and converts wide-format data into long-format. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">mdata &lt;- melt(data,id=c(\"month\"),variable.name=\"city\", value.name=\"avg.temp\")\nmdata<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-reshape2-melt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75931 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-reshape2-melt.png\" alt=\"data reshaping in r - reshape2 melt()\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h4>2. merge() Function<\/h4>\n<p>The <strong><code>merge()<\/code><\/strong> function can merge data frames. The function merges the input data frames horizontally, therefore, the data frames must have the same variable that is column names. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">months2 &lt;- c(\"apr\", \"mar\", \"feb\", \"jun\")\nyear2 &lt;- c(\"2018\",\"2018\",\"2019\",\"2019\")\ntemp2 &lt;- c(38.75,37.68,28.56,41.74)\ndelhi_temp2 &lt;- data.frame(months2,year2,temp2)\ncolnames(delhi_temp2) &lt;- c(\"months\",\"year\",\"temp\")\ndelhi_temp2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-reshape2-merge.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75930 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/data-reshaping-in-r-reshape2-merge.png\" alt=\"reshape2 merge() - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">merge_delhi_temp &lt;- merge(delhi_temp,delhi_temp2,by=\"year\")\nmerge_delhi_temp<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/17.data-reshaping-in-r-reshape2-merge2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75929 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/17.data-reshaping-in-r-reshape2-merge2.png\" alt=\"eshape2 merge()2 - data reshaping in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>Data reshaping is the first step of any data analysis project. It is also called <strong>data formatting<\/strong> and <strong>data cleaning<\/strong>.<\/p>\n<p>In this article, we looked at the functions in base R that allow us to reshape and transform our data.<\/p>\n<p>We also looked at the most popular data processing and transformation package, the <strong><code>tidyr<\/code><\/strong> package. The <strong><code>tidyr<\/code><\/strong> package is a part of the tidyverse collection of R packages. It is a must-have for beginner, intermediate and advanced R programmers.<\/p>\n<p><strong>Still, have some doubts about the data reshaping in R?<\/strong> Ask us and our <strong>TechVidvan<\/strong> experts will be happy to help you.<\/p>\n<p>Keep Visiting\u00a0?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this TechVidvan tutorial, discover why data reshaping is required in R and\u00a0learn how to use different functions in R to do this. For an analysis project, the gathered data is messy and unstructured&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75954,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[1522,1523,1524,1525,1526,1527,1528,1529,1530,1531],"class_list":["post-75903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-data-reshaping-in-r","tag-gather-r","tag-r-melt","tag-r-merge","tag-r-reshape2","tag-r-unite","tag-reshape-r-package","tag-spread-r","tag-tidyr-package","tag-tidyr-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Data Reshaping in R \u2013 Popular Functions to Organise Data - TechVidvan<\/title>\n<meta name=\"description\" content=\"Data Reshaping in R - In this tutorial, learn to change the gathered data and conform it to our desired format with the help of functions in R programming.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Data Reshaping in R \u2013 Popular Functions to Organise Data - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Data Reshaping in R - In this tutorial, learn to change the gathered data and conform it to our desired format with the help of functions in R programming.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-28T08:54:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/data-reshaping-in-R.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Data Reshaping in R \u2013 Popular Functions to Organise Data - TechVidvan","description":"Data Reshaping in R - In this tutorial, learn to change the gathered data and conform it to our desired format with the help of functions in R programming.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Data Reshaping in R \u2013 Popular Functions to Organise Data - TechVidvan","og_description":"Data Reshaping in R - In this tutorial, learn to change the gathered data and conform it to our desired format with the help of functions in R programming.","og_url":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-28T08:54:10+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/data-reshaping-in-R.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Data Reshaping in R \u2013 Popular Functions to Organise Data","datePublished":"2020-01-28T08:54:10+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/"},"wordCount":811,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/data-reshaping-in-R.jpg","keywords":["data reshaping in R","gather() R","R melt","R merge()","R reshape2","R unite()","Reshape R Package","spread() R","tidyr package","tidyr() R"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/","url":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/","name":"Data Reshaping in R \u2013 Popular Functions to Organise Data - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/data-reshaping-in-R.jpg","datePublished":"2020-01-28T08:54:10+00:00","description":"Data Reshaping in R - In this tutorial, learn to change the gathered data and conform it to our desired format with the help of functions in R programming.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/data-reshaping-in-R.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/data-reshaping-in-R.jpg","width":802,"height":420,"caption":"data reshaping in R"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/data-reshaping-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Data Reshaping in R \u2013 Popular Functions to Organise Data"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75903"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75903\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75954"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}