<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/r-tip-use-drop-false-with-data-frames/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/02/r-tip-use-drop-false-with-data-frames/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Another <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">tip</a>. Get in the habit of using <code>drop = FALSE</code> when indexing (using <code>[ , ]</code> on) <code>data.frame</code>s.</p>
<p><center><img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/02/NewImage.png?resize=600%2C450" alt="NewImage" width="450" title="NewImage.png" data-recalc-dims="1">
<p>
<p><a rel="nofollow" href="https://en.wikipedia.org/wiki/Prince_Rupert%27s_Drop" target="_blank">Prince Rupert’s drops</a> (img: <a rel="nofollow" href="https://commons.wikimedia.org/wiki/File:Prince_Ruperts_drops.jpg" target="_blank">Wikimedia Commons</a>)</p>
<p><span></span></p>
<p>In R, single column <code>data.frame</code>s are often converted to vectors when manipulated. For example:</p>
<pre>
d &lt;- data.frame(x = seq_len(3))
print(d)
#>   x
#> 1 1
#> 2 2
#> 3 3
</pre>
<pre>
# not a data frame!
d[order(-d$x), ]
#> [1] 3 2 1
</pre>
<p>We were merely trying to re-order the rows and the result was converted to a vector. This happened because the rules for <code>[ , ]</code> change if there is only one result column. This happens <em>even</em> if the there had been only one input column. Another example is: <code>d[,]</code> is also vector in this case.</p>
<p>The issue is: if we are writing re-usable code we are often programming before we know complete contents of a variable or argument. For a <code>data.frame</code> named “<code>g</code>” supplied as an argument: <code>g[vec, ]</code> can be a <code>data.frame</code> or a <code>vector</code> (or even possibly a <code>list</code>). However we do know if <code>g</code> is a <code>data.frame</code> then <code>g[vec, , drop = FALSE]</code> is also a <code>data.frame</code> (assuming <code>vec</code> is a vector of valid row indices or a <code>logical</code> vector, note: <code>NA</code> induces some special cases).</p>
<p>We care as <code>vector</code>s and <code>data.frame</code>s have different semantics, so are not fully substitutable in later code.</p>
<p>The fix is to include <code>drop = FALSE</code> as a third argument to <code>[ , ]</code>.</p>
<pre>
# is a data frame.
d[order(-d$x), , drop = FALSE]
#>   x
#> 3 3
#> 2 2
#> 1 1
</pre>
<p>To pull out a column I suggest using one of the <em>many</em> good extraction notations (all using the fact a <code>data.frame</code> is officially a list of columns):</p>
<pre>
d[[&quot;x&quot;]]
#> [1] 1 2 3

d$x
#> [1] 1 2 3

d[[1]]
#> [1] 1 2 3
</pre>
<p>My overall advice is: get in the habit of including <code>drop = FALSE</code> when working with <code>[ , ]</code> and <code>data.frame</code>s. I say do this even when it is obvious that the result does in fact have more than one column.</p>
<p>For example write “<code>mtcars[, c(&quot;mpg&quot;, &quot;cyl&quot;), drop = FALSE]</code>” instead of “<code>mtcars[, c(&quot;mpg&quot;, &quot;cyl&quot;)]</code>“. It is clear that for <code>data.frame</code>s both forms <em>should</em> work the same (either selecting a data frame with two columns, or throwing an error if we have mentioned a non existent column). But longer <code>drop = FALSE</code> form is safer (go further towards ensuring <a rel="nofollow" href="http://www.johnmyleswhite.com/notebook/2013/12/06/writing-type-stable-code-in-julia/" target="_blank">type stable code</a>) and more importantly <em>documents intent</em> (that you wanted a <code>data.frame</code> result).</p>
<p>One can also try <a rel="nofollow" href="http://www.win-vector.com/blog/2018/02/is-r-basesubset-really-that-bad/" target="_blank">base::subset()</a>, as it has non-dropping defaults.</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="http://www.win-vector.com/blog/2018/02/r-tip-use-drop-false-with-data-frames/"> R – Win-Vector Blog</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/J4VaZ0C2OKo" height="1"></a></div></center>