<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-from-the-turn-of-the-century/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.jumpingrivers.com/blog/r-from-the-turn-of-the-century/"> r – Jumping Rivers</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Last week I spent some time reminiscing about my PhD and looking through some old R code. This trip down memory lane led to some of my old R scripts that amazingly still run. My R scripts were fairly simple and just created a few graphs. However now that I’ve been programming in R for a while, with hindsight (and also things have changed), my original R code could be improved.</p>
<p>I wrote this code around April 2000. To put this into perspective,</p>
<ul>
<li>R mailing list was started in 1997</li>
<li>R version 1.0 was released in Feb 29, 2000</li>
<li>The initial release of Git was in 2005</li>
<li>Twitter started in 2006</li>
<li>StackOverflow was launched in 2008</li>
</ul>
<p>Basically, sharing code and getting help was much more tricky than today – so cut me some slack!</p>
<h2>The Original Code</h2>
<p>My original code was fairly simple – a collection of <code>scan()</code> commands with some <code>plot()</code> and <code>lines()</code> function calls.</p>
<pre><code>## Bad code, don't copy!
## Seriously, don't copy
par(cex=2)
a&lt;-scan(&quot;data/time10.out&quot;,list(x=0))       
c&lt;-seq(0,120) 
plot(c,a$x,type='l',xlab=&quot;Counts&quot;,ylim=c(0,0.08),ylab=&quot;Probability&quot;,lwd=2.5)
a&lt;-scan(&quot;data/time15.out&quot;,list(x=0))       
lines(c,a$x,col=2,lwd=2.5)
a&lt;-scan(&quot;data/time20.out&quot;,list(x=0))       
lines(c,a$x,col=3,lwd=2.5)
a&lt;-scan(&quot;data/time25.out&quot;,list(x=0))       
lines(c,a$x,col=4,lwd=2.5)
a&lt;-scan(&quot;data/time30.out&quot;,list(x=0))       
lines(c,a$x,col=6,lwd=2.5)
abline(h=0)
legend(90,0.08,lty=c(1,1,1,1,1),lwd=2.5,col=c(1,2,3,4,6), c(&quot;t=10&quot;,&quot;t=15&quot;,&quot;t=20&quot;,&quot;t=25&quot;,&quot;t=30&quot;)) 
</code></pre>
<p>The resulting graph ended up in my thesis and a black and white version in a resulting <a rel="nofollow" href="http://www.mas.ncl.ac.uk/~ncsg3/content/papers/renshaw2008.pdf" target="_blank">paper</a>. Notice that it took over eight years to get published! A combination of focusing on my thesis, very long review times (over a year) and that we sent the paper via snail mail to journals.</p>
<p><img src="https://i2.wp.com/www.jumpingrivers.com/wp-content/uploads/2018/09/setup-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>How I should have written the code</h2>
<p>Within the code, there are a number of obvious improvements that could be made.</p>
<ol>
<li>
<p>In 2000, it appears that I didn’t really see the need for formatting code. A few spaces around assignment arrows would be nice.</p>
</li>
<li>
<p>I could have been cleverer with my <code>par()</code> settings. See our recent <a rel="nofollow" href="https://www.jumpingrivers.com/blog/styling-base-r-graphics/" target="_blank">blog post</a> on styling base graphics.</p>
</li>
<li>
<p>My file extensions for the data sets weren’t great. For some reason, I used <code>.out</code> instead of <code>.csv</code>.</p>
</li>
<li>
<p>I used <code>scan()</code> to read in the data. It would be much nicer using <code>read.csv()</code>.</p>
</li>
<li>
<p>My variable names could be more informative, for example, avoiding <code>c</code> and <code>a</code></p>
</li>
<li>
<p>Generating some of the vectors could be more succinct. For example</p>
</li>
</ol>
<p></p>
<pre><code>rep.int(1, 5) # instead of
c(1, 1, 1, 1, 1)
</code></pre>
<p>and</p>
<pre><code>0:120 # instead of
seq(0, 120)
</code></pre>
<p>Overall, other than my use of <code>scan()</code>, the actual code would be remarkably similar.</p>
<h2>A tidyverse version</h2>
<p>An interesting experiment is how the code structure differs using a <strong>tidyverse</strong>. The first step is to load the necessary packages</p>
<pre><code>library(&quot;fs&quot;) # Overkill here
library(&quot;purrr&quot;) # Fancy for loops 
library(&quot;readr&quot;) # Reading in csv files
library(&quot;dplyr&quot;) # Manipulation of data frames
library(&quot;ggplot2&quot;) # Plotting
</code></pre>
<p>The actual tidyverse inspired code consists of three main section</p>
<ul>
<li>Read the data into a single data frame/tibble using <code>purrr::map_df()</code></li>
<li>Cleaning up the data frame using <code>mutate()</code> and <code>rename()</code></li>
<li>Plotting the data using <strong>gplot2</strong></li>
</ul>
<p>The amount of code is similar in length</p>
<pre><code>dir_ls(path = &quot;data&quot;) %>% # list files
  map_df(read_csv, .id = &quot;filename&quot;, 
         col_names = FALSE) %>% # read &amp; combine files
  mutate(Time = rep(0:120, 5)) %>% # Create Time column
  rename(&quot;Counts&quot; = &quot;X1&quot;) %>% # Rename column
  ggplot(aes(Time, Counts)) +
  geom_line(aes(colour = filename)) + 
  theme_minimal() + # Nicer theme
  scale_colour_viridis_d(labels = paste0(&quot;t = &quot;, seq(10, 30, 5)), 
                         name = NULL) # Change colours
</code></pre>
<p>and gives a similar (but nicer) looking graph. <img src="https://i1.wp.com/www.jumpingrivers.com/wp-content/uploads/2018/09/blog-2018-oldr-unnamed-chunk-5-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>I lied about my code working</h2>
<p>Everyone who uses R knows that there are two assignment operators: <code>&lt;-</code> and <code>=</code>. These operators are (more or less, but not quite) equivalent. However, when R was first created, there was another assignment operator, the underscore <code>_</code>. My original code actually used the <code>_</code> as the assignment operator, i.e.</p>
<pre><code>a_scan(&quot;data/time10.out&quot;,list(x=0))       
</code></pre>
<p>instead of</p>
<pre><code>a&lt;-scan(&quot;data/time10.out&quot;,list(x=0))       
</code></pre>
<p>I can’t find <code>_</code> operator was finally removed from R, I seem to recall around 2005.</p>
<hr>
<p><a rel="nofollow" href="https://www.jumpingrivers.com/training/all-courses/?utm_source=blog&utm_medium=image&utm_campaign=2018oldr" target="_blank"><img alt="" src="https://i2.wp.com/www.jumpingrivers.com/wp-content/uploads/2018/09/300_125.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>The post <a rel="nofollow" href="https://www.jumpingrivers.com/blog/r-from-the-turn-of-the-century/" target="_blank">R from the turn of the century</a> appeared first on <a rel="nofollow" href="https://www.jumpingrivers.com/" target="_blank">Jumping Rivers</a>.</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://www.jumpingrivers.com/blog/r-from-the-turn-of-the-century/"> r – Jumping Rivers</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/80VyGBwyE-s" height="1"></a></div>