1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="generator" content="litedown 0.8">
<title>Pipe-friendly Arithmetic Helpers in doBy</title>
<style type="text/css">
body {
font-family: sans-serif;
max-width: 800px;
margin: auto;
padding: 1em;
line-height: 1.5;
print-color-adjust: exact;
-webkit-print-color-adjust: exact;
}
body, .abstract, code, .footnotes, footer, #refs, .caption { font-size: .9em; }
li li { font-size: .95em; }
ul:has(li > input[type="checkbox"]) { list-style: none; padding-left: 1em; }
*, :before, :after { box-sizing: border-box; }
a { color: steelblue; }
pre, img { max-width: 100%; }
pre { white-space: pre-wrap; word-break: break-word; }
pre code { display: block; padding: 1em; overflow-x: auto; }
code { font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace; }
:not(pre, th) > code, code[class], div > .caption { background: #f8f8f8; }
pre > code:is(:not([class]), .language-plain, .language-none, .plain), .box, .figure, .table { background: inherit; border: 1px solid #eee; }
pre > code {
&.message { border-color: #9eeaf9; }
&.warning { background: #fff3cd; border-color: #fff3cd; }
&.error { background: #f8d7da; border-color: #f8d7da; }
}
.fenced-chunk { border-left: 1px solid #666; }
.code-fence {
opacity: .4;
border: 1px dashed #666;
border-left: 2px solid;
&:hover { opacity: inherit; }
}
.box, .figure, .table, table { margin: 1em auto; }
div > .caption { padding: 1px 1em; }
.figure { p:has(img, svg), pre:has(svg) { text-align: center; } }
.flex-col { display: flex; justify-content: space-between; }
table {
&:only-child:not(.table > *) { margin: auto; }
th, td { padding: 5px; font-variant-numeric: tabular-nums; }
thead, tfoot, tr:nth-child(even) { background: whitesmoke; }
thead th { border-bottom: 1px solid #ddd; }
&:not(.datatable-table) {
border-top: 1px solid #666;
border-bottom: 1px solid #666;
}
}
blockquote {
color: #666;
margin: 0;
padding: 1px 1em;
border-left: .5em solid #eee;
}
hr, .footnotes::before { border: 1px dashed #ddd; }
.frontmatter { text-align: center; }
#TOC {
a { text-decoration: none; }
ul { list-style: none; padding-left: 1em; }
& > ul { padding: 0; }
ul ul { border-left: 1px solid lightsteelblue; }
}
.body h2 { border-bottom: 1px solid #666; }
.body .appendix, .appendix ~ h2 { border-bottom-style: dashed; }
.main-number::after { content: "."; }
span[class^="ref-number-"] { font-weight: bold; }
.ref-number-fig::after, .ref-number-tab::after { content: ":"; }
.cross-ref-chp::before { content: "Chapter "; }
.cross-ref-sec::before { content: "Section "; }
.cross-ref-fig::before, .ref-number-fig::before { content: "Figure "; }
.cross-ref-tab::before, .ref-number-tab::before { content: "Table "; }
.cross-ref-eqn::before, .MathJax_ref:has(mjx-mtext > mjx-c + mjx-c)::before { content: "Equation "; }
.abstract, #refs {
&::before { display: block; margin: 1em auto; font-weight: bold; }
}
.abstract::before { content: "Abstract"; text-align: center; }
#refs::before { content: "Bibliography"; font-size: 1.5em; }
.ref-paren-open::before { content: "("; }
.ref-paren-close::after { content: ")"; }
.ref-semicolon::after { content: "; "; }
.ref-and::after { content: " and "; }
.ref-et-al::after { content: " et al."; font-style: italic; }
.footnote-ref a {
&::before { content: "["; }
&::after { content: "]"; }
}
section.footnotes {
margin-top: 2em;
&::before { content: ""; display: block; max-width: 20em; }
}
.fade {
background: repeating-linear-gradient(135deg, white, white 30px, #ddd 32px, #ddd 32px);
opacity: 0.6;
}
@media print {
body { max-width: 100%; }
tr, img { break-inside: avoid; }
}
@media only screen and (min-width: 992px) {
body:not(.pagesjs) pre:has(.line-numbers):not(:hover) { white-space: pre; }
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xiee/utils@1.14.17/css/prism-xcode.min.css">
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js" defer></script>
</head>
<body>
<div class="frontmatter">
<div class="title"><h1>Pipe-friendly Arithmetic Helpers in doBy</h1></div>
<div class="author"><h2>Søren Højsgaard</h2></div>
</div>
<div class="body">
<p>The <strong>doBy</strong> package includes a suite of simple, <em>pipe-friendly</em> arithmetic
helper functions that make common transformations clearer and more expressive
when using the native R pipe (<code>|></code>).</p>
<p>These helpers let you write transformations in readable pipelines
without nested or hard-to-read expressions. All functions are
vectorized and work with scalars or numeric vectors.</p>
<hr />
<h2 id="sec:available-functions">Available functions</h2>
<ul>
<li><code>add()</code>: add a constant</li>
<li><code>subtract()</code>: subtract a constant</li>
<li><code>mult()</code>: multiply by a constant</li>
<li><code>divide()</code>: divide by a constant</li>
<li><code>reciprocal()</code>: compute 1/x</li>
<li><code>pow()</code>: raise to a power</li>
</ul>
<hr />
<h2 id="sec:load-doby">Load doBy</h2>
<pre><code class="language-r">library(doBy)
x <- c(1, 2, 3)
# Addition
x |> add(5)
# [1] 6 7 8
# Subtraction
x |> subtract(1)
# [1] 0 1 2
# Multiplication
x |> mult(10)
# [1] 10 20 30
# Division
x |> divide(2)
# [1] 0.5 1.0 1.5
# Reciprocal
x |> reciprocal()
# [1] 1.0000000 0.5000000 0.3333333
# Power
x |> pow(2)
# [1] 1 4 9
x |>
mult(2) |>
add(3) |>
reciprocal()
</code></pre>
</div>
</body>
</html>
|