<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/errors-0-0-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.enchufa2.es/archives/errors-0-0-1.html"> R – Enchufa2</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In physics, engineering and other disciplines, a single number, a bare quantity, is useless, meaningless. When you measure something, your results will be within the precision of your equipment, and then you need to propagate those errors up to whatever final indirect measure you are interested in. Finally, you need to express it properly. For instance, the elementary charge:</p>
<pre><code><span>library</span><span>(</span><span>errors</span><span>)</span>

<span>e</span> <span>&lt;-</span> <span>set_errors</span><span>(</span><span>1.6021766208e-19</span>, <span>0.0000000098e-19</span><span>)</span>
<span>print</span><span>(</span><span>e</span>, <span>digits</span><span>=</span><span>2</span>, <span>notation</span><span>=</span><span>&quot;plus-minus&quot;</span><span>)</span></code></pre>
<pre><code>## (1.6021766208 +/- 0.0000000098)e-19</code></pre>
<pre><code><span>print</span><span>(</span><span>e</span>, <span>digits</span><span>=</span><span>2</span>, <span>notation</span><span>=</span><span>&quot;parenthesis&quot;</span><span>)</span></code></pre>
<pre><code>## 1.6021766208(98)e-19</code></pre>
<p>Propagation of uncertainty (or propagation of error) is easy, but too labourious. Lately, for various reasons, I have been growing sick of this task, so I decided to write this small package. In a nutshell, the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=errors"><strong>errors</strong></a> package, which is already on CRAN, provides support for painless automatic error propagation in numerical operations and pretty printing.</p>
<p>With <strong>errors</strong>, you can add errors to your numeric vectors:</p>
<pre><code><span>x</span> <span>&lt;-</span> <span>1</span><span>:</span><span>10</span>
<span>errors</span><span>(</span><span>x</span><span>)</span> <span>&lt;-</span> <span>0.1</span>
<span>x</span></code></pre>
<pre><code>## errors: 0.1 0.1 0.1 0.1 0.1 ...
##  [1]  1  2  3  4  5  6  7  8  9 10</code></pre>
<pre><code><span>errors</span><span>(</span><span>x</span><span>)</span></code></pre>
<pre><code>##  [1] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1</code></pre>
<pre><code><span>errors</span><span>(</span><span>x</span><span>)</span> <span>&lt;-</span> <span>seq</span><span>(</span><span>0.1</span>, <span>1</span>, <span>0.1</span><span>)</span>
<span>errors</span><span>(</span><span>x</span><span>)</span></code></pre>
<pre><code>##  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0</code></pre>
<p>The <code>set_errors()</code> method is a pipe-friendly version of the above:</p>
<pre><code><span>(</span><span>x</span> <span>&lt;-</span> <span>set_errors</span><span>(</span><span>1</span><span>:</span><span>10</span>, <span>seq</span><span>(</span><span>0.1</span>, <span>1</span>, <span>0.1</span><span>)</span><span>)</span><span>)</span></code></pre>
<pre><code>## errors: 0.1 0.2 0.3 0.4 0.5 ...
##  [1]  1  2  3  4  5  6  7  8  9 10</code></pre>
<p>Then, you simply work with your quantities without having to worry about errors anymore:</p>
<pre><code><span>df</span> <span>&lt;-</span> <span>as.data.frame</span><span>(</span><span>x</span><span>)</span>

<span>(</span><span>df</span><span>$</span><span>`3x`</span> <span>&lt;-</span> <span>3</span><span>*</span><span>x</span><span>)</span></code></pre>
<pre><code>## errors: 0.3 0.6 0.9 1.2 1.5 ...
##  [1]  3  6  9 12 15 18 21 24 27 30</code></pre>
<pre><code><span>(</span><span>df</span><span>$</span><span>`x^2`</span> <span>&lt;-</span> <span>x</span><span>^</span><span>2</span><span>)</span></code></pre>
<pre><code>## errors: 0.2 0.8 1.8 3.2 5 ...
##  [1]   1   4   9  16  25  36  49  64  81 100</code></pre>
<pre><code><span>(</span><span>df</span><span>$</span><span>`sin(x)`</span> <span>&lt;-</span> <span>sin</span><span>(</span><span>x</span><span>)</span><span>)</span></code></pre>
<pre><code>## errors: 0.054030230586814 0.0832293673094285 0.296997748980134 0.261457448345445 0.141831092731613 ...
##  [1]  0.8414710  0.9092974  0.1411200 -0.7568025 -0.9589243 -0.2794155
##  [7]  0.6569866  0.9893582  0.4121185 -0.5440211</code></pre>
<pre><code><span>(</span><span>df</span><span>$</span><span>`cumsum(x)`</span> <span>&lt;-</span> <span>cumsum</span><span>(</span><span>x</span><span>)</span><span>)</span></code></pre>
<pre><code>## errors: 0.1 0.223606797749979 0.374165738677394 0.547722557505166 0.741619848709566 ...
##  [1]  1  3  6 10 15 21 28 36 45 55</code></pre>
<p>Finally, you probably need to report your data in a consistent way. No problem: just print your table.</p>
<pre><code><span>df</span></code></pre>
<pre><code>##         x     3x     x^2  sin(x) cumsum(x)
## 1  1.0(1) 3.0(3)  1.0(2) 0.84(5)    1.0(1)
## 2  2.0(2) 6.0(6)  4.0(8) 0.91(8)    3.0(2)
## 3  3.0(3) 9.0(9)    9(2)  0.1(3)    6.0(4)
## 4  4.0(4)  12(1)   16(3) -0.8(3)   10.0(5)
## 5  5.0(5)  15(2)   25(5) -1.0(1)   15.0(7)
## 6  6.0(6)  18(2)   36(7) -0.3(6)  21.0(10)
## 7  7.0(7)  21(2)  49(10)  0.7(5)     28(1)
## 8  8.0(8)  24(2)  60(10)  1.0(1)     36(1)
## 9  9.0(9)  27(3)  80(20)  0.4(8)     45(2)
## 10  10(1)  30(3) 100(20) -0.5(8)     55(2)</code></pre>
<p>By default, <strong>errors</strong> uses <em>parenthesis</em> notation (which is more compact) and a single significant digit for errors. If you prefer the <em>plus-minus</em> notation or you need more significant digits, just pass the <code>notation</code> and <code>digits</code> arguments to the <code>print()</code> method, as in the first example, or set them as global options with <code>options(errors.notation=&quot;plus-minus&quot;, errors.digits=2)</code>.</p>
<p>The inner workings of this package have been inspired by Edzer Pebesma and his excellent <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=units"><code>units</code></a> package. As a next step, I envision numeric vectors with errors and units for R. Thus, I publicly invite Edzer to collaborate with me in making our packages work together.</p>
<p><em>Article originally published in Enchufa2.es: <a target="_blank" rel="nofollow" href="https://www.enchufa2.es/archives/errors-0-0-1.html">errors 0.0.1</a></em>.</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.enchufa2.es/archives/errors-0-0-1.html"> R – Enchufa2</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/e_8kJwVPf7k"></a></div>