<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/fixing-rs-design-flaws-in-a-new-version-of-pqr/"></div></div><div><a data-url="http://www.r-bloggers.com/fixing-rs-design-flaws-in-a-new-version-of-pqr/" data-counturl="http://www.r-bloggers.com/fixing-rs-design-flaws-in-a-new-version-of-pqr/" data-count="horizontal" data-text="Fixing R’s design flaws in a new version of pqR" 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="https://radfordneal.wordpress.com/2016/06/25/fixing-rs-design-flaws-in-a-new-version-of-pqr/"> R Programming – Radford Neal's blog</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<table>
<tbody>
<tr>
<td valign="top"><a target="_blank" href="https://radfordneal.files.wordpress.com/2013/12/logolg.png"><img height="200" alt="" width="256" align="top" src="https://radfordneal.files.wordpress.com/2013/12/logolg.png?w=256&h=200"></a></td>
<td valign="top">I’ve released a new version of my pqR implementation of R. This version introduces extensions to the R language that fix some long-standing design flaws that were inherited from S.
<p>These language extensions make it easier to write reliable programs, that work even in edge cases, such as data sets with one observation.



<p>In particular, the extensions fix the problems that <tt>1:n</tt> doesn’t work as intended when <tt>n</tt> is zero, and that <tt>M[1:n,]</tt> is a matrix rather than a vector when <tt>n</tt> is one, or when <tt>M</tt> has only one column. Since changing the “<tt>:</tt>” operator would cause too many problems with existing programs, pqR introduces a new “<tt>..</tt>” operator for generating increasing sequences.  Unwanted dimension dropping is also addressed in ways that have minimal effects on existing code.</p>
<p>The new release, pqR-2016-06-24, is available at <a target="_blank" href="http://pqr-project.org/">pqR-project-org</a>. The <a target="_blank" href="http://pqr-project.org/NEWS.txt">NEWS</a> file for this release also documents some other language extensions, as well as fixes for various bugs (some of which are also in R-3.3.1).<br>
<span></span></p>
<p>I’ve written about these design flaws in R before, <a target="_blank" href="https://radfordneal.wordpress.com/2008/08/06/design-flaws-in-r-1-reversing-sequences/">here</a> and <a target="_blank" href="https://radfordneal.wordpress.com/2008/08/20/design-flaws-in-r-2-%E2%80%94-dropped-dimensions/">here</a> (and for my previous ideas on a solution, now obsolete, see <a target="_blank" href="https://radfordneal.wordpress.com/2008/08/25/r-design-flaws-1-and-2-a-solution-to-both/">here</a>). These design flaws have been producing unreliable programs for decades, including bugs in code maintained by R Core. It is long past time that they were fixed.</p>
<p>It is crucial that the fixes make the <em>easy</em> way of writing a program also be the <em>correct</em> way. This is not the case with previous “fixes” like the <tt>seq_len</tt> function, and the <tt>drop=FALSE</tt> option, both of which are clumsy, as well as being unknown to many R programmers.</p>
<p>Here’s an example of how the new <tt>..</tt> operator can be used:</p>
<pre>          for (i in 2..nrow(M)-1)
              for (j in 2..ncol(M)-1)
                  M[i,j] &lt;- 0
</pre>
<p>This code sets all the elements of the matrix <tt>M</tt> to zeros, except for those on the edges — in the first or last row or column.</p>
<p>If you replace the “<tt>..</tt>” operators above with “<tt>:</tt>“, the code will not work, because “<tt>:</tt>” has higher precedence than “<tt>-</tt>“. You need to write <tt>2:(nrow(M)-1)</tt>. This is a common error, which is avoided with the new “<tt>..</tt>” operator, which has lower precedence than the arithmetic operators. Fortunately the precedence problem with “<tt>:</tt>” is mostly just an annoyance, since it leads to the program not working at all, which is usually obvious.</p>
<p>The more insidious problem with writing the code above using “<tt>:</tt>” is that, after fixing the precedence problem, the result will work <em>except</em> when the number of rows or the number of columns in <tt>M</tt> is less than three. When <tt>M</tt> has two rows, <tt>2:(nrow(M)-1)</tt> produces a sequence of length two, consisting of 2 and 1, rather than the sequence of length zero that is needed for this code to work correctly.</p>
<p>This could be fixed by prefixing the code segment with</p>
<pre>        if (nrow(M)>2 &amp;&amp; ncol(M)>2)
</pre>
<p>But this requires the programmer to realize that there is a problem, and to not be lazy (with the excuse that they don’t intend to ever use the code with small matrices). And of course the problems with “<tt>:</tt>” cannot in general be solved with a single check like this.</p>
<p>Alternatively, one could write the program as follows:</p>
<pre>          for (i in 1+seq_len(nrow(M)-2))
              for (j in 1+seq_len(ncol(M)-2))
                  M[i,j] &lt;- 0
</pre>
<p>I hope readers will agree that this is not an ideal solution.</p>
<p>Now let’s consider the problems with R dropping dimensions from matrices (and higher-dimensional arrays). Some of these stem from R usually not distinguishing a scalar from a vector of length one. Fortunately, R actually can distinguish these, since a vector can have a <tt>dim</tt> attribute that explicitly states that it is a one-dimensional array. Such one-dimensional arrays are presently uncommon, but are easily created — if <tt>v</tt> is any vector, <tt>array(v)</tt> will be a one-dimensional array with the same contents. (Note that it will print like a plain vector, though <tt>dim(array(v))</tt> will show the difference.)</p>
<p>So, the first change in pqR to address the dimension dropping problem is to not drop a dimension of size one if its subscript is a one-dimensional array (excluding logical arrays, or when <tt>drop=TRUE</tt> is stated explicitly). Here’s an example of how this now works in pqR:</p>
<pre>    > M &lt;- matrix(1:12,3,4)
    > M
         [,1] [,2] [,3] [,4]
    [1,]    1    4    7   10
    [2,]    2    5    8   11
    [3,]    3    6    9   12
    > r &lt;- c(1,3)
    > c &lt;- c(2,4)
    > M[r,c]
         [,1] [,2]
    [1,]    4   10
    [2,]    6   12
    > c &lt;- 3
    > M[r,c]
    [1] 7 9
    > M[array(r),array(c)]
         [,1]
    [1,]    7
    [2,]    9
</pre>
<p>The final command above is the one which now acts differently, not dropping the dimensions even though there is only one column, since <tt>array(c)</tt> is an explicit one-dimensional vector. The use of <tt>array(r)</tt> similarly guards against only one row being selected, though that has no effect above, where <tt>r</tt> is of length two.</p>
<p>In this situation, the same result could be obtained with similar ease using <tt>M[r,c,drop=FALSE]</tt>. But <tt>drop=FALSE</tt> applies to every dimension, which is not always what is needed for higher-dimensional arrays. For example, in pqR, if <tt>A</tt> is a three-dimensional array, <tt>A[array(u),1,array(v)]</tt> will now select the slice of <tt>A</tt> with second subscript 1, and always return a matrix, even if <tt>u</tt> or <tt>v</tt> happened to have length one. There is no other convenient way of doing this that I know of.</p>
<p>The power of this feature becomes much greater when combined with the new “<tt>..</tt>” operator, which is defined to return a sequence that is a one-dimensional array, rather than a plain vector. Here’s how this works when continuing the example above:</p>
<pre>    > n &lt;- 2
    > m &lt;- 3
    > M[1..n,1..m]
         [,1] [,2] [,3]
    [1,]    1    4    7
    [2,]    2    5    8
    > m &lt;- 1
    > M[1..n,1..m]
         [,1]
    [1,]    1
    [2,]    2
    > n &lt;- 0
    > M[1..n,1..m]
         [,1]
    >
</pre>
<p>Note how <tt>M[1..n,1..m]</tt> is guaranteed to return a matrix, even if <tt>n</tt> or <tt>m</tt> is one. A matrix with zero rows or columns is also returned when appropriate, due to the “<tt>..</tt>” operator being able to produce a zero-length vector. To get the same effect without the “<tt>..</tt>” operator, one would need to write</p>
<pre>    M [seq_len(n), seq_len(m), drop=FALSE]
</pre>
<p>It gets worse if you want to extract a subset that doesn’t start with the first row and first column — the simplest equivalent of <tt>M[a..b,x..y]</tt> seems to be</p>
<pre>    M [a-1+seq_len(b-a+1), x-1+seq_len(y-x+1), drop=FALSE]
</pre>
<p>I suspect that not many R programmers have been writing code like this, which means that a lot of R programs don’t quite work correctly. Of course, the solution is not to berate these programmers for being lazy, but instead to make it easy to write correct code.</p>
<p>Dimensions can also get dropped inappropriately when an empty subscript is used to select all the rows or all the columns of a matrix. If this dimension happens to be of size one, R will reduce the result to a plain vector. Of course, this issue can be combined with the issues above — for example, <tt>M[1:n,]</tt> will fail to do what is likely intended if <tt>n</tt> is zero, or if <tt>n</tt> is one, or if <tt>M</tt> has only one column.</p>
<p>To solve this problem, pqR now allows “missing” arguments to be specified with an underscore, rather than by leaving the argument empty. The subscripting operator will not drop a dimension with an underscore subscript (unless <tt>drop=TRUE</tt> is specified explicitly). With this extension, along with “<tt>..</tt>“, one can rewrite <tt>M[1:n,]</tt> as <tt>M[1..n,_]</tt>, which will always do the right thing.</p>
<p>Note that it is unfortunately probably not feasible to just never drop a dimension with a missing argument, since there is likely too much existing code that relies on the current behaviour (though there is probably even more code where the existing behaviour produces bugs). Hence the creation of a new way to specify a missing argument. A more explicit “missing” indicator may be desirable anyway, as it seems more readable, and less error-prone, than nothing at all.</p>
<p>It may also be infeasible to extend the rule of not dropping dimensions indexed by one-dimensional arrays to logical subscripts — when <tt>a</tt> and <tt>b</tt> are one-dimensional arrays, <tt>M[a==0,b==0]</tt> may be intended to select a single element of <tt>M</tt>, not to return a 1×1 matrix — though one-dimensional arrays are rare enough at present that maybe one could get away with this.</p>
<p>The new “<tt>..</tt>” operator does break some existing code. In order that “<tt>..</tt>” can conveniently be used without always putting spaces around it, pqR now prohibits names from containing consecutive dots, except at the beginning or the end. So <tt>i..j</tt> is no longer a valid name (unless quoted with backticks), although <tt>..i..</tt> is still valid (but not recommended). With this restriction, most uses of the “<tt>..</tt>” operator are unambiguous, though there are exceptions, such as <tt>i..(x+y)</tt>, which is a call of the function <tt>i..</tt>, and <tt>i..-j</tt>, which computes <tt>i..</tt> minus <tt>j</tt>. There would be no ambiguities at all if consecutive dots were allowed only at the beginning of names, but unfortunately the ggplot2 package uses names like <tt>..count..</tt> in its API (not just internally).</p>
<p>Also, <tt>..</tt> is now a reserved word. This is not actually necessary to avoid ambiguity, but not making it reserved seems error-prone, since many typos would be valid syntax, and fetching from <tt>..</tt> would not even be a run-time error, since it is defined as a primitive. A number of CRAN packages use <tt>..</tt> as a name, but almost all such uses are typos, with <tt>...</tt> being what was intended (many such uses are copied from an example with a typo in <tt>help(make.rgb)</tt>).</p>
<p>To accommodate packages with incompatible uses of “<tt>..</tt>“, there is an option to disabling parsing of “<tt>..</tt>” as an operator, allowing packages written without using this new extensions to still be installed.</p>
<p>The new pqR also has other new features, including a new version of the “for” statement. Implementation of these new language features is made possible by the new parser that was introduced in pqR-2015-09-14, which has other advantages as well. I plan to write blog posts on these topics soon.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/radfordneal.wordpress.com/2392/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/radfordneal.wordpress.com/2392/" border="0"></a> <img height="1" alt="" width="1" src="https://pixel.wp.com/b.gif?host=radfordneal.wordpress.com&blog=4390751&%23038;post=2392&%23038;subd=radfordneal&%23038;ref=&%23038;feed=1" border="0"></p>
<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/fixing-rs-design-flaws-in-a-new-version-of-pqr/"></div></div><div><a data-url="http://www.r-bloggers.com/fixing-rs-design-flaws-in-a-new-version-of-pqr/" data-counturl="http://www.r-bloggers.com/fixing-rs-design-flaws-in-a-new-version-of-pqr/" data-count="vertical" data-text="Fixing R’s design flaws in a new version of pqR" 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="https://radfordneal.wordpress.com/2016/06/25/fixing-rs-design-flaws-in-a-new-version-of-pqr/"> R Programming – Radford Neal's blog</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/hdLhy6285_w"></a></div></td></tr></tbody></table>