Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is your preferred style for naming variables in R?
    primarykey
    data
    text
    <p>Which conventions for naming variables and functions do you favor in R code? </p> <p>As far as I can tell, there are several different conventions, all of which coexist in cacophonous harmony:</p> <p><strong>1. Use of period separator, e.g.</strong> </p> <pre><code> stock.prices &lt;- c(12.01, 10.12) col.names &lt;- c('symbol','price') </code></pre> <p><em>Pros:</em> Has historical precedence in the R community, prevalent throughout the R core, and recommended by <a href="https://google.github.io/styleguide/Rguide.xml" rel="noreferrer">Google's R Style Guide</a>.</p> <p><em>Cons:</em> Rife with object-oriented connotations, and confusing to R newbies</p> <p><strong>2. Use of underscores</strong> </p> <pre><code> stock_prices &lt;- c(12.01, 10.12) col_names &lt;- c('symbol','price') </code></pre> <p><em>Pros:</em> A common convention in many programming langs; favored by <a href="http://had.co.nz/stat405/resources/r-style-guide.html" rel="noreferrer">Hadley Wickham's Style Guide</a>, and used in ggplot2 and plyr packages. </p> <p><em>Cons:</em> Not historically used by R programmers; is annoyingly mapped to '&lt;-' operator in Emacs-Speaks-Statistics (alterable with 'ess-toggle-underscore').</p> <p><strong>3. Use of mixed capitalization (camelCase)</strong></p> <pre><code> stockPrices &lt;- c(12.01, 10.12) colNames &lt;- c('symbol','price') </code></pre> <p><em>Pros:</em> Appears to have wide adoption in several language communities.</p> <p><em>Cons:</em> Has recent precedent, but not historically used (in either R base or its documentation).</p> <p>Finally, as if it weren't confusing enough, I ought to point out that the Google Style Guide argues for dot notation for variables, but mixed capitalization for functions.</p> <p>The lack of consistent style across R packages is problematic on several levels. From a developer standpoint, it makes maintaining and extending other's code difficult (esp. where its style is inconsistent with your own). From a R user standpoint, the inconsistent syntax steepens R's learning curve, by multiplying the ways a concept might be expressed (e.g. is that date casting function asDate(), as.date(), or as_date()? No, it's as.Date()).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload