<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/take-your-data-frames-to-the-next-level/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://realdataweb.wordpress.com/2017/03/31/take-your-data-frames-to-the-next-level/"> R – Real Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> </p>
<div>
<div></div>
<p><img sizes="(max-width: 600px) 100vw, 600px" data-image-description="" data-orig-file="https://realdataweb.files.wordpress.com/2017/03/leo.jpg?w=456" data-orig-size="600,400" data-image-title="leo" class="wp-image-458" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="458" data-permalink="https://realdataweb.wordpress.com/2017/03/31/take-your-data-frames-to-the-next-level/leo/" alt="leo" data-medium-file="https://realdataweb.files.wordpress.com/2017/03/leo.jpg?w=456?w=300" data-large-file="https://realdataweb.files.wordpress.com/2017/03/leo.jpg?w=456?w=600" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://realdataweb.files.wordpress.com/2017/03/leo.jpg?w=456"></p>
<p>While finishing up with R-rockstar Hadley Wickham’s book (<a target="_blank" rel="nofollow" href="https://realdataweb.wordpress.com/2017/01/06/free-book-r-for-data-science/">Free Book – R for Data Science</a>), <a target="_blank" rel="nofollow" href="http://r4ds.had.co.nz/model-building.html">the section on model building</a> elaborates on something pretty cool that I had no idea about – list columns.</p>
<p>Most of us have probably seen the following data frame column format:</p>
<pre><code>df &lt;- data.frame(&quot;col_uno&quot; = c(1,2,3),&quot;col_dos&quot; = c('a','b','c'), &quot;col_tres&quot; = factor(c(&quot;google&quot;, &quot;apple&quot;, &quot;amazon&quot;)))</code></pre>
<p>And the output:</p>
<pre><code>df</code></pre>
<pre><code>##   col_uno col_dos col_tres
## 1       1       a   google
## 2       2       b    apple
## 3       3       c   amazon</code></pre>
<p>This is an awesome way to organize data and one of R’s strong points. However, we can use list functionality to go deeper. Check this out:</p>
<pre><code>library(tidyverse)
library(datasets)</code></pre>
<pre><code>head(iris)</code></pre>
<pre><code>##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa</code></pre>
<pre><code>nested &lt;- iris %>%
  group_by(Species) %>%
  nest()</code></pre>
<pre><code>## # A tibble: 3 × 2
##      Species              data
##       &lt;fctr>            &lt;list>
## 1     setosa &lt;tibble [50 × 4]>
## 2 versicolor &lt;tibble [50 × 4]>
## 3  virginica &lt;tibble [50 × 4]></code></pre>
<p>Using <code>nest</code> we can compartmentalize our data frame for readability and more efficient iteration. Here we can use <code>map</code> from the <code>purrr</code> package to compute the mean of each column in our nested data.</p>
<pre><code>means &lt;- map(nested$data, colMeans)</code></pre>
<pre><code>## [[1]]
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
##        5.006        3.428        1.462        0.246 
## 
## [[2]]
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
##        5.936        2.770        4.260        1.326 
## 
## [[3]]
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
##        6.588        2.974        5.552        2.026</code></pre>
<p>Once you’re done messing around with data-ception, use <code>unnest</code> to revert your data back to its original state.</p>
<pre><code>head(unnest(nested))</code></pre>
<pre><code>## # A tibble: 6 × 5
##   Species Sepal.Length Sepal.Width Petal.Length Petal.Width
##    &lt;fctr>        &lt;dbl>       &lt;dbl>        &lt;dbl>       &lt;dbl>
## 1  setosa          5.1         3.5          1.4         0.2
## 2  setosa          4.9         3.0          1.4         0.2
## 3  setosa          4.7         3.2          1.3         0.2
## 4  setosa          4.6         3.1          1.5         0.2
## 5  setosa          5.0         3.6          1.4         0.2
## 6  setosa          5.4         3.9          1.7         0.4</code></pre>
<p>I was pretty excited to learn about this property of data.frames and will definitely make use of it in the future. If you have any neat examples of nested dataset usage, please feel free to share in the comments.  As always, I’m happy to answer questions or talk data!</p>
<p>Kiefer Smith</p>
</div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/realdataweb.wordpress.com/457/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/realdataweb.wordpress.com/457/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" 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://realdataweb.wordpress.com/2017/03/31/take-your-data-frames-to-the-next-level/"> R – Real Data</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/6kwgIR-sNLE"></a></div>