| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
|
| 14 |
<meta name="date" content="2025-05-22" /> |
| 15 |
|
| 16 |
<title>Unpacking Assignment</title> |
| 17 |
|
| 18 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 19 |
// be compatible with the behavior of Pandoc < 2.8). |
| 20 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 21 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 22 |
var i, h, a; |
| 23 |
for (i = 0; i < hs.length; i++) { |
| 24 |
h = hs[i]; |
| 25 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 26 |
a = h.attributes; |
| 27 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 28 |
} |
| 29 |
}); |
| 30 |
</script> |
| 31 |
|
| 32 |
<style type="text/css"> |
| 33 |
code{white-space: pre-wrap;} |
| 34 |
span.smallcaps{font-variant: small-caps;} |
| 35 |
span.underline{text-decoration: underline;} |
| 36 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 37 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 38 |
ul.task-list{list-style: none;} |
| 39 |
</style> |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
<style type="text/css"> |
| 44 |
code { |
| 45 |
white-space: pre; |
| 46 |
} |
| 47 |
.sourceCode { |
| 48 |
overflow: visible; |
| 49 |
} |
| 50 |
</style> |
| 51 |
<style type="text/css" data-origin="pandoc"> |
| 52 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 53 |
pre > code.sourceCode > span { line-height: 1.25; } |
| 54 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 55 |
.sourceCode { overflow: visible; } |
| 56 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 57 |
div.sourceCode { margin: 1em 0; } |
| 58 |
pre.sourceCode { margin: 0; } |
| 59 |
@media screen { |
| 60 |
div.sourceCode { overflow: auto; } |
| 61 |
} |
| 62 |
@media print { |
| 63 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 64 |
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; } |
| 65 |
} |
| 66 |
pre.numberSource code |
| 67 |
{ counter-reset: source-line 0; } |
| 68 |
pre.numberSource code > span |
| 69 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 70 |
pre.numberSource code > span > a:first-child::before |
| 71 |
{ content: counter(source-line); |
| 72 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 73 |
border: none; display: inline-block; |
| 74 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 75 |
-khtml-user-select: none; -moz-user-select: none; |
| 76 |
-ms-user-select: none; user-select: none; |
| 77 |
padding: 0 4px; width: 4em; |
| 78 |
color: #aaaaaa; |
| 79 |
} |
| 80 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 81 |
div.sourceCode |
| 82 |
{ } |
| 83 |
@media screen { |
| 84 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 85 |
} |
| 86 |
code span.al { color: #ff0000; font-weight: bold; } |
| 87 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 88 |
code span.at { color: #7d9029; } |
| 89 |
code span.bn { color: #40a070; } |
| 90 |
code span.bu { color: #008000; } |
| 91 |
code span.cf { color: #007020; font-weight: bold; } |
| 92 |
code span.ch { color: #4070a0; } |
| 93 |
code span.cn { color: #880000; } |
| 94 |
code span.co { color: #60a0b0; font-style: italic; } |
| 95 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 96 |
code span.do { color: #ba2121; font-style: italic; } |
| 97 |
code span.dt { color: #902000; } |
| 98 |
code span.dv { color: #40a070; } |
| 99 |
code span.er { color: #ff0000; font-weight: bold; } |
| 100 |
code span.ex { } |
| 101 |
code span.fl { color: #40a070; } |
| 102 |
code span.fu { color: #06287e; } |
| 103 |
code span.im { color: #008000; font-weight: bold; } |
| 104 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 105 |
code span.kw { color: #007020; font-weight: bold; } |
| 106 |
code span.op { color: #666666; } |
| 107 |
code span.ot { color: #007020; } |
| 108 |
code span.pp { color: #bc7a00; } |
| 109 |
code span.sc { color: #4070a0; } |
| 110 |
code span.ss { color: #bb6688; } |
| 111 |
code span.st { color: #4070a0; } |
| 112 |
code span.va { color: #19177c; } |
| 113 |
code span.vs { color: #4070a0; } |
| 114 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 115 |
</style> |
| 116 |
<script> |
| 117 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 118 |
(function() { |
| 119 |
var sheets = document.styleSheets; |
| 120 |
for (var i = 0; i < sheets.length; i++) { |
| 121 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 122 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 123 |
var j = 0; |
| 124 |
while (j < rules.length) { |
| 125 |
var rule = rules[j]; |
| 126 |
// check if there is a div.sourceCode rule |
| 127 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 128 |
j++; |
| 129 |
continue; |
| 130 |
} |
| 131 |
var style = rule.style.cssText; |
| 132 |
// check if color or background-color is set |
| 133 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 134 |
j++; |
| 135 |
continue; |
| 136 |
} |
| 137 |
// replace div.sourceCode by a pre.sourceCode rule |
| 138 |
sheets[i].deleteRule(j); |
| 139 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 140 |
} |
| 141 |
} |
| 142 |
})(); |
| 143 |
</script> |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
<style type="text/css">body { |
| 149 |
background-color: #fff; |
| 150 |
margin: 1em auto; |
| 151 |
max-width: 700px; |
| 152 |
overflow: visible; |
| 153 |
padding-left: 2em; |
| 154 |
padding-right: 2em; |
| 155 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 156 |
font-size: 14px; |
| 157 |
line-height: 1.35; |
| 158 |
} |
| 159 |
#TOC { |
| 160 |
clear: both; |
| 161 |
margin: 0 0 10px 10px; |
| 162 |
padding: 4px; |
| 163 |
width: 400px; |
| 164 |
border: 1px solid #CCCCCC; |
| 165 |
border-radius: 5px; |
| 166 |
background-color: #f6f6f6; |
| 167 |
font-size: 13px; |
| 168 |
line-height: 1.3; |
| 169 |
} |
| 170 |
#TOC .toctitle { |
| 171 |
font-weight: bold; |
| 172 |
font-size: 15px; |
| 173 |
margin-left: 5px; |
| 174 |
} |
| 175 |
#TOC ul { |
| 176 |
padding-left: 40px; |
| 177 |
margin-left: -1.5em; |
| 178 |
margin-top: 5px; |
| 179 |
margin-bottom: 5px; |
| 180 |
} |
| 181 |
#TOC ul ul { |
| 182 |
margin-left: -2em; |
| 183 |
} |
| 184 |
#TOC li { |
| 185 |
line-height: 16px; |
| 186 |
} |
| 187 |
table { |
| 188 |
margin: 1em auto; |
| 189 |
border-width: 1px; |
| 190 |
border-color: #DDDDDD; |
| 191 |
border-style: outset; |
| 192 |
border-collapse: collapse; |
| 193 |
} |
| 194 |
table th { |
| 195 |
border-width: 2px; |
| 196 |
padding: 5px; |
| 197 |
border-style: inset; |
| 198 |
} |
| 199 |
table td { |
| 200 |
border-width: 1px; |
| 201 |
border-style: inset; |
| 202 |
line-height: 18px; |
| 203 |
padding: 5px 5px; |
| 204 |
} |
| 205 |
table, table th, table td { |
| 206 |
border-left-style: none; |
| 207 |
border-right-style: none; |
| 208 |
} |
| 209 |
table thead, table tr.even { |
| 210 |
background-color: #f7f7f7; |
| 211 |
} |
| 212 |
p { |
| 213 |
margin: 0.5em 0; |
| 214 |
} |
| 215 |
blockquote { |
| 216 |
background-color: #f6f6f6; |
| 217 |
padding: 0.25em 0.75em; |
| 218 |
} |
| 219 |
hr { |
| 220 |
border-style: solid; |
| 221 |
border: none; |
| 222 |
border-top: 1px solid #777; |
| 223 |
margin: 28px 0; |
| 224 |
} |
| 225 |
dl { |
| 226 |
margin-left: 0; |
| 227 |
} |
| 228 |
dl dd { |
| 229 |
margin-bottom: 13px; |
| 230 |
margin-left: 13px; |
| 231 |
} |
| 232 |
dl dt { |
| 233 |
font-weight: bold; |
| 234 |
} |
| 235 |
ul { |
| 236 |
margin-top: 0; |
| 237 |
} |
| 238 |
ul li { |
| 239 |
list-style: circle outside; |
| 240 |
} |
| 241 |
ul ul { |
| 242 |
margin-bottom: 0; |
| 243 |
} |
| 244 |
pre, code { |
| 245 |
background-color: #f7f7f7; |
| 246 |
border-radius: 3px; |
| 247 |
color: #333; |
| 248 |
white-space: pre-wrap; |
| 249 |
} |
| 250 |
pre { |
| 251 |
border-radius: 3px; |
| 252 |
margin: 5px 0px 10px 0px; |
| 253 |
padding: 10px; |
| 254 |
} |
| 255 |
pre:not([class]) { |
| 256 |
background-color: #f7f7f7; |
| 257 |
} |
| 258 |
code { |
| 259 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 260 |
font-size: 85%; |
| 261 |
} |
| 262 |
p > code, li > code { |
| 263 |
padding: 2px 0px; |
| 264 |
} |
| 265 |
div.figure { |
| 266 |
text-align: center; |
| 267 |
} |
| 268 |
img { |
| 269 |
background-color: #FFFFFF; |
| 270 |
padding: 2px; |
| 271 |
border: 1px solid #DDDDDD; |
| 272 |
border-radius: 3px; |
| 273 |
border: 1px solid #CCCCCC; |
| 274 |
margin: 0 5px; |
| 275 |
} |
| 276 |
h1 { |
| 277 |
margin-top: 0; |
| 278 |
font-size: 35px; |
| 279 |
line-height: 40px; |
| 280 |
} |
| 281 |
h2 { |
| 282 |
border-bottom: 4px solid #f7f7f7; |
| 283 |
padding-top: 10px; |
| 284 |
padding-bottom: 2px; |
| 285 |
font-size: 145%; |
| 286 |
} |
| 287 |
h3 { |
| 288 |
border-bottom: 2px solid #f7f7f7; |
| 289 |
padding-top: 10px; |
| 290 |
font-size: 120%; |
| 291 |
} |
| 292 |
h4 { |
| 293 |
border-bottom: 1px solid #f7f7f7; |
| 294 |
margin-left: 8px; |
| 295 |
font-size: 105%; |
| 296 |
} |
| 297 |
h5, h6 { |
| 298 |
border-bottom: 1px solid #ccc; |
| 299 |
font-size: 105%; |
| 300 |
} |
| 301 |
a { |
| 302 |
color: #0033dd; |
| 303 |
text-decoration: none; |
| 304 |
} |
| 305 |
a:hover { |
| 306 |
color: #6666ff; } |
| 307 |
a:visited { |
| 308 |
color: #800080; } |
| 309 |
a:visited:hover { |
| 310 |
color: #BB00BB; } |
| 311 |
a[href^="http:"] { |
| 312 |
text-decoration: underline; } |
| 313 |
a[href^="https:"] { |
| 314 |
text-decoration: underline; } |
| 315 |
|
| 316 |
code > span.kw { color: #555; font-weight: bold; } |
| 317 |
code > span.dt { color: #902000; } |
| 318 |
code > span.dv { color: #40a070; } |
| 319 |
code > span.bn { color: #d14; } |
| 320 |
code > span.fl { color: #d14; } |
| 321 |
code > span.ch { color: #d14; } |
| 322 |
code > span.st { color: #d14; } |
| 323 |
code > span.co { color: #888888; font-style: italic; } |
| 324 |
code > span.ot { color: #007020; } |
| 325 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 326 |
code > span.fu { color: #900; font-weight: bold; } |
| 327 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 328 |
</style> |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
</head> |
| 334 |
|
| 335 |
<body> |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
<h1 class="title toc-ignore">Unpacking Assignment</h1> |
| 341 |
<h4 class="date">2025-05-22</h4> |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
<div id="getting-started" class="section level2"> |
| 346 |
<h2>Getting Started</h2> |
| 347 |
<p>The <em>zeallot</em> package defines an operator for <em>unpacking |
| 348 |
assignment</em>, sometimes called <em>parallel assignment</em> or |
| 349 |
<em>destructuring assignment</em> in other programming languages. The |
| 350 |
operator is written as <code>%<-%</code> and used like this.</p> |
| 351 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">c</span>(lat, lng) <span class="sc">%<-%</span> <span class="fu">list</span>(<span class="fl">38.061944</span>, <span class="sc">-</span><span class="fl">122.643889</span>)</span></code></pre></div> |
| 352 |
<p>The result is that the list is unpacked into its elements, and the |
| 353 |
elements are assigned to <code>lat</code> and <code>lng</code>.</p> |
| 354 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>lat</span> |
| 355 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="co">#> [1] 38.06194</span></span> |
| 356 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a>lng</span> |
| 357 |
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co">#> [1] -122.6439</span></span></code></pre></div> |
| 358 |
<p>You can also unpack the elements of a vector.</p> |
| 359 |
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">c</span>(lat, lng) <span class="sc">%<-%</span> <span class="fu">c</span>(<span class="fl">38.061944</span>, <span class="sc">-</span><span class="fl">122.643889</span>)</span> |
| 360 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>lat</span> |
| 361 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#> [1] 38.06194</span></span> |
| 362 |
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a>lng</span> |
| 363 |
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#> [1] -122.6439</span></span></code></pre></div> |
| 364 |
<p>You can unpack much longer structures, too, of course, such as the |
| 365 |
6-part summary of a vector.</p> |
| 366 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="fu">c</span>(min_wt, q1_wt, med_wt, mean_wt, q3_wt, max_wt) <span class="sc">%<-%</span> <span class="fu">summary</span>(mtcars<span class="sc">$</span>wt)</span> |
| 367 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a>min_wt</span> |
| 368 |
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#> [1] 1.513</span></span> |
| 369 |
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a>q1_wt</span> |
| 370 |
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#> [1] 2.58125</span></span> |
| 371 |
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a>med_wt</span> |
| 372 |
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#> [1] 3.325</span></span> |
| 373 |
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a>mean_wt</span> |
| 374 |
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#> [1] 3.21725</span></span> |
| 375 |
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a>q3_wt</span> |
| 376 |
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#> [1] 3.61</span></span> |
| 377 |
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a>max_wt</span> |
| 378 |
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#> [1] 5.424</span></span></code></pre></div> |
| 379 |
<p>If the left-hand side and right-hand sides do not match, an error is |
| 380 |
raised. This guards against missing or unexpected values.</p> |
| 381 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="fu">c</span>(stg1, stg2, stg3) <span class="sc">%<-%</span> <span class="fu">list</span>(<span class="st">"Moe"</span>, <span class="st">"Donald"</span>)</span> |
| 382 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="co">#> Error in c(stg1, stg2, stg3) %<-% list("Moe", "Donald"): missing value for variable `stg3`</span></span></code></pre></div> |
| 383 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="fu">c</span>(stg1, stg2, stg3) <span class="sc">%<-%</span> <span class="fu">list</span>(<span class="st">"Moe"</span>, <span class="st">"Larry"</span>, <span class="st">"Curley"</span>, <span class="st">"Donald"</span>)</span></code></pre></div> |
| 384 |
<div id="unpacking-a-returned-value" class="section level3"> |
| 385 |
<h3>Unpacking a returned value</h3> |
| 386 |
<p>A common use-case is when a function returns a list of values and you |
| 387 |
want to extract the individual values. In this example, the list of |
| 388 |
values returned by <code>coords_list()</code> is unpacked into the |
| 389 |
variables <code>lat</code> and <code>lng</code>.</p> |
| 390 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="co">#</span></span> |
| 391 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co"># A function which returns a list of 2 numeric values.</span></span> |
| 392 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co"># </span></span> |
| 393 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a>coords_list <span class="ot"><-</span> <span class="cf">function</span>() {</span> |
| 394 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a> <span class="fu">list</span>(<span class="fl">38.061944</span>, <span class="sc">-</span><span class="fl">122.643889</span>)</span> |
| 395 |
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a>}</span> |
| 396 |
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a></span> |
| 397 |
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="fu">c</span>(lat, lng) <span class="sc">%<-%</span> <span class="fu">coords_list</span>()</span> |
| 398 |
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a>lat</span> |
| 399 |
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="co">#> [1] 38.06194</span></span> |
| 400 |
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a>lng</span> |
| 401 |
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a><span class="co">#> [1] -122.6439</span></span></code></pre></div> |
| 402 |
<p>In this next example, we call a function that returns a vector.</p> |
| 403 |
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="co">#</span></span> |
| 404 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="co"># Convert cartesian coordinates to polar</span></span> |
| 405 |
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="co">#</span></span> |
| 406 |
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a>to_polar <span class="ot">=</span> <span class="cf">function</span>(x, y) {</span> |
| 407 |
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a> <span class="fu">c</span>(<span class="fu">sqrt</span>(x<span class="sc">^</span><span class="dv">2</span> <span class="sc">+</span> y<span class="sc">^</span><span class="dv">2</span>), <span class="fu">atan</span>(y <span class="sc">/</span> x))</span> |
| 408 |
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a>}</span> |
| 409 |
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a></span> |
| 410 |
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="fu">c</span>(radius, angle) <span class="sc">%<-%</span> <span class="fu">to_polar</span>(<span class="dv">12</span>, <span class="dv">5</span>)</span> |
| 411 |
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a>radius</span> |
| 412 |
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="co">#> [1] 13</span></span> |
| 413 |
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a>angle</span> |
| 414 |
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a><span class="co">#> [1] 0.3947911</span></span></code></pre></div> |
| 415 |
</div> |
| 416 |
<div id="example-intercept-and-slope-of-regression" class="section level3"> |
| 417 |
<h3>Example: Intercept and slope of regression</h3> |
| 418 |
<p>You can directly unpack the coefficients of a simple linear |
| 419 |
regression into the intercept and slope.</p> |
| 420 |
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="fu">c</span>(inter, slope) <span class="sc">%<-%</span> <span class="fu">coef</span>(<span class="fu">lm</span>(mpg <span class="sc">~</span> cyl, <span class="at">data =</span> mtcars))</span> |
| 421 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>inter</span> |
| 422 |
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="co">#> [1] 37.88458</span></span> |
| 423 |
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a>slope</span> |
| 424 |
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#> [1] -2.87579</span></span></code></pre></div> |
| 425 |
</div> |
| 426 |
<div id="example-unpacking-the-result-of-safely" class="section level3"> |
| 427 |
<h3>Example: Unpacking the result of <code>safely</code></h3> |
| 428 |
<p>The <em>purrr</em> package includes the <code>safely</code> function. |
| 429 |
It wraps a given function to create a new, “safe” version of the |
| 430 |
original function.</p> |
| 431 |
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>safe_log <span class="ot"><-</span> purrr<span class="sc">::</span><span class="fu">safely</span>(log)</span></code></pre></div> |
| 432 |
<p>The safe version returns a list of two items. The first item is the |
| 433 |
result of calling the original function, assuming no error occurred; or |
| 434 |
<code>NULL</code> if an error did occur. The second item is the error, |
| 435 |
if an error occurred; or <code>NULL</code> if no error occurred. Whether |
| 436 |
or not the original function would have thrown an error, the safe |
| 437 |
version will never throw an error.</p> |
| 438 |
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>pair <span class="ot"><-</span> <span class="fu">safe_log</span>(<span class="dv">10</span>)</span> |
| 439 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a>pair<span class="sc">$</span>result</span> |
| 440 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co">#> [1] 2.302585</span></span> |
| 441 |
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a>pair<span class="sc">$</span>error</span> |
| 442 |
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div> |
| 443 |
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>pair <span class="ot"><-</span> <span class="fu">safe_log</span>(<span class="st">"donald"</span>)</span> |
| 444 |
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a>pair<span class="sc">$</span>result</span> |
| 445 |
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co">#> NULL</span></span> |
| 446 |
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a>pair<span class="sc">$</span>error</span> |
| 447 |
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="co">#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function></span></span></code></pre></div> |
| 448 |
<p>You can tighten and clarify calls to the safe function by using |
| 449 |
<code>%<-%</code>.</p> |
| 450 |
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a><span class="fu">c</span>(res, err) <span class="sc">%<-%</span> <span class="fu">safe_log</span>(<span class="dv">10</span>)</span> |
| 451 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a>res</span> |
| 452 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="co">#> [1] 2.302585</span></span> |
| 453 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a>err</span> |
| 454 |
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div> |
| 455 |
</div> |
| 456 |
</div> |
| 457 |
<div id="unpacking-a-data-frame" class="section level2"> |
| 458 |
<h2>Unpacking a data frame</h2> |
| 459 |
<p>A data frame is simply a list of columns, so the <em>zeallot</em> |
| 460 |
assignment does what you expect. It unpacks the data frame into |
| 461 |
individual columns.</p> |
| 462 |
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">c</span>(<span class="at">mpg=</span>, <span class="at">cyl=</span>, <span class="at">dist=</span>, <span class="at">hp=</span>) <span class="sc">%<-%</span> mtcars</span> |
| 463 |
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a></span> |
| 464 |
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a>cyl</span> |
| 465 |
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4</span></span></code></pre></div> |
| 466 |
<div id="example-list-of-data-frames" class="section level3"> |
| 467 |
<h3>Example: List of data frames</h3> |
| 468 |
<p>Bear in mind, a list of data frames is still just a list. The |
| 469 |
assignment will extract the list elements (which are data frames) but |
| 470 |
not unpack the data frames themselves.</p> |
| 471 |
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="fu">c</span>(gear3, gear4, gear5) <span class="sc">%<-%</span> <span class="fu">split</span>(mtcars, <span class="sc">~</span> gear)</span> |
| 472 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a></span> |
| 473 |
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="fu">head</span>(gear3)</span> |
| 474 |
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="co">#> mpg cyl disp hp drat wt qsec vs am gear carb</span></span> |
| 475 |
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a><span class="co">#> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1</span></span> |
| 476 |
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="co">#> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2</span></span> |
| 477 |
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a><span class="co">#> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1</span></span> |
| 478 |
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a><span class="co">#> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4</span></span> |
| 479 |
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a><span class="co">#> Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3</span></span> |
| 480 |
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a><span class="co">#> Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3</span></span> |
| 481 |
<span id="cb15-11"><a href="#cb15-11" tabindex="-1"></a></span> |
| 482 |
<span id="cb15-12"><a href="#cb15-12" tabindex="-1"></a><span class="fu">head</span>(gear4)</span> |
| 483 |
<span id="cb15-13"><a href="#cb15-13" tabindex="-1"></a><span class="co">#> mpg cyl disp hp drat wt qsec vs am gear carb</span></span> |
| 484 |
<span id="cb15-14"><a href="#cb15-14" tabindex="-1"></a><span class="co">#> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4</span></span> |
| 485 |
<span id="cb15-15"><a href="#cb15-15" tabindex="-1"></a><span class="co">#> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4</span></span> |
| 486 |
<span id="cb15-16"><a href="#cb15-16" tabindex="-1"></a><span class="co">#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1</span></span> |
| 487 |
<span id="cb15-17"><a href="#cb15-17" tabindex="-1"></a><span class="co">#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2</span></span> |
| 488 |
<span id="cb15-18"><a href="#cb15-18" tabindex="-1"></a><span class="co">#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2</span></span> |
| 489 |
<span id="cb15-19"><a href="#cb15-19" tabindex="-1"></a><span class="co">#> Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4</span></span> |
| 490 |
<span id="cb15-20"><a href="#cb15-20" tabindex="-1"></a></span> |
| 491 |
<span id="cb15-21"><a href="#cb15-21" tabindex="-1"></a>gear5</span> |
| 492 |
<span id="cb15-22"><a href="#cb15-22" tabindex="-1"></a><span class="co">#> mpg cyl disp hp drat wt qsec vs am gear carb</span></span> |
| 493 |
<span id="cb15-23"><a href="#cb15-23" tabindex="-1"></a><span class="co">#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2</span></span> |
| 494 |
<span id="cb15-24"><a href="#cb15-24" tabindex="-1"></a><span class="co">#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2</span></span> |
| 495 |
<span id="cb15-25"><a href="#cb15-25" tabindex="-1"></a><span class="co">#> Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4</span></span> |
| 496 |
<span id="cb15-26"><a href="#cb15-26" tabindex="-1"></a><span class="co">#> Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6</span></span> |
| 497 |
<span id="cb15-27"><a href="#cb15-27" tabindex="-1"></a><span class="co">#> Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8</span></span></code></pre></div> |
| 498 |
<p>The <code>%<-%</code> operator assigned four data frames to four |
| 499 |
variables, leaving the data frames intact.</p> |
| 500 |
</div> |
| 501 |
</div> |
| 502 |
<div id="custom-classes" class="section level2"> |
| 503 |
<h2>Custom classes</h2> |
| 504 |
<p><em>zeallot</em> includes implementations of <code>destructure</code> |
| 505 |
for data frames and linear model summaries. However, because |
| 506 |
<code>destructure</code> is a generic function, you can define new |
| 507 |
implementations for custom classes. When defining a new implementation |
| 508 |
keep in mind the return value must be a list so the values are properly |
| 509 |
unpacked.</p> |
| 510 |
</div> |
| 511 |
<div id="trailing-values-the-everything-else-clause" class="section level2"> |
| 512 |
<h2>Trailing values: the “everything else” clause</h2> |
| 513 |
<p>In some cases, you want the first few elements of a list or vector |
| 514 |
but do not care about the trailing elements. The <code>summary.lm</code> |
| 515 |
function, for example, returns a list of 11 values, and you may want |
| 516 |
only the first few. Fortunately, there is a way to capture those first |
| 517 |
few and say “don’t worry about everything else”.</p> |
| 518 |
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>lm_mpg_cyl <span class="ot"><-</span> <span class="fu">lm</span>(mpg <span class="sc">~</span> cyl, <span class="at">data =</span> mtcars)</span> |
| 519 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a></span> |
| 520 |
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a><span class="fu">c</span>(lmc_call, lmc_terms, lmc_residuals, ..rest) <span class="sc">%<-%</span> <span class="fu">summary</span>(lm_mpg_cyl)</span> |
| 521 |
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a></span> |
| 522 |
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a>lmc_call</span> |
| 523 |
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="co">#> lm(formula = mpg ~ cyl, data = mtcars)</span></span> |
| 524 |
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a></span> |
| 525 |
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a>lmc_terms</span> |
| 526 |
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a><span class="co">#> mpg ~ cyl</span></span> |
| 527 |
<span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a><span class="co">#> attr(,"variables")</span></span> |
| 528 |
<span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a><span class="co">#> list(mpg, cyl)</span></span> |
| 529 |
<span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a><span class="co">#> attr(,"factors")</span></span> |
| 530 |
<span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a><span class="co">#> cyl</span></span> |
| 531 |
<span id="cb16-14"><a href="#cb16-14" tabindex="-1"></a><span class="co">#> mpg 0</span></span> |
| 532 |
<span id="cb16-15"><a href="#cb16-15" tabindex="-1"></a><span class="co">#> cyl 1</span></span> |
| 533 |
<span id="cb16-16"><a href="#cb16-16" tabindex="-1"></a><span class="co">#> attr(,"term.labels")</span></span> |
| 534 |
<span id="cb16-17"><a href="#cb16-17" tabindex="-1"></a><span class="co">#> [1] "cyl"</span></span> |
| 535 |
<span id="cb16-18"><a href="#cb16-18" tabindex="-1"></a><span class="co">#> attr(,"order")</span></span> |
| 536 |
<span id="cb16-19"><a href="#cb16-19" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 537 |
<span id="cb16-20"><a href="#cb16-20" tabindex="-1"></a><span class="co">#> attr(,"intercept")</span></span> |
| 538 |
<span id="cb16-21"><a href="#cb16-21" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 539 |
<span id="cb16-22"><a href="#cb16-22" tabindex="-1"></a><span class="co">#> attr(,"response")</span></span> |
| 540 |
<span id="cb16-23"><a href="#cb16-23" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 541 |
<span id="cb16-24"><a href="#cb16-24" tabindex="-1"></a><span class="co">#> attr(,".Environment")</span></span> |
| 542 |
<span id="cb16-25"><a href="#cb16-25" tabindex="-1"></a><span class="co">#> <environment: R_GlobalEnv></span></span> |
| 543 |
<span id="cb16-26"><a href="#cb16-26" tabindex="-1"></a><span class="co">#> attr(,"predvars")</span></span> |
| 544 |
<span id="cb16-27"><a href="#cb16-27" tabindex="-1"></a><span class="co">#> list(mpg, cyl)</span></span> |
| 545 |
<span id="cb16-28"><a href="#cb16-28" tabindex="-1"></a><span class="co">#> attr(,"dataClasses")</span></span> |
| 546 |
<span id="cb16-29"><a href="#cb16-29" tabindex="-1"></a><span class="co">#> mpg cyl </span></span> |
| 547 |
<span id="cb16-30"><a href="#cb16-30" tabindex="-1"></a><span class="co">#> "numeric" "numeric"</span></span> |
| 548 |
<span id="cb16-31"><a href="#cb16-31" tabindex="-1"></a></span> |
| 549 |
<span id="cb16-32"><a href="#cb16-32" tabindex="-1"></a><span class="fu">head</span>(lmc_residuals)</span> |
| 550 |
<span id="cb16-33"><a href="#cb16-33" tabindex="-1"></a><span class="co">#> Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive </span></span> |
| 551 |
<span id="cb16-34"><a href="#cb16-34" tabindex="-1"></a><span class="co">#> 0.3701643 0.3701643 -3.5814159 0.7701643 </span></span> |
| 552 |
<span id="cb16-35"><a href="#cb16-35" tabindex="-1"></a><span class="co">#> Hornet Sportabout Valiant </span></span> |
| 553 |
<span id="cb16-36"><a href="#cb16-36" tabindex="-1"></a><span class="co">#> 3.8217446 -2.5298357</span></span></code></pre></div> |
| 554 |
<p>The collector variable <code>rest</code> captures everything |
| 555 |
else.</p> |
| 556 |
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="fu">str</span>(rest)</span> |
| 557 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="co">#> List of 15</span></span> |
| 558 |
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a><span class="co">#> $ : num 37.9</span></span> |
| 559 |
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a><span class="co">#> $ : num -2.88</span></span> |
| 560 |
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="co">#> $ : num 2.07</span></span> |
| 561 |
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co">#> $ : num 0.322</span></span> |
| 562 |
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a><span class="co">#> $ : num 18.3</span></span> |
| 563 |
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a><span class="co">#> $ : num -8.92</span></span> |
| 564 |
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a><span class="co">#> $ : num 8.37e-18</span></span> |
| 565 |
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a><span class="co">#> $ : num 6.11e-10</span></span> |
| 566 |
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="co">#> $ aliased : Named logi [1:2] FALSE FALSE</span></span> |
| 567 |
<span id="cb17-12"><a href="#cb17-12" tabindex="-1"></a><span class="co">#> ..- attr(*, "names")= chr [1:2] "(Intercept)" "cyl"</span></span> |
| 568 |
<span id="cb17-13"><a href="#cb17-13" tabindex="-1"></a><span class="co">#> $ sigma : num 3.21</span></span> |
| 569 |
<span id="cb17-14"><a href="#cb17-14" tabindex="-1"></a><span class="co">#> $ df : int [1:3] 2 30 2</span></span> |
| 570 |
<span id="cb17-15"><a href="#cb17-15" tabindex="-1"></a><span class="co">#> $ r.squared : num 0.726</span></span> |
| 571 |
<span id="cb17-16"><a href="#cb17-16" tabindex="-1"></a><span class="co">#> $ adj.r.squared: num 0.717</span></span> |
| 572 |
<span id="cb17-17"><a href="#cb17-17" tabindex="-1"></a><span class="co">#> $ fstatistic : Named num [1:3] 79.6 1 30</span></span> |
| 573 |
<span id="cb17-18"><a href="#cb17-18" tabindex="-1"></a><span class="co">#> ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"</span></span> |
| 574 |
<span id="cb17-19"><a href="#cb17-19" tabindex="-1"></a><span class="co">#> $ cov.unscaled : num [1:2, 1:2] 0.4185 -0.0626 -0.0626 0.0101</span></span> |
| 575 |
<span id="cb17-20"><a href="#cb17-20" tabindex="-1"></a><span class="co">#> ..- attr(*, "dimnames")=List of 2</span></span> |
| 576 |
<span id="cb17-21"><a href="#cb17-21" tabindex="-1"></a><span class="co">#> .. ..$ : chr [1:2] "(Intercept)" "cyl"</span></span> |
| 577 |
<span id="cb17-22"><a href="#cb17-22" tabindex="-1"></a><span class="co">#> .. ..$ : chr [1:2] "(Intercept)" "cyl"</span></span></code></pre></div> |
| 578 |
<p>Because <code>..rest</code> is prefixed with <code>..</code> a |
| 579 |
variable called <code>rest</code> is created for the trailing values of |
| 580 |
the list.</p> |
| 581 |
</div> |
| 582 |
<div id="leading-values-and-middle-values" class="section level2"> |
| 583 |
<h2>Leading values and middle values</h2> |
| 584 |
<p>In addition to collecting trailing values, you can also collect |
| 585 |
initial values and assign specific remaining values.</p> |
| 586 |
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="fu">c</span>(..skip, e, f) <span class="sc">%<-%</span> <span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)</span> |
| 587 |
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a>skip</span> |
| 588 |
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 589 |
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 590 |
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a><span class="co">#> </span></span> |
| 591 |
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 592 |
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="co">#> [1] 2</span></span> |
| 593 |
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a><span class="co">#> </span></span> |
| 594 |
<span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a><span class="co">#> [[3]]</span></span> |
| 595 |
<span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a><span class="co">#> [1] 3</span></span> |
| 596 |
<span id="cb18-11"><a href="#cb18-11" tabindex="-1"></a>e</span> |
| 597 |
<span id="cb18-12"><a href="#cb18-12" tabindex="-1"></a><span class="co">#> [1] 4</span></span> |
| 598 |
<span id="cb18-13"><a href="#cb18-13" tabindex="-1"></a>f</span> |
| 599 |
<span id="cb18-14"><a href="#cb18-14" tabindex="-1"></a><span class="co">#> [1] 5</span></span></code></pre></div> |
| 600 |
<p>Or you can assign the first value, skip values, and then assign the |
| 601 |
last value.</p> |
| 602 |
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="fu">c</span>(begin, ..middle, end) <span class="sc">%<-%</span> <span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)</span> |
| 603 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a>begin</span> |
| 604 |
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 605 |
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a>middle</span> |
| 606 |
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 607 |
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a><span class="co">#> [1] 2</span></span> |
| 608 |
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a><span class="co">#> </span></span> |
| 609 |
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 610 |
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a><span class="co">#> [1] 3</span></span> |
| 611 |
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a><span class="co">#> </span></span> |
| 612 |
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a><span class="co">#> [[3]]</span></span> |
| 613 |
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a><span class="co">#> [1] 4</span></span> |
| 614 |
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a>end</span> |
| 615 |
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a><span class="co">#> [1] 5</span></span></code></pre></div> |
| 616 |
</div> |
| 617 |
<div id="skipped-values-anonymous-elements" class="section level2"> |
| 618 |
<h2>Skipped values: anonymous elements</h2> |
| 619 |
<p>You can skip one or more values using a period (<code>.</code>) |
| 620 |
instead of a variable name.</p> |
| 621 |
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a><span class="fu">c</span>(x, ., z) <span class="sc">%<-%</span> <span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>)</span> |
| 622 |
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a>x</span> |
| 623 |
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 624 |
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a>z</span> |
| 625 |
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a><span class="co">#> [1] 3</span></span></code></pre></div> |
| 626 |
<p>You can skip many values with the anonymous collector |
| 627 |
(<code>..</code>).</p> |
| 628 |
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a><span class="fu">c</span>(begin, .., end) <span class="sc">%<-%</span> <span class="fu">list</span>(<span class="st">"hello"</span>, <span class="st">"blah"</span>, <span class="fu">list</span>(<span class="st">"blah"</span>), <span class="st">"blah"</span>, <span class="st">"world!"</span>)</span> |
| 629 |
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a>begin</span> |
| 630 |
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a><span class="co">#> [1] "hello"</span></span> |
| 631 |
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a>end</span> |
| 632 |
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a><span class="co">#> [1] "world!"</span></span></code></pre></div> |
| 633 |
<p>You can mix skips and collectors together to selectively keep and |
| 634 |
discard elements.</p> |
| 635 |
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a><span class="fu">c</span>(begin, ., ..middle, end) <span class="sc">%<-%</span> <span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)</span> |
| 636 |
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a>begin</span> |
| 637 |
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 638 |
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a>middle</span> |
| 639 |
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 640 |
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a><span class="co">#> [1] 3</span></span> |
| 641 |
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a><span class="co">#> </span></span> |
| 642 |
<span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 643 |
<span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a><span class="co">#> [1] 4</span></span> |
| 644 |
<span id="cb22-10"><a href="#cb22-10" tabindex="-1"></a>end</span> |
| 645 |
<span id="cb22-11"><a href="#cb22-11" tabindex="-1"></a><span class="co">#> [1] 5</span></span></code></pre></div> |
| 646 |
</div> |
| 647 |
<div id="default-values-handle-missing-values" class="section level2"> |
| 648 |
<h2>Default values: handle missing values</h2> |
| 649 |
<p>You can specify a default value for a left-hand side variable using |
| 650 |
<code>=</code>, similar to specifying the default value of a function |
| 651 |
argument. This comes in handy when the number of elements returned by a |
| 652 |
function cannot be guaranteed. <code>tail()</code> for example may |
| 653 |
return fewer elements than asked for.</p> |
| 654 |
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a>nums <span class="ot"><-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>)</span> |
| 655 |
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="fu">c</span>(x, y) <span class="sc">%<-%</span> <span class="fu">tail</span>(nums, <span class="dv">2</span>)</span> |
| 656 |
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a>x</span> |
| 657 |
<span id="cb23-4"><a href="#cb23-4" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 658 |
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a>y</span> |
| 659 |
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="co">#> [1] 2</span></span></code></pre></div> |
| 660 |
<p>However, if we tried to get the last 3 elements an error would be |
| 661 |
raised because <code>tail(nums, 3)</code> still returns only 2 |
| 662 |
values.</p> |
| 663 |
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a><span class="fu">c</span>(x, y, z) <span class="sc">%<-%</span> <span class="fu">tail</span>(nums, <span class="dv">3</span>)</span> |
| 664 |
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="co">#> Error in c(x, y, z) %<-% tail(nums, 3): missing value for variable `z`</span></span></code></pre></div> |
| 665 |
<p>We can fix the problem by specifying a default value for |
| 666 |
<code>z</code>.</p> |
| 667 |
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a><span class="fu">c</span>(x, y, <span class="at">z =</span> <span class="cn">NULL</span>) <span class="sc">%<-%</span> <span class="fu">tail</span>(nums, <span class="dv">3</span>)</span> |
| 668 |
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a>x</span> |
| 669 |
<span id="cb25-3"><a href="#cb25-3" tabindex="-1"></a><span class="co">#> [1] 1</span></span> |
| 670 |
<span id="cb25-4"><a href="#cb25-4" tabindex="-1"></a>y</span> |
| 671 |
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a><span class="co">#> [1] 2</span></span> |
| 672 |
<span id="cb25-6"><a href="#cb25-6" tabindex="-1"></a>z</span> |
| 673 |
<span id="cb25-7"><a href="#cb25-7" tabindex="-1"></a><span class="co">#> NULL</span></span></code></pre></div> |
| 674 |
</div> |
| 675 |
<div id="swapping-values" class="section level2"> |
| 676 |
<h2>Swapping values</h2> |
| 677 |
<p>A handy trick is swapping values without the use of a temporary |
| 678 |
variable.</p> |
| 679 |
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a><span class="fu">c</span>(first, last) <span class="sc">%<-%</span> <span class="fu">c</span>(<span class="st">"Ai"</span>, <span class="st">"Genly"</span>)</span> |
| 680 |
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a>first</span> |
| 681 |
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a><span class="co">#> [1] "Ai"</span></span> |
| 682 |
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a>last</span> |
| 683 |
<span id="cb26-5"><a href="#cb26-5" tabindex="-1"></a><span class="co">#> [1] "Genly"</span></span> |
| 684 |
<span id="cb26-6"><a href="#cb26-6" tabindex="-1"></a></span> |
| 685 |
<span id="cb26-7"><a href="#cb26-7" tabindex="-1"></a><span class="fu">c</span>(first, last) <span class="sc">%<-%</span> <span class="fu">c</span>(last, first)</span> |
| 686 |
<span id="cb26-8"><a href="#cb26-8" tabindex="-1"></a>first</span> |
| 687 |
<span id="cb26-9"><a href="#cb26-9" tabindex="-1"></a><span class="co">#> [1] "Genly"</span></span> |
| 688 |
<span id="cb26-10"><a href="#cb26-10" tabindex="-1"></a>last</span> |
| 689 |
<span id="cb26-11"><a href="#cb26-11" tabindex="-1"></a><span class="co">#> [1] "Ai"</span></span></code></pre></div> |
| 690 |
<p>or</p> |
| 691 |
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a>cat <span class="ot"><-</span> <span class="st">"meow"</span></span> |
| 692 |
<span id="cb27-2"><a href="#cb27-2" tabindex="-1"></a>dog <span class="ot"><-</span> <span class="st">"bark"</span></span> |
| 693 |
<span id="cb27-3"><a href="#cb27-3" tabindex="-1"></a></span> |
| 694 |
<span id="cb27-4"><a href="#cb27-4" tabindex="-1"></a><span class="fu">c</span>(cat, dog, fish) <span class="sc">%<-%</span> <span class="fu">c</span>(dog, cat, dog)</span> |
| 695 |
<span id="cb27-5"><a href="#cb27-5" tabindex="-1"></a>cat</span> |
| 696 |
<span id="cb27-6"><a href="#cb27-6" tabindex="-1"></a><span class="co">#> [1] "bark"</span></span> |
| 697 |
<span id="cb27-7"><a href="#cb27-7" tabindex="-1"></a>dog</span> |
| 698 |
<span id="cb27-8"><a href="#cb27-8" tabindex="-1"></a><span class="co">#> [1] "meow"</span></span> |
| 699 |
<span id="cb27-9"><a href="#cb27-9" tabindex="-1"></a>fish</span> |
| 700 |
<span id="cb27-10"><a href="#cb27-10" tabindex="-1"></a><span class="co">#> [1] "bark"</span></span></code></pre></div> |
| 701 |
</div> |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
<!-- code folding --> |
| 706 |
|
| 707 |
|
| 708 |
<!-- dynamically load mathjax for compatibility with self-contained --> |
| 709 |
<script> |
| 710 |
(function () { |
| 711 |
var script = document.createElement("script"); |
| 712 |
script.type = "text/javascript"; |
| 713 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 714 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 715 |
})(); |
| 716 |
</script> |
| 717 |
|
| 718 |
</body> |
| 719 |
</html> |