<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/using-r-reshape2-to-tidyr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://martinsbioblogg.wordpress.com/2017/12/17/using-r-reshape2-to-tidyr/"> R – On unicorns and genes</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Tidy data — it’s one of those terms that tend to confuse people, and certainly confused me. It’s Codd’s third normal form, but you can’t go around telling that to people and expect to be understood. One form is ”long”, the other is ”wide”. One form is ”melted”, another ”cast”. One form is ”gathered”, the other ”spread”. To make matters worse, I often botch the explanation and mix up at least two of the terms.</p>
<p>The word is also associated with the tidyverse suite of R packages in a somewhat loose way. But you don’t need to write in a tidyverse-style (including the %>%s and all) to enjoy tidy data.</p>
<p>But <a rel="nofollow" href="http://vita.had.co.nz/papers/tidy-data.pdf" target="_blank">Hadley Wickham’s definition</a> is straightforward:</p>
<blockquote>
<p>In tidy data:<br>
1. Each variable forms a column.<br>
2. Each observation forms a row.<br>
3. Each type of observational unit forms a table.</p>
</blockquote>
<p>In practice, I don’t think people always take their data frames all the way to tidy. For example, to make a scatterplot, it is convenient to keep a couple of variables as different columns. The key is that we need to move between different forms rapidly (brain time-rapidly, more than computer time-rapidly, I might add).</p>
<p>And not everything should be organized this way. If you’re a geneticist, genotypes are notoriously inconvenient in normalized form. Better keep that individual by marker matrix.</p>
<p>The first serious piece of R code I wrote for someone else was a function to turn data into long form for plotting. I suspect plotting is often the gateway to tidy data. The function was like you’d expect from R code written by a beginner who comes from C-style languages: It reinvented the wheel, and I bet it had nested for loops, a bunch of hard bracket indices, and so on. Then I discovered reshape2.</p>
<pre>
library(reshape2)
fake_data &lt;- data.frame(id = 1:20,
                        variable1 = runif(20, 0, 1),
                        variable2 = rnorm(20))
melted &lt;- melt(fake_data, id.vars = &quot;id&quot;)
</pre>
<p>The id.vars argument is to tell the function that the id column is the key, a column that tells us which individual each observation comes from. As the name suggests, id.vars can name multiple columns in a vector.</p>
<p>So the is the data before:</p>
<pre>
  id   variable1    variable2
1  1 0.938173781  0.852098580
2  2 0.408216233  0.261269134
3  3 0.341325188  1.796235963
4  4 0.958889279 -0.356218000
</pre>
<p>And this is after. This time the data frame doesn’t become wider. There are still three columns. But we go from 20 rows to 40: two variables times 20 individuals.</p>
<pre>
  id  variable       value
1  1 variable1 0.938173781
2  2 variable1 0.408216233
3  3 variable1 0.341325188
4  4 variable1 0.958889279
</pre>
<p>And now: tidyr. tidyr is the new tidyverse package for rearranging data like this.</p>
<p>The tidyr equivalent of the melt function is called gather. There are two important differences that messed with my mind at first.</p>
<p>The melt and gather functions take the opposite default assumption about what columns should be treated as keys and what columns should be treated as containing values. In melt, as we saw above, we need to list the keys to keep them with each observation. In gather, we need to list the value columns, and the rest will be treated as keys.</p>
<p>Also, the second and third arguments (and they would be the first and second if you piped something into it), are the variable names that will be used in the long form data. In this case, to get a data frame that looks exactly the same as the first, we will stick with ”variable” and ”value”.</p>
<p>Here are five different ways to get the same long form data frame as above:</p>
<pre>
library(tidyr)
melted &lt;- gather(fake_data, variable, value, 2:3)

## Column names instead of indices
melted &lt;- gather(fake_data, variable, value, variable1, variable2)

## Excluding instead of including
melted &lt;- gather(fake_data, variable, value, -1)

## Excluding using column name
melted &lt;- gather(fake_data, variable, value, -id)

## With pipe
melted &lt;- fake_data %>% gather(variable, value, -id)
</pre>
<p>Usually, this is the transformation we need: wide to long. If we need to go the other way, we can use plyr’s cast functions, and tidyr’s gather. This code recovers the original data frame:</p>
<pre>
## plyr
dcast(melted, id ~  variable)

## tidyr
spread(melted, variable, value)
</pre>
<p>Postat i:<a rel="nofollow" href="https://martinsbioblogg.wordpress.com/category/computer-stuff/" target="_blank">computer stuff</a>, <a rel="nofollow" href="https://martinsbioblogg.wordpress.com/category/data-analysis/" target="_blank">data analysis</a>, <a rel="nofollow" href="https://martinsbioblogg.wordpress.com/category/english/" target="_blank">english</a> Tagged: <a rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/r/" target="_blank">R</a>, <a rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/reshape2/" target="_blank">reshape2</a>, <a rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/tidyr/" target="_blank">tidyr</a> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/godelicious/martinsbioblogg.wordpress.com/5587/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/delicious/martinsbioblogg.wordpress.com/5587/"></a> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gofacebook/martinsbioblogg.wordpress.com/5587/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/facebook/martinsbioblogg.wordpress.com/5587/"></a> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gotwitter/martinsbioblogg.wordpress.com/5587/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/twitter/martinsbioblogg.wordpress.com/5587/"></a> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gostumble/martinsbioblogg.wordpress.com/5587/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/stumble/martinsbioblogg.wordpress.com/5587/"></a> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/godigg/martinsbioblogg.wordpress.com/5587/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/digg/martinsbioblogg.wordpress.com/5587/"></a> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/goreddit/martinsbioblogg.wordpress.com/5587/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/reddit/martinsbioblogg.wordpress.com/5587/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://martinsbioblogg.wordpress.com/2017/12/17/using-r-reshape2-to-tidyr/"> R – On unicorns and genes</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/B6CBsXYlwEQ" height="1"></a></div>