<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/data-wrangling-reshaping/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/06/22/data-wrangling-reshaping/?utm_source=rss&utm_medium=rss&utm_campaign=data-wrangling-reshaping"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/monitor.png" target="_blank"><img alt="" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/monitor.png?resize=275%2C140" class="wp-image-5943" data-recalc-dims="1"></a><br>
Data wrangling is a task of great importance in data analysis. Data wrangling, is the process of importing, cleaning and transforming raw data into actionable information for analysis. It is a time-consuming process which is estimated to take about 60-80% of analyst’s time. In this series we will go through this process. It will be a brief series with goal to craft the reader’s skills on the data wrangling task. This is the second part of this series and it aims to cover the reshaping of data used to turn them into a tidy form. By tidy form, we mean that each feature forms a column and each observation forms a row.</p>
<p>Before proceeding, it might be helpful to look over the help pages for the <code>spread</code>, <code>gather</code>, <code>unite</code>, <code>separate</code>, <code>replace_na</code>, <code>fill</code>, <code>extract_numeric</code>.</p>
<p>Moreover please load the following libraries.<br>
<code>install.packages(&quot;magrittr&quot;)</code><br>
<code>library(magrittr)</code><br>
<code>install.packages(&quot;tidyr&quot;)</code><br>
<code>library(tidyr)</code></p>
<p>Please run the code below in order to load the data set:</p>
<p><code>data &lt;- airquality[4:6]</code></p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/06/22/data-wrangling-reshaping-solution" target="_blank">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p>Exercise 1</p>
<p>Print out the structure of the data frame.</p>
<p>Exercise 2</p>
<p>Let’s turn the data frame in a wider form, from above and and turn the Month variable into column headings and spread the Temp values across the months they are related to.</p>
<p>Exercise 3</p>
<p>Turn the wide (exercise 2) data frame into its initial format using the <code>gather</code> function, specify the columns you would like to gather by index number.</p>
<p>Exercise 4</p>
<p>Turn the wide (exercise 2) data frame into its initial format using the <code>gather</code> function, specify the columns you would like to gather by column name.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/22/data-wrangling-reshaping/?utm_source=rss&utm_medium=rss&utm_campaign=data-wrangling-reshaping"></aside>
<div><strong>Learn more</strong> about Data Pre-Processing in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/r-data-pre-processing-data-management-shape-your-data/" target="_blank">R Data Pre-Processing &amp; Data Management – Shape your Data!</a>. In this course you will learn how to:
<ul>
<li>import data into R in several ways while also beeing able to identify a suitable import tool</li>
<li>use SQL code within R</li>
<li>And much more</li>
</ul>
</div>
</div>
<p>Exercise 5</p>
<p>Turn the wide (exercise 2) data frame into its initial format using the <code>gather</code> function, specify the columns by using remaining column names(the ones you don’t use for gathering).</p>
<p>Exercise 6</p>
<p>Unite the variables <code>Day</code> and <code>Month</code> to a new feature named <code>Date</code> with the format <code>%d-%m </code>.</p>
<p>Exercise 7</p>
<p>Create the data frame at its previous format (exercise 6). Separate the variable you have created before (<code>Date</code>) to <code>Day</code>, <code>Month</code>.</p>
<p>Exercise 8 </p>
<p>Replace the missing values (NA) with <code>'Unknown'</code>.</p>
<p>Exercise 9 </p>
<p>Run the script below, so that you make a new feature <code>year</code>.<br>
<code>back2long_na$year &lt;- rep(NA, nrow(back2long_na))</code></p>
<p><code>back2long_na$year[1] &lt;- '2015'</code><br>
<code>back2long_na$year[as.integer(nrow(back2long_na)/3)] &lt;- '2016'</code></p>
<p><code>back2long_na$year[as.integer(2*nrow(back2long_na)/3)] &lt;- '2017'</code></p>
<p>You have noticed, that the new column has many values. Fill the NAs with the non-missing value write above it. (eg.the NA’s that are below the ‘2016’ and ‘2017’ value assign it to ‘2016’. </p>
<p>Hint: use the <code>fill</code> function. </p>
<p>Exercise 10 </p>
<p>Extract the numeric values from the <code>Temp</code> feature. </p>
<p>Hint: <code>extract_numeric</code>, this is a very important function when the variable we apply the function on is a character with ‘noise’, for example ‘$40’ and you want to transform it to 40. </p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/07/06/data-shape-transformation-with-reshape/" title="Data Shape Transformation With Reshape()" target="_blank">Data Shape Transformation With Reshape() </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/04/11/building-shiny-app-exercises-part-10/" title="Building Shiny App exercises part 10" target="_blank">Building Shiny App exercises part 10 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/08/26/reshape-2-exercises/" title="Reshape 2 Exercises" target="_blank">Reshape 2 Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/06/22/data-wrangling-reshaping/?utm_source=rss&utm_medium=rss&utm_campaign=data-wrangling-reshaping"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/DsazRsmgrUk" height="1"></a></div>