<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/an-object-has-no-name/"></div></div><div><a data-url="http://www.r-bloggers.com/an-object-has-no-name/" data-counturl="http://www.r-bloggers.com/an-object-has-no-name/" data-count="horizontal" data-text="An object has no name" 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://blog.revolutionanalytics.com/2016/05/an-object-has-no-name.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>No, it's not a <a target="_blank" href="http://gameofthrones.wikia.com/wiki/Jaqen_H%27ghar">Jaqen H'ghar</a> quote. Recently, Hadley Wickham <a target="_blank" href="https://twitter.com/hadleywickham/status/732288980549390336">tweeted the following image</a>:</p>
<p><a target="_blank" href="https://twitter.com/hadleywickham/status/732288980549390336/photo/1"><img title="Hadley-names" alt="Hadley-names" src="http://a5.typepad.com/6a0105360ba1c6970c01bb090797a5970d-450wi"></a></p>
<p>While this image isn't included in Hadley's <a target="_blank" href="http://adv-r.had.co.nz/">Advanced R book</a>, he does discuss <a target="_blank" href="http://adv-r.had.co.nz/Performance.html">many of the implications</a> there. The most significant of these is that <strong>creating a copy of an object in R doesn't consume any additional memory</strong>. (Most of the time, anyway: there are exceptions, but I'm not going into them here.) The simplest example is the following:</p>
<p><code>a &lt;- matrix(0, ncol=1000, nrow=1000)</code><br><code>b &lt;- a</code></p>
<p>Creating <span>a</span> allocates 8Mb of memory, but creating <span>b</span> requires basically nothing: all that gets created is a new <em>name</em>, <span>b</span>, that points to the same data as <span>a</span>. This isn't like a pointer in C though; if you later modify <span>a</span> or <span>b</span> a new 8Mb matrix will be created in memory to preserve the logical semantics of there being two separate copies. As a user, you don't have to worry about any of this: it all happens behind the scenes, seamlessly, thanks to the &quot;names have objects&quot; concept Hadley illustrates above. </p>
<p>The same thing applies to the call-by-value semantics of functions: if you pass an object to a function, as in <span>eigen(a)</span>,  the <em>semantics</em> of R is that a copy of the object <span>a</span> is passed to the function <span>eigen</span>, with which it can do as it pleases without modifying the global copy. In practice though, no new memory will be needed for the copy as long as the function doesn't actually mess with the object passed to it. You can prove this to yourself by using the <a target="_blank" href="https://rstudio.github.io/profvis/">provfis package</a>, or by using the <a target="_blank" href="https://blog.rstudio.org/2016/05/23/profiling-with-rstudio-and-profvis/">profiling tools in the preview release of RStudio</a>.</p>
<p>This is all likely second nature to experienced R programmers, but it can be a bit of a shock to programmers coming to R from other languages. Miles McBain (who learned to program in C++) had a it of an epiphany on seeing Hadley's diagram, and <a target="_blank" href="http://bragqut.github.io/2016/05/26/milesmcbain-rnoprimitives/">explored many of the implications in a blog post</a> which is well worth reading. His take-away:</p>
<blockquote>
<p>If you’re trying to optimise R while thinking like a C++ coder, you may well be doing more harm than good. I myself have fallen foul of this in an attempt to modify data frames in place with my <code>pushr</code> package. It ended up just being syntactic sugar, with no observable performance boost.</p>
</blockquote>
<p>If you think you might be in the same boat, take a look at Miles's post linked below.</p>
<p>One weiRd tip: <a target="_blank" href="http://bragqut.github.io/2016/05/26/milesmcbain-rnoprimitives/">R Has No Primitives</a></p>
</div>
<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/an-object-has-no-name/"></div></div><div><a data-url="http://www.r-bloggers.com/an-object-has-no-name/" data-counturl="http://www.r-bloggers.com/an-object-has-no-name/" data-count="vertical" data-text="An object has no name" 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://blog.revolutionanalytics.com/2016/05/an-object-has-no-name.html"> Revolutions</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/PcK-YtrsBsk"></a></div>