<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/whether-to-use-a-data-frame-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://privefl.github.io/blog/whether-to-use-a-data-frame-in-r/"> Florian Privé</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<section>
<p>In this post, I try to show you in which situations using a data frame is appropriate, and in which it’s not.</p>
<p>Learn more with the <a rel="nofollow" href="https://adv-r.hadley.nz/" target="_blank">Advanced R book</a>.</p>
<div>
<h2>What is a data frame?</h2>
<p>A data frame is just a list of vectors of the same length, each vector being a column.</p>
<p>This may convince you:</p>
<div>
<pre><code><span>str</span>(iris)</code></pre>
</div>
<pre><code>## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels &quot;setosa&quot;,&quot;versicolor&quot;,..: 1 1 1 1 1 1 1 1 1 1 ...</code></pre>
<div>
<pre><code><span>is.list</span>(iris)</code></pre>
</div>
<pre><code>## [1] TRUE</code></pre>
<div>
<pre><code><span>length</span>(iris)</code></pre>
</div>
<pre><code>## [1] 5</code></pre>
<div>
<pre><code><span>sapply</span>(iris, typeof)</code></pre>
</div>
<pre><code>## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##     &quot;double&quot;     &quot;double&quot;     &quot;double&quot;     &quot;double&quot;    &quot;integer&quot;</code></pre>
<div>
<pre><code><span>sapply</span>(iris, length)</code></pre>
</div>
<pre><code>## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##          150          150          150          150          150</code></pre>
</div>
<div>
<h2>What is a list?</h2>
<p>A list is just a vector of references to objects in memory.</p>
<div>
<pre><code>x &lt;-<span> </span><span>1</span><span>:</span><span>1e6</span>
pryr<span>::</span><span>object_size</span>(x)</code></pre>
</div>
<pre><code>## 4 MB</code></pre>
<div>
<pre><code>y &lt;-<span> </span><span>list</span>(x, x, x)
pryr<span>::</span><span>object_size</span>(y)</code></pre>
</div>
<pre><code>## 4 MB</code></pre>
<div>
<pre><code>address &lt;-<span> </span>data.table<span>::</span>address
<span>address</span>(x)</code></pre>
</div>
<pre><code>## [1] &quot;000000001E49C530&quot;</code></pre>
<div>
<pre><code><span>sapply</span>(y, address)</code></pre>
</div>
<pre><code>## [1] &quot;000000001E49C530&quot; &quot;000000001E49C530&quot; &quot;000000001E49C530&quot;</code></pre>
<p>So, basically, here <code>y</code> is a vector of 3 references, each pointing to the same object <code>x</code> in memory. This is very efficient because there is no need to copy <code>x</code> 3 times when creating <code>y</code>.</p>
</div>
<div>
<h2>Using package {dplyr}</h2>
<p>Using {dplyr} operations such as <code>mutate</code> or <code>select</code> is very efficient.</p>
<ul>
<li>
<p><code>select</code>:</p>
<div>
<pre><code><span>library</span>(dplyr)
mydf &lt;-<span> </span>iris
mydf2 &lt;-<span> </span><span>select</span>(mydf, <span>-</span>Species)
<span>sapply</span>(mydf, address)</code></pre>
</div>
<pre><code>##       Sepal.Length        Sepal.Width       Petal.Length        Petal.Width            Species 
## &quot;000000001CE852F8&quot; &quot;000000001BC64EB8&quot; &quot;000000000B965428&quot; &quot;000000000B39A758&quot; &quot;000000000B356168&quot;</code></pre>
<div>
<pre><code><span>sapply</span>(mydf2, address)</code></pre>
</div>
<pre><code>##       Sepal.Length        Sepal.Width       Petal.Length        Petal.Width 
## &quot;000000001CE852F8&quot; &quot;000000001BC64EB8&quot; &quot;000000000B965428&quot; &quot;000000000B39A758&quot;</code></pre>
<p>So, when you use <code>select</code>, you get a new object. This object is a new data frame (a new list). Yet, remember that a list is nothing but a vector of references. So, this is extremely efficient because it creates only a new vector of 4 references pointing to objects already in memory.</p>
</li>
<li>
<p><code>mutate</code>:</p>
<div>
<pre><code>mydf3 &lt;-<span> </span><span>mutate</span>(iris, <span>Species =</span> <span>as.character</span>(Species))
<span>sapply</span>(mydf, address)</code></pre>
</div>
<pre><code>##       Sepal.Length        Sepal.Width       Petal.Length        Petal.Width            Species 
## &quot;000000001CE852F8&quot; &quot;000000001BC64EB8&quot; &quot;000000000B965428&quot; &quot;000000000B39A758&quot; &quot;000000000B356168&quot;</code></pre>
<div>
<pre><code><span>sapply</span>(mydf3, address)</code></pre>
</div>
<pre><code>##       Sepal.Length        Sepal.Width       Petal.Length        Petal.Width            Species 
## &quot;000000001CE852F8&quot; &quot;000000001BC64EB8&quot; &quot;000000000B965428&quot; &quot;000000000B39A758&quot; &quot;0000000020451AB0&quot;</code></pre>
<p>This is the same when using <code>mutate</code>. You get a new object, yet you modified the 5-th variable only. So, the first 4 variables don’t have to be copied, your new data frame (list) can just point to the same 4 vectors in memory. R only creates a new vector of character and points to it in the new object.</p>
</li>
</ul>
<p>So, adding/removing/modifying one variable of a data frame is efficient because R doesn’t have to copy the other variables.</p>
</div>
<div>
<h2>What about modifying one row of a data frame?</h2>
<p>If you modify the first row of a data frame, then you modify the first element of each variable. If there are multiple references to these vectors, R would decide to copy them all, getting you a full copy of the data frame.</p>
<div>
<pre><code>mydf4 &lt;-<span> </span>mydf3
<span>sapply</span>(mydf3, address)</code></pre>
</div>
<pre><code>##       Sepal.Length        Sepal.Width       Petal.Length        Petal.Width            Species 
## &quot;000000001CE852F8&quot; &quot;000000001BC64EB8&quot; &quot;000000000B965428&quot; &quot;000000000B39A758&quot; &quot;0000000020451AB0&quot;</code></pre>
<div>
<pre><code><span>sapply</span>(mydf4, address)</code></pre>
</div>
<pre><code>##       Sepal.Length        Sepal.Width       Petal.Length        Petal.Width            Species 
## &quot;000000001CE852F8&quot; &quot;000000001BC64EB8&quot; &quot;000000000B965428&quot; &quot;000000000B39A758&quot; &quot;0000000020451AB0&quot;</code></pre>
<div>
<pre><code>mydf4[<span>1</span>, ] &lt;-<span> </span>mydf3[<span>1</span>, ]
<span>sapply</span>(mydf4, address)</code></pre>
</div>
<pre><code>##       Sepal.Length        Sepal.Width       Petal.Length        Petal.Width            Species 
## &quot;0000000029BAB238&quot; &quot;0000000029BAB718&quot; &quot;000000002841AB70&quot; &quot;000000002841B050&quot; &quot;000000002841B530&quot;</code></pre>
</div>
<div>
<h2>Conclusion</h2>
<p>It is appropriate to use data frames when you want to operate on variables, but not when you want to operate on rows. If you still want or need to do so, I recommend you to watch <a rel="nofollow" href="https://www.rstudio.com/resources/webinars/thinking-inside-the-box-you-can-do-that-inside-a-data-frame/" target="_blank">this webinar</a>.</p>
</div>
</section>
	<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://privefl.github.io/blog/whether-to-use-a-data-frame-in-r/"> Florian Privé</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/7agP2eJ3v4U" height="1"></a></div>