<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/data-shape-transformation-with-reshape/"></div></div><div><a data-url="http://www.r-bloggers.com/data-shape-transformation-with-reshape/" data-counturl="http://www.r-bloggers.com/data-shape-transformation-with-reshape/" data-count="horizontal" data-text="Data Shape Transformation With Reshape()" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/07/06/data-shape-transformation-with-reshape/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://i0.wp.com/r-exercises.com/wp-content/uploads/2016/06/1280px-Origami-crane.jpg"><img sizes="(max-width: 300px) 100vw, 300px" srcset="http://i2.wp.com/r-exercises.com/wp-content/uploads/2016/06/1280px-Origami-crane-300x241.jpg?resize=300%2C241 300w, http://r-exercises.com/wp-content/uploads/2016/06/1280px-Origami-crane-768x617.jpg 768w, http://r-exercises.com/wp-content/uploads/2016/06/1280px-Origami-crane-1024x823.jpg 1024w, http://r-exercises.com/wp-content/uploads/2016/06/1280px-Origami-crane-600x482.jpg 600w, http://r-exercises.com/wp-content/uploads/2016/06/1280px-Origami-crane.jpg 1280w" alt="1280px-Origami-crane" class="wp-image-898" src="http://i2.wp.com/r-exercises.com/wp-content/uploads/2016/06/1280px-Origami-crane-300x241.jpg?resize=300%2C241" data-recalc-dims="1"></a><code>reshape()</code> is an R function that accesses “observations” in grouped dataset columns and “records” in dataset rows, in order to programmatically transform the dataset shape into “long” or “wide” format.</p>
<p>Required dataframe:<br>
<code>data1 &lt;- data.frame(id=c(&quot;ID.1&quot;, &quot;ID.2&quot;, &quot;ID.3&quot;),<br>
sample1=c(5.01, 79.40, 80.37),<br>
sample2=c(5.12, 81.42, 83.12),<br>
sample3=c(8.62, 81.29, 85.92))</code></p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/07/06/data-shape-transformation-with-reshape-solutions/">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Wide-to-Long:<br>
Using the <code>reshape()</code> parameter “<code>direction=</code>“, “<code>varying=</code>” columns are stacked according to the new records created by the “<code>idvar=</code>” column.</p>
<p>Therefore, convert “<code>data1</code>” to long format, by stacking columns 2 through 4. The new row names are from column “<code>id</code>“. The new time variable is called, “<code>TIME</code>“. The column name of the stacked data is called “<code>Sample</code>“. Set a new dataframe variable called, “<code>data2</code>“.</p>
<p><strong>Exercise 2</strong><br>
Long-to-Wide:<br>
Use <code>direction=&quot;wide&quot;</code> to convert “<code>data2</code>” back to the shape of “<code>data1</code>“. Setting a new variable isn’t needed. (Note that rownames from “<code>data2</code>” are retained.)</p>
<p><strong>Exercise 3</strong><br>
Time Variables:<br>
Script a <code>reshape()</code> operation, where “<code>timevar=</code>” is set to the variable within “<code>data2</code>” that differentiates multiple records.</p>
<p><strong>Exercise 4</strong><br>
New Row Names:<br>
Script a <code>reshape()</code> operation, where “<code>data2</code>” is converted to “<code>wide</code>” format, and “<code>new.row.names=</code>” is set to unique “<code>data2$id</code>” names.</p>
<p><strong>Exercise 5</strong><br>
Convert “<code>data2</code>” to wide format. Set “<code>v.names=</code>” to the “<code>data2</code>” column with observations.</p>
<p><strong>Exercise 6</strong><br>
Set <code>sep = &quot;&quot;</code> in order to reshape “<code>data1</code>” to long format.</p>
<p><strong>Exercise 7</strong><br>
Reshape “<code>data2</code>” to “<code>wide</code>“. Use the “<code>direction =</code>” parameter. Setting a new dataframe variable isn’t required.</p>
<p><strong>Exercise 8</strong><br>
Use the most basic reshape command possible, in order to reshape<br>
“<code>data2</code>” to wide format.</p>
<p><strong>Exercise 9</strong><br>
Reshape “<code>data2</code>” to “<code>wide</code>“, with column names for the reshaped data of “<code>TIME</code>” and “<code>Sample</code>“.</p>
<p><strong>Exercise 10</strong><br>
Reshape “<code>data1</code>” by varying “<code>sample1</code>“, “<code>sample2</code>“, and “<code>sample3</code>“.</p>
<p>Image by Andreas Bauer (Own work) [<a target="_blank" href="https://creativecommons.org/licenses/by-sa/2.5/">CC-BY-SA-2.5</a>], <a target="_blank" href="https://commons.wikimedia.org/wiki/Origami#/media/File:Origami-crane.jpg">via Wikimedia Commons</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/data-shape-transformation-with-reshape/"></div></div><div><a data-url="http://www.r-bloggers.com/data-shape-transformation-with-reshape/" data-counturl="http://www.r-bloggers.com/data-shape-transformation-with-reshape/" data-count="vertical" data-text="Data Shape Transformation With Reshape()" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/07/06/data-shape-transformation-with-reshape/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Qp5wwyS07UY"></a></div>