<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%e2%81%b6-idiomatic-for-the-people/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/05/23/r%E2%81%B6-idiomatic-for-the-people/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>NOTE: I’ll do my best to ensure the next post will have nothing to do with Twitter, and this post might not completely meet my R⁶ criteria. </p>
<p>A single, altruistic, nigh exuberant R tweet about slurping up a directory of CSVs devolved quickly — at least in my opinion, and partly (sadly) with my aid — into a thread that ultimately strayed from a crucial point: <em>idiomatic is in the eye of the beholder</em>. </p>
<p>I’m not linking to the twitter thread, but there are enough folks with sufficient Klout scores on it (is Klout even still a thing?) that you can easily find it if you feel so compelled. </p>
<p>I’ll take a page out of the U.S. High School “write an essay” playbook and start with a definition of <em>idiomatic</em>:</p>
<blockquote>
<p><em>using, containing, or denoting expressions that are natural to a native speaker</em></p>
</blockquote>
<p>That comes from <em>idiom</em>:</p>
<blockquote>
<p><em>a form of expression natural to a language, person, or group of people</em></p>
</blockquote>
<p>I usually joke with my students that a strength (and weakness) of R is that there are ~twelve ways to do any given task. While the statement is deliberately hyperbolic, the core message is accurate: there’s more than one way to do most things in R. A cascading truth is: what makes one way more “correct” over another often comes down to idiom.</p>
<p>My rstudio::conf 2017 presentation included an example of my version of using <code>purrr</code> for idiomatic CSV/JSON directory slurping. There are lots of ways to do this in R (the point of the post is not really to show you how to do the directory slurping and it is unlikely that I’ll approve comments with code snippets about that task). Here are three. One from base R tribe, one from the <code>data.table</code> tribe and one from the <code>tidyverse</code> tribe:</p>
<pre><code># We need some files and we'll use base R to make some
dir.create(&quot;readings&quot;)
for (i in 1970:2010) write.csv(mtcars, file.path(&quot;readings&quot;, sprintf(&quot;%s.csv&quot;, i)), row.names=FALSE)

fils &lt;- list.files(&quot;readings&quot;, pattern = &quot;.csv$&quot;, full.names=TRUE)

do.call(rbind, lapply(fils, read.csv, stringsAsFactors=FALSE))

data.table::rbindlist(lapply(fils, data.table::fread))

purrr::map_df(fils, readr::read_csv)</code></pre>
<p>You get data for all the “years” into a <code>data.frame</code>, <code>data.table</code> and <code>tibble</code> (respectively) with those three “phrases”. </p>
<p>However, what if you want the year as a column? Many of these “datalogger” CSV data sets do not have a temporal “grouping” variable as they let the directory structure &amp; naming conventions embed that bit of metadata. That information would be nice, though:</p>
<pre><code>do.call(rbind, lapply(fils, function(x) {
  f &lt;- read.csv(x, stringsAsFactors=FALSE)
  f$year &lt;- gsub(&quot;^readings/|\\.csv$&quot;, &quot;&quot;, x)
  f
}))

dt &lt;- data.table::rbindlist(lapply(fils, data.table::fread), idcol=&quot;year&quot;)
dt[, year := gsub(&quot;^readings/|\\.csv$&quot;, &quot;&quot;, fils[year])]

purrr::map_df(fils, readr::read_csv, .id = &quot;year&quot;) %>% 
  dplyr::mutate(year = stringr::str_replace_all(fils[as.numeric(year)],
                                                &quot;^readings/|\\.csv$&quot;, &quot;&quot;))</code></pre>
<p>All three versions do the same thing, and each tribe understands each idiom.</p>
<p>The <code>data.table</code> and <code>tidyverse</code> versions get you much faster file reading and the ability to “fill” missing columns — another common slurping task. You can hack something together in base R to do column fills (you’ll find a few StackOverflow answers that accomplish such a task) but you will likely decide to choose one of the other idioms for that and become equally as comfortable in that new idiom.</p>
<p>There are multiple ways to further extend the slurping example, but that’s not the point of the post.</p>
<p>Each set of snippets contains 100% valid R code. They accomplish the task and are <em>idiomatic for each tribe</em>. Despite what any <em>“mil gun feos turrach na latsa”</em> experts’ exchange would try to tell you, the best idiom is the one that works for you/you &amp; your collaborators and the one that gets you to the real work — data analysis — in the most straightforward &amp; reproducible way possible (for you).</p>
<p>Idiomatic does not mean there’s only a singular One, True Way™, and I think a whole host of us forget that at times. </p>
<p>Write good, clean, error-free, reproducible code.</p>
<p>Choose idioms that work best for you and your collaborators.</p>
<p>Adapt when necessary.</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://rud.is/b/2017/05/23/r%E2%81%B6-idiomatic-for-the-people/"> R – rud.is</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/xKiGRGrTME8" height="1"></a></div>