{"id":35,"date":"2026-03-03T06:52:52","date_gmt":"2026-03-03T06:52:52","guid":{"rendered":"https:\/\/gigz.pk\/r\/?post_type=lesson&#038;p=35"},"modified":"2026-04-01T11:00:02","modified_gmt":"2026-04-01T11:00:02","slug":"working-with-tidyr-for-data-tidying","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/r\/lesson\/working-with-tidyr-for-data-tidying\/","title":{"rendered":"Working with tidyr for Data Tidying"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>tidyr<\/code> is a powerful R package designed to help you clean, reshape, and organize messy data into a tidy format. Tidy data makes analysis, visualization, and modeling easier and more efficient. Each variable should have its own column, each observation should have its own row, and each value should occupy its own cell.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>1. Installing and Loading tidyr<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Before using <code>tidyr<\/code>, install and load the package:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">install.packages(\"tidyr\")  # Install tidyr<br>library(tidyr)             # Load tidyr<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>2. Key tidyr Functions<\/strong><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>a) gather() \/ pivot_longer()<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pivot_longer()<\/code> (modern replacement for <code>gather()<\/code>) converts wide-format data into long-format data. This is useful when columns represent values instead of variables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data &lt;- data.frame(<br>  Name = c(\"Alice\", \"Bob\"),<br>  Math = c(90, 85),<br>  Science = c(88, 92)<br>)# Convert wide data to long format<br>long_data &lt;- pivot_longer(data, cols = c(Math, Science), <br>                          names_to = \"Subject\", <br>                          values_to = \"Score\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong> Each row represents one subject score per student.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>b) spread() \/ pivot_wider()<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pivot_wider()<\/code> (replacement for <code>spread()<\/code>) converts long-format data into wide-format data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wide_data &lt;- pivot_wider(long_data, names_from = Subject, values_from = Score)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong> Each subject becomes a separate column again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>c) separate()<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>separate()<\/code> splits a single column into multiple columns based on a separator.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data &lt;- data.frame(ID = c(\"A-01\", \"B-02\"))<br>separate(data, col = ID, into = c(\"Letter\", \"Number\"), sep = \"-\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>d) unite()<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>unite()<\/code> combines multiple columns into one.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data &lt;- data.frame(Letter = c(\"A\",\"B\"), Number = c(\"01\",\"02\"))<br>unite(data, col = \"ID\", Letter, Number, sep = \"-\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>e) drop_na() and fill()<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>drop_na()<\/code> removes rows with missing values<\/li>\n\n\n\n<li><code>fill()<\/code> fills missing values with the previous or next non-missing value<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">data &lt;- data.frame(Name = c(\"Alice\",\"Bob\",\"Charlie\"), Score = c(90, NA, 88))<br>drop_na(data)           # Removes row with NA<br>fill(data, Score)       # Fills NA with previous value<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>3. Advantages of Using tidyr<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Converts messy datasets into tidy format for easier analysis<\/li>\n\n\n\n<li>Works seamlessly with <code>dplyr<\/code> and the tidyverse<\/li>\n\n\n\n<li>Handles missing data and reshaping tasks efficiently<\/li>\n\n\n\n<li>Simplifies data preparation for visualization and modeling<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><code>tidyr<\/code> is essential for cleaning and reshaping data in R. By mastering functions like <code>pivot_longer()<\/code>, <code>pivot_wider()<\/code>, <code>separate()<\/code>, <code>unite()<\/code>, and handling missing values, you can prepare datasets in a tidy, consistent format. This allows for faster, more accurate, and more effective data analysis.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1775041175395\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/r\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">R Programming (R Lang) > Data Manipulation in R > Working with tidyr for Data Tidying<\/span><\/span><\/div>","protected":false},"menu_order":12,"template":"","class_list":["post-35","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Working with tidyr for Data Tidying - Analyze Deep. Visualize Better. Build with R.<\/title>\n<meta name=\"description\" content=\"Learn how to reshape and clean data in R with tidyr. Master pivot_longer, pivot_wider, separate, unite, and handling missing values.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Working with tidyr for Data Tidying - Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"og:description\" content=\"Learn how to reshape and clean data in R with tidyr. Master pivot_longer, pivot_wider, separate, unite, and handling missing values.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-01T11:00:02+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/lesson\\\/working-with-tidyr-for-data-tidying\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Working with tidyr for Data Tidying - Analyze Deep. Visualize Better. Build with R.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\"},\"datePublished\":\"2026-03-03T06:52:52+00:00\",\"dateModified\":\"2026-04-01T11:00:02+00:00\",\"description\":\"Learn how to reshape and clean data in R with tidyr. Master pivot_longer, pivot_wider, separate, unite, and handling missing values.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R Programming (R Lang) > Data Manipulation in R > Working with tidyr for Data Tidying\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\",\"name\":\"Analyze Deep. Visualize Better. Build with R.\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/r\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Working with tidyr for Data Tidying - Analyze Deep. Visualize Better. Build with R.","description":"Learn how to reshape and clean data in R with tidyr. Master pivot_longer, pivot_wider, separate, unite, and handling missing values.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Working with tidyr for Data Tidying - Analyze Deep. Visualize Better. Build with R.","og_description":"Learn how to reshape and clean data in R with tidyr. Master pivot_longer, pivot_wider, separate, unite, and handling missing values.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Analyze Deep. Visualize Better. Build with R.","article_modified_time":"2026-04-01T11:00:02+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/r\/lesson\/working-with-tidyr-for-data-tidying\/","url":"https:\/\/gigz.pk\/","name":"Working with tidyr for Data Tidying - Analyze Deep. Visualize Better. Build with R.","isPartOf":{"@id":"https:\/\/gigz.pk\/r\/#website"},"datePublished":"2026-03-03T06:52:52+00:00","dateModified":"2026-04-01T11:00:02+00:00","description":"Learn how to reshape and clean data in R with tidyr. Master pivot_longer, pivot_wider, separate, unite, and handling missing values.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/r\/"},{"@type":"ListItem","position":2,"name":"R Programming (R Lang) > Data Manipulation in R > Working with tidyr for Data Tidying"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/r\/#website","url":"https:\/\/gigz.pk\/r\/","name":"Analyze Deep. Visualize Better. Build with R.","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/r\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/media?parent=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}