| 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 |
<meta name="author" content="Sebastian Krantz" /> |
| 14 |
|
| 15 |
<meta name="date" content="2021-01-04" /> |
| 16 |
|
| 17 |
<title>collapse and dplyr</title> |
| 18 |
|
| 19 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 20 |
// be compatible with the behavior of Pandoc < 2.8). |
| 21 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 22 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 23 |
var i, h, a; |
| 24 |
for (i = 0; i < hs.length; i++) { |
| 25 |
h = hs[i]; |
| 26 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 27 |
a = h.attributes; |
| 28 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 29 |
} |
| 30 |
}); |
| 31 |
</script> |
| 32 |
|
| 33 |
<style type="text/css"> |
| 34 |
code{white-space: pre-wrap;} |
| 35 |
span.smallcaps{font-variant: small-caps;} |
| 36 |
span.underline{text-decoration: underline;} |
| 37 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 38 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 39 |
ul.task-list{list-style: none;} |
| 40 |
</style> |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
<style type="text/css"> |
| 45 |
code { |
| 46 |
white-space: pre; |
| 47 |
} |
| 48 |
.sourceCode { |
| 49 |
overflow: visible; |
| 50 |
} |
| 51 |
</style> |
| 52 |
<style type="text/css" data-origin="pandoc"> |
| 53 |
html { -webkit-text-size-adjust: 100%; } |
| 54 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 55 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 56 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 57 |
.sourceCode { overflow: visible; } |
| 58 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 59 |
div.sourceCode { margin: 1em 0; } |
| 60 |
pre.sourceCode { margin: 0; } |
| 61 |
@media screen { |
| 62 |
div.sourceCode { overflow: auto; } |
| 63 |
} |
| 64 |
@media print { |
| 65 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 66 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 67 |
} |
| 68 |
pre.numberSource code |
| 69 |
{ counter-reset: source-line 0; } |
| 70 |
pre.numberSource code > span |
| 71 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 72 |
pre.numberSource code > span > a:first-child::before |
| 73 |
{ content: counter(source-line); |
| 74 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 75 |
border: none; display: inline-block; |
| 76 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 77 |
-khtml-user-select: none; -moz-user-select: none; |
| 78 |
-ms-user-select: none; user-select: none; |
| 79 |
padding: 0 4px; width: 4em; |
| 80 |
color: #aaaaaa; |
| 81 |
} |
| 82 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 83 |
div.sourceCode |
| 84 |
{ } |
| 85 |
@media screen { |
| 86 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 87 |
} |
| 88 |
code span.al { color: #ff0000; font-weight: bold; } |
| 89 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 90 |
code span.at { color: #7d9029; } |
| 91 |
code span.bn { color: #40a070; } |
| 92 |
code span.bu { color: #008000; } |
| 93 |
code span.cf { color: #007020; font-weight: bold; } |
| 94 |
code span.ch { color: #4070a0; } |
| 95 |
code span.cn { color: #880000; } |
| 96 |
code span.co { color: #60a0b0; font-style: italic; } |
| 97 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 98 |
code span.do { color: #ba2121; font-style: italic; } |
| 99 |
code span.dt { color: #902000; } |
| 100 |
code span.dv { color: #40a070; } |
| 101 |
code span.er { color: #ff0000; font-weight: bold; } |
| 102 |
code span.ex { } |
| 103 |
code span.fl { color: #40a070; } |
| 104 |
code span.fu { color: #06287e; } |
| 105 |
code span.im { color: #008000; font-weight: bold; } |
| 106 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 107 |
code span.kw { color: #007020; font-weight: bold; } |
| 108 |
code span.op { color: #666666; } |
| 109 |
code span.ot { color: #007020; } |
| 110 |
code span.pp { color: #bc7a00; } |
| 111 |
code span.sc { color: #4070a0; } |
| 112 |
code span.ss { color: #bb6688; } |
| 113 |
code span.st { color: #4070a0; } |
| 114 |
code span.va { color: #19177c; } |
| 115 |
code span.vs { color: #4070a0; } |
| 116 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 117 |
</style> |
| 118 |
<script> |
| 119 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 120 |
(function() { |
| 121 |
var sheets = document.styleSheets; |
| 122 |
for (var i = 0; i < sheets.length; i++) { |
| 123 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 124 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 125 |
var j = 0; |
| 126 |
while (j < rules.length) { |
| 127 |
var rule = rules[j]; |
| 128 |
// check if there is a div.sourceCode rule |
| 129 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 130 |
j++; |
| 131 |
continue; |
| 132 |
} |
| 133 |
var style = rule.style.cssText; |
| 134 |
// check if color or background-color is set |
| 135 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 136 |
j++; |
| 137 |
continue; |
| 138 |
} |
| 139 |
// replace div.sourceCode by a pre.sourceCode rule |
| 140 |
sheets[i].deleteRule(j); |
| 141 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 142 |
} |
| 143 |
} |
| 144 |
})(); |
| 145 |
</script> |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
<style type="text/css">body { |
| 151 |
background-color: #fff; |
| 152 |
margin: 1em auto; |
| 153 |
max-width: 700px; |
| 154 |
overflow: visible; |
| 155 |
padding-left: 2em; |
| 156 |
padding-right: 2em; |
| 157 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 158 |
font-size: 14px; |
| 159 |
line-height: 1.35; |
| 160 |
} |
| 161 |
#TOC { |
| 162 |
clear: both; |
| 163 |
margin: 0 0 10px 10px; |
| 164 |
padding: 4px; |
| 165 |
width: 400px; |
| 166 |
border: 1px solid #CCCCCC; |
| 167 |
border-radius: 5px; |
| 168 |
background-color: #f6f6f6; |
| 169 |
font-size: 13px; |
| 170 |
line-height: 1.3; |
| 171 |
} |
| 172 |
#TOC .toctitle { |
| 173 |
font-weight: bold; |
| 174 |
font-size: 15px; |
| 175 |
margin-left: 5px; |
| 176 |
} |
| 177 |
#TOC ul { |
| 178 |
padding-left: 40px; |
| 179 |
margin-left: -1.5em; |
| 180 |
margin-top: 5px; |
| 181 |
margin-bottom: 5px; |
| 182 |
} |
| 183 |
#TOC ul ul { |
| 184 |
margin-left: -2em; |
| 185 |
} |
| 186 |
#TOC li { |
| 187 |
line-height: 16px; |
| 188 |
} |
| 189 |
table { |
| 190 |
margin: 1em auto; |
| 191 |
border-width: 1px; |
| 192 |
border-color: #DDDDDD; |
| 193 |
border-style: outset; |
| 194 |
border-collapse: collapse; |
| 195 |
} |
| 196 |
table th { |
| 197 |
border-width: 2px; |
| 198 |
padding: 5px; |
| 199 |
border-style: inset; |
| 200 |
} |
| 201 |
table td { |
| 202 |
border-width: 1px; |
| 203 |
border-style: inset; |
| 204 |
line-height: 18px; |
| 205 |
padding: 5px 5px; |
| 206 |
} |
| 207 |
table, table th, table td { |
| 208 |
border-left-style: none; |
| 209 |
border-right-style: none; |
| 210 |
} |
| 211 |
table thead, table tr.even { |
| 212 |
background-color: #f7f7f7; |
| 213 |
} |
| 214 |
p { |
| 215 |
margin: 0.5em 0; |
| 216 |
} |
| 217 |
blockquote { |
| 218 |
background-color: #f6f6f6; |
| 219 |
padding: 0.25em 0.75em; |
| 220 |
} |
| 221 |
hr { |
| 222 |
border-style: solid; |
| 223 |
border: none; |
| 224 |
border-top: 1px solid #777; |
| 225 |
margin: 28px 0; |
| 226 |
} |
| 227 |
dl { |
| 228 |
margin-left: 0; |
| 229 |
} |
| 230 |
dl dd { |
| 231 |
margin-bottom: 13px; |
| 232 |
margin-left: 13px; |
| 233 |
} |
| 234 |
dl dt { |
| 235 |
font-weight: bold; |
| 236 |
} |
| 237 |
ul { |
| 238 |
margin-top: 0; |
| 239 |
} |
| 240 |
ul li { |
| 241 |
list-style: circle outside; |
| 242 |
} |
| 243 |
ul ul { |
| 244 |
margin-bottom: 0; |
| 245 |
} |
| 246 |
pre, code { |
| 247 |
background-color: #f7f7f7; |
| 248 |
border-radius: 3px; |
| 249 |
color: #333; |
| 250 |
white-space: pre-wrap; |
| 251 |
} |
| 252 |
pre { |
| 253 |
border-radius: 3px; |
| 254 |
margin: 5px 0px 10px 0px; |
| 255 |
padding: 10px; |
| 256 |
} |
| 257 |
pre:not([class]) { |
| 258 |
background-color: #f7f7f7; |
| 259 |
} |
| 260 |
code { |
| 261 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 262 |
font-size: 85%; |
| 263 |
} |
| 264 |
p > code, li > code { |
| 265 |
padding: 2px 0px; |
| 266 |
} |
| 267 |
div.figure { |
| 268 |
text-align: center; |
| 269 |
} |
| 270 |
img { |
| 271 |
background-color: #FFFFFF; |
| 272 |
padding: 2px; |
| 273 |
border: 1px solid #DDDDDD; |
| 274 |
border-radius: 3px; |
| 275 |
border: 1px solid #CCCCCC; |
| 276 |
margin: 0 5px; |
| 277 |
} |
| 278 |
h1 { |
| 279 |
margin-top: 0; |
| 280 |
font-size: 35px; |
| 281 |
line-height: 40px; |
| 282 |
} |
| 283 |
h2 { |
| 284 |
border-bottom: 4px solid #f7f7f7; |
| 285 |
padding-top: 10px; |
| 286 |
padding-bottom: 2px; |
| 287 |
font-size: 145%; |
| 288 |
} |
| 289 |
h3 { |
| 290 |
border-bottom: 2px solid #f7f7f7; |
| 291 |
padding-top: 10px; |
| 292 |
font-size: 120%; |
| 293 |
} |
| 294 |
h4 { |
| 295 |
border-bottom: 1px solid #f7f7f7; |
| 296 |
margin-left: 8px; |
| 297 |
font-size: 105%; |
| 298 |
} |
| 299 |
h5, h6 { |
| 300 |
border-bottom: 1px solid #ccc; |
| 301 |
font-size: 105%; |
| 302 |
} |
| 303 |
a { |
| 304 |
color: #0033dd; |
| 305 |
text-decoration: none; |
| 306 |
} |
| 307 |
a:hover { |
| 308 |
color: #6666ff; } |
| 309 |
a:visited { |
| 310 |
color: #800080; } |
| 311 |
a:visited:hover { |
| 312 |
color: #BB00BB; } |
| 313 |
a[href^="http:"] { |
| 314 |
text-decoration: underline; } |
| 315 |
a[href^="https:"] { |
| 316 |
text-decoration: underline; } |
| 317 |
|
| 318 |
code > span.kw { color: #555; font-weight: bold; } |
| 319 |
code > span.dt { color: #902000; } |
| 320 |
code > span.dv { color: #40a070; } |
| 321 |
code > span.bn { color: #d14; } |
| 322 |
code > span.fl { color: #d14; } |
| 323 |
code > span.ch { color: #d14; } |
| 324 |
code > span.st { color: #d14; } |
| 325 |
code > span.co { color: #888888; font-style: italic; } |
| 326 |
code > span.ot { color: #007020; } |
| 327 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 328 |
code > span.fu { color: #900; font-weight: bold; } |
| 329 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 330 |
</style> |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
</head> |
| 336 |
|
| 337 |
<body> |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
<h1 class="title toc-ignore">collapse and dplyr</h1> |
| 343 |
<h3 class="subtitle">Fast (Weighted) Aggregations and Transformations in |
| 344 |
a Piped Workflow</h3> |
| 345 |
<h4 class="author">Sebastian Krantz</h4> |
| 346 |
<h4 class="date">2021-01-04</h4> |
| 347 |
|
| 348 |
|
| 349 |
<div id="TOC"> |
| 350 |
<ul> |
| 351 |
<li><a href="#fast-aggregations" id="toc-fast-aggregations">1. Fast |
| 352 |
Aggregations</a> |
| 353 |
<ul> |
| 354 |
<li><a href="#simple-aggregations" id="toc-simple-aggregations">1.1 |
| 355 |
Simple Aggregations</a></li> |
| 356 |
<li><a href="#more-speed-using-collapse-verbs" id="toc-more-speed-using-collapse-verbs">1.2 More Speed using |
| 357 |
<em>collapse</em> Verbs</a></li> |
| 358 |
<li><a href="#multi-function-aggregations" id="toc-multi-function-aggregations">1.3 Multi-Function |
| 359 |
Aggregations</a></li> |
| 360 |
<li><a href="#weighted-aggregations" id="toc-weighted-aggregations">1.4 |
| 361 |
Weighted Aggregations</a></li> |
| 362 |
</ul></li> |
| 363 |
<li><a href="#fast-transformations" id="toc-fast-transformations">2. |
| 364 |
Fast Transformations</a> |
| 365 |
<ul> |
| 366 |
<li><a href="#fast-transform-and-compute-variables" id="toc-fast-transform-and-compute-variables">2.1 Fast Transform and |
| 367 |
Compute Variables</a></li> |
| 368 |
<li><a href="#replacing-and-sweeping-out-statistics" id="toc-replacing-and-sweeping-out-statistics">2.2 Replacing and |
| 369 |
Sweeping out Statistics</a></li> |
| 370 |
<li><a href="#more-control-using-the-tra-function" id="toc-more-control-using-the-tra-function">2.3 More Control using the |
| 371 |
<code>TRA</code> Function</a></li> |
| 372 |
<li><a href="#faster-centering-averaging-and-standardizing" id="toc-faster-centering-averaging-and-standardizing">2.4 Faster |
| 373 |
Centering, Averaging and Standardizing</a></li> |
| 374 |
<li><a href="#lags-leads-differences-and-growth-rates" id="toc-lags-leads-differences-and-growth-rates">2.5 Lags / Leads, |
| 375 |
Differences and Growth Rates</a></li> |
| 376 |
</ul></li> |
| 377 |
<li><a href="#benchmarks" id="toc-benchmarks">3. Benchmarks</a> |
| 378 |
<ul> |
| 379 |
<li><a href="#data" id="toc-data">3.1 Data</a></li> |
| 380 |
<li><a href="#selecting-subsetting-ordering-and-grouping" id="toc-selecting-subsetting-ordering-and-grouping">3.1 Selecting, |
| 381 |
Subsetting, Ordering and Grouping</a></li> |
| 382 |
<li><a href="#aggregation" id="toc-aggregation">3.1 Aggregation</a></li> |
| 383 |
<li><a href="#transformation" id="toc-transformation">3.2 |
| 384 |
Transformation</a></li> |
| 385 |
</ul></li> |
| 386 |
<li><a href="#references" id="toc-references">References</a></li> |
| 387 |
</ul> |
| 388 |
</div> |
| 389 |
|
| 390 |
<style type="text/css"> |
| 391 |
pre { |
| 392 |
max-height: 500px; |
| 393 |
overflow-y: auto; |
| 394 |
} |
| 395 |
pre[class] { |
| 396 |
max-height: 500px; |
| 397 |
} |
| 398 |
</style> |
| 399 |
<!-- |
| 400 |
*collapse* is a C/C++ based package for data transformation and statistical computing in R. It's aims are: |
| 401 |
|
| 402 |
1. To facilitate complex data transformation, exploration and computing tasks in R. |
| 403 |
2. To help make R code fast, flexible, parsimonious and programmer friendly. |
| 404 |
--> |
| 405 |
<p>This vignette focuses on the integration of <em>collapse</em> and the |
| 406 |
popular <em>dplyr</em> package by Hadley Wickham. In particular it will |
| 407 |
demonstrate how using <em>collapse</em>’s fast functions and some fast |
| 408 |
alternatives for <em>dplyr</em> verbs can substantially facilitate and |
| 409 |
speed up basic data manipulation, grouped and weighted aggregations and |
| 410 |
transformations, and panel data computations (i.e. between- and |
| 411 |
within-transformations, panel-lags, differences and growth rates) in a |
| 412 |
<em>dplyr</em> (piped) workflow.</p> |
| 413 |
<hr /> |
| 414 |
<p><strong>Notes:</strong></p> |
| 415 |
<ul> |
| 416 |
<li><p>This vignette is targeted at <em>dplyr</em> / <em>tidyverse</em> |
| 417 |
users. <em>collapse</em> is a standalone package and can be programmed |
| 418 |
efficiently without pipes or <em>dplyr</em> verbs.</p></li> |
| 419 |
<li><p>The ‘Introduction to <em>collapse</em>’ vignette provides a |
| 420 |
thorough introduction to the package and a built-in structured |
| 421 |
documentation is available under |
| 422 |
<code>help("collapse-documentation")</code> after installing the |
| 423 |
package. In addition <code>help("collapse-package")</code> provides a |
| 424 |
compact set of examples for quick-start.</p></li> |
| 425 |
<li><p>Documentation and vignettes can also be viewed <a href="https://fastverse.org/collapse/">online</a>.</p></li> |
| 426 |
</ul> |
| 427 |
<hr /> |
| 428 |
<div id="fast-aggregations" class="section level2"> |
| 429 |
<h2>1. Fast Aggregations</h2> |
| 430 |
<p>A key feature of <em>collapse</em> is it’s broad set of <em>Fast |
| 431 |
Statistical Functions</em> |
| 432 |
(<code>fsum, fprod, fmean, fmedian, fmode, fvar, fsd, fmin, fmax, fnth, ffirst, flast, fnobs, fndistinct</code>) |
| 433 |
which are able to substantially speed-up column-wise, grouped and |
| 434 |
weighted computations on vectors, matrices or data frames. The functions |
| 435 |
are S3 generic, with a default (vector), matrix and data frame method, |
| 436 |
as well as a grouped_df method for grouped tibbles used by |
| 437 |
<em>dplyr</em>. The grouped tibble method has the following |
| 438 |
arguments:</p> |
| 439 |
<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">FUN.grouped_df</span>(x, [<span class="at">w =</span> <span class="cn">NULL</span>,] <span class="at">TRA =</span> <span class="cn">NULL</span>, [<span class="at">na.rm =</span> <span class="cn">TRUE</span>,]</span> |
| 440 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a> <span class="at">use.g.names =</span> <span class="cn">FALSE</span>, <span class="at">keep.group_vars =</span> <span class="cn">TRUE</span>, [<span class="at">keep.w =</span> <span class="cn">TRUE</span>,] ...)</span></code></pre></div> |
| 441 |
<p>where <code>w</code> is a weight variable, and <code>TRA</code> and |
| 442 |
can be used to transform <code>x</code> using the computed statistics |
| 443 |
and one of 10 available transformations |
| 444 |
(<code>"replace_fill", "replace", "-", "-+", "/", "%", "+", "*", "%%", "-%%"</code>, |
| 445 |
discussed in section 2). <code>na.rm</code> efficiently removes missing |
| 446 |
values and is <code>TRUE</code> by default. <code>use.g.names</code> |
| 447 |
generates new row-names from the unique combinations of groups (default: |
| 448 |
disabled), whereas <code>keep.group_vars</code> (default: enabled) will |
| 449 |
keep the grouping columns as is custom in the native |
| 450 |
<code>data %>% group_by(...) %>% summarize(...)</code> workflow in |
| 451 |
<em>dplyr</em>. Finally, <code>keep.w</code> regulates whether a |
| 452 |
weighting variable used is also aggregated and saved in a column. For |
| 453 |
<code>fsum, fmean, fmedian, fnth, fvar, fsd</code> and |
| 454 |
<code>fmode</code> this will compute the sum of the weights in each |
| 455 |
group, whereas <code>fprod</code> returns the product of the |
| 456 |
weights.</p> |
| 457 |
<p>With that in mind, let’s consider some straightforward |
| 458 |
applications.</p> |
| 459 |
<div id="simple-aggregations" class="section level3"> |
| 460 |
<h3>1.1 Simple Aggregations</h3> |
| 461 |
<p>Consider the Groningen Growth and Development Center 10-Sector |
| 462 |
Database included in <em>collapse</em> and introduced in the main |
| 463 |
vignette:</p> |
| 464 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="fu">library</span>(collapse)</span> |
| 465 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">head</span>(GGDC10S)</span> |
| 466 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU</span></span> |
| 467 |
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co"># 1 BWA SSA Sub-saharan Africa VA 1960 NA NA NA NA</span></span> |
| 468 |
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="co"># 2 BWA SSA Sub-saharan Africa VA 1961 NA NA NA NA</span></span> |
| 469 |
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co"># 3 BWA SSA Sub-saharan Africa VA 1962 NA NA NA NA</span></span> |
| 470 |
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co"># 4 BWA SSA Sub-saharan Africa VA 1963 NA NA NA NA</span></span> |
| 471 |
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co"># 5 BWA SSA Sub-saharan Africa VA 1964 16.30154 3.494075 0.7365696 0.1043936</span></span> |
| 472 |
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co"># 6 BWA SSA Sub-saharan Africa VA 1965 15.72700 2.495768 1.0181992 0.1350976</span></span> |
| 473 |
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="co"># CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 474 |
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a><span class="co"># 1 NA NA NA NA NA NA NA</span></span> |
| 475 |
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a><span class="co"># 2 NA NA NA NA NA NA NA</span></span> |
| 476 |
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a><span class="co"># 3 NA NA NA NA NA NA NA</span></span> |
| 477 |
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a><span class="co"># 4 NA NA NA NA NA NA NA</span></span> |
| 478 |
<span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a><span class="co"># 5 0.6600454 6.243732 1.658928 1.119194 4.822485 2.341328 37.48229</span></span> |
| 479 |
<span id="cb2-16"><a href="#cb2-16" tabindex="-1"></a><span class="co"># 6 1.3462312 7.064825 1.939007 1.246789 5.695848 2.678338 39.34710</span></span> |
| 480 |
<span id="cb2-17"><a href="#cb2-17" tabindex="-1"></a></span> |
| 481 |
<span id="cb2-18"><a href="#cb2-18" tabindex="-1"></a><span class="co"># Summarize the Data: </span></span> |
| 482 |
<span id="cb2-19"><a href="#cb2-19" tabindex="-1"></a><span class="co"># descr(GGDC10S, cols = is_categorical)</span></span> |
| 483 |
<span id="cb2-20"><a href="#cb2-20" tabindex="-1"></a><span class="co"># aperm(qsu(GGDC10S, ~Variable, cols = is.numeric))</span></span> |
| 484 |
<span id="cb2-21"><a href="#cb2-21" tabindex="-1"></a></span> |
| 485 |
<span id="cb2-22"><a href="#cb2-22" tabindex="-1"></a><span class="co"># Efficiently converting to tibble (no deep copy)</span></span> |
| 486 |
<span id="cb2-23"><a href="#cb2-23" tabindex="-1"></a>GGDC10S <span class="ot"><-</span> <span class="fu">qTBL</span>(GGDC10S)</span></code></pre></div> |
| 487 |
<p>Simple column-wise computations using the fast functions and pipe |
| 488 |
operators are performed as follows:</p> |
| 489 |
<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">library</span>(dplyr)</span> |
| 490 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a></span> |
| 491 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> fnobs <span class="co"># Number of Observations</span></span> |
| 492 |
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU </span></span> |
| 493 |
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co"># 5027 5027 5027 5027 5027 4364 4355 4355 4354 </span></span> |
| 494 |
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co"># CON WRT TRA FIRE GOV OTH SUM </span></span> |
| 495 |
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co"># 4355 4355 4355 4355 3482 4248 4364</span></span> |
| 496 |
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> fndistinct <span class="co"># Number of distinct values</span></span> |
| 497 |
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU </span></span> |
| 498 |
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co"># 43 6 6 2 67 4353 4224 4353 4237 </span></span> |
| 499 |
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="co"># CON WRT TRA FIRE GOV OTH SUM </span></span> |
| 500 |
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a><span class="co"># 4339 4344 4334 4349 3470 4238 4364</span></span> |
| 501 |
<span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">select_at</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fmedian <span class="co"># Median</span></span> |
| 502 |
<span id="cb3-14"><a href="#cb3-14" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV </span></span> |
| 503 |
<span id="cb3-15"><a href="#cb3-15" tabindex="-1"></a><span class="co"># 4394.5194 173.2234 3718.0981 167.9500 1473.4470 3773.6430 1174.8000 960.1251 3928.5127 </span></span> |
| 504 |
<span id="cb3-16"><a href="#cb3-16" tabindex="-1"></a><span class="co"># OTH SUM </span></span> |
| 505 |
<span id="cb3-17"><a href="#cb3-17" tabindex="-1"></a><span class="co"># 1433.1722 23186.1936</span></span> |
| 506 |
<span id="cb3-18"><a href="#cb3-18" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">select_at</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fmean <span class="co"># Mean</span></span> |
| 507 |
<span id="cb3-19"><a href="#cb3-19" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV </span></span> |
| 508 |
<span id="cb3-20"><a href="#cb3-20" tabindex="-1"></a><span class="co"># 2526696.5 1867908.9 5538491.4 335679.5 1801597.6 3392909.5 1473269.7 1657114.8 1712300.3 </span></span> |
| 509 |
<span id="cb3-21"><a href="#cb3-21" tabindex="-1"></a><span class="co"># OTH SUM </span></span> |
| 510 |
<span id="cb3-22"><a href="#cb3-22" tabindex="-1"></a><span class="co"># 1684527.3 21566436.8</span></span> |
| 511 |
<span id="cb3-23"><a href="#cb3-23" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> fmode <span class="co"># Mode</span></span> |
| 512 |
<span id="cb3-24"><a href="#cb3-24" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year </span></span> |
| 513 |
<span id="cb3-25"><a href="#cb3-25" tabindex="-1"></a><span class="co"># "USA" "ASI" "Asia" "EMP" "2010" </span></span> |
| 514 |
<span id="cb3-26"><a href="#cb3-26" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON </span></span> |
| 515 |
<span id="cb3-27"><a href="#cb3-27" tabindex="-1"></a><span class="co"># "171.315882316326" "0" "4645.12507642586" "0" "1.34623115930777" </span></span> |
| 516 |
<span id="cb3-28"><a href="#cb3-28" tabindex="-1"></a><span class="co"># WRT TRA FIRE GOV OTH </span></span> |
| 517 |
<span id="cb3-29"><a href="#cb3-29" tabindex="-1"></a><span class="co"># "21.8380052682527" "8.97743416914571" "40.0701608636442" "0" "3626.84423577048" </span></span> |
| 518 |
<span id="cb3-30"><a href="#cb3-30" tabindex="-1"></a><span class="co"># SUM </span></span> |
| 519 |
<span id="cb3-31"><a href="#cb3-31" tabindex="-1"></a><span class="co"># "37.4822945751317"</span></span> |
| 520 |
<span id="cb3-32"><a href="#cb3-32" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fmode</span>(<span class="at">drop =</span> <span class="cn">FALSE</span>) <span class="co"># Keep data structure intact</span></span> |
| 521 |
<span id="cb3-33"><a href="#cb3-33" tabindex="-1"></a><span class="co"># # A tibble: 1 × 16</span></span> |
| 522 |
<span id="cb3-34"><a href="#cb3-34" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 523 |
<span id="cb3-35"><a href="#cb3-35" tabindex="-1"></a><span class="co"># * <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 524 |
<span id="cb3-36"><a href="#cb3-36" tabindex="-1"></a><span class="co"># 1 USA ASI Asia EMP 2010 171. 0 4645. 0 1.35 21.8 8.98 40.1 0</span></span> |
| 525 |
<span id="cb3-37"><a href="#cb3-37" tabindex="-1"></a><span class="co"># # ℹ 2 more variables: OTH <dbl>, SUM <dbl></span></span></code></pre></div> |
| 526 |
<p>Moving on to grouped statistics, we can compute the average value |
| 527 |
added and employment by sector and country using:</p> |
| 528 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> </span> |
| 529 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 530 |
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a> <span class="fu">select_at</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fmean</span> |
| 531 |
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="co"># # A tibble: 85 × 13</span></span> |
| 532 |
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 533 |
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 534 |
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co"># 1 EMP ARG 1420. 52.1 1932. 1.02e2 7.42e2 1.98e3 6.49e2 628. 2043. 9.92e2 1.05e4</span></span> |
| 535 |
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co"># 2 EMP BOL 964. 56.0 235. 5.35e0 1.23e2 2.82e2 1.15e2 44.6 NA 3.96e2 2.22e3</span></span> |
| 536 |
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co"># 3 EMP BRA 17191. 206. 6991. 3.65e2 3.52e3 8.51e3 2.05e3 4414. 5307. 5.71e3 5.43e4</span></span> |
| 537 |
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co"># 4 EMP BWA 188. 10.5 18.1 3.09e0 2.53e1 3.63e1 8.36e0 15.3 61.1 2.76e1 3.94e2</span></span> |
| 538 |
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co"># 5 EMP CHL 702. 101. 625. 2.94e1 2.96e2 6.95e2 2.58e2 272. NA 1.00e3 3.98e3</span></span> |
| 539 |
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a><span class="co"># 6 EMP CHN 287744. 7050. 67144. 1.61e3 2.09e4 2.89e4 1.39e4 4929. 22669. 3.10e4 4.86e5</span></span> |
| 540 |
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co"># 7 EMP COL 3091. 145. 1175. 3.39e1 5.24e2 2.07e3 4.70e2 649. NA 1.73e3 9.89e3</span></span> |
| 541 |
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="co"># 8 EMP CRI 231. 1.70 136. 1.43e1 5.76e1 1.57e2 4.24e1 54.9 128. 6.51e1 8.87e2</span></span> |
| 542 |
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="co"># 9 EMP DEW 2490. 407. 8473. 2.26e2 2.09e3 4.44e3 1.48e3 1689. 3945. 9.99e2 2.62e4</span></span> |
| 543 |
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a><span class="co"># 10 EMP DNK 236. 8.03 507. 1.38e1 1.71e2 4.55e2 1.61e2 181. 549. 1.11e2 2.39e3</span></span> |
| 544 |
<span id="cb4-17"><a href="#cb4-17" tabindex="-1"></a><span class="co"># # ℹ 75 more rows</span></span></code></pre></div> |
| 545 |
<p>Similarly we can aggregate using any other of the above |
| 546 |
functions.</p> |
| 547 |
<!-- ```{r} --> |
| 548 |
<!-- GGDC10S %>% --> |
| 549 |
<!-- group_by(Variable, Country) %>% --> |
| 550 |
<!-- select_at(6:16) %>% fmedian --> |
| 551 |
<!-- GGDC10S %>% --> |
| 552 |
<!-- group_by(Variable, Country) %>% --> |
| 553 |
<!-- select_at(6:16) %>% fsd --> |
| 554 |
<!-- ``` --> |
| 555 |
<p>It is important to not use <em>dplyr</em>’s <code>summarize</code> |
| 556 |
together with these functions since that would eliminate their speed |
| 557 |
gain. These functions are fast because they are executed only once and |
| 558 |
carry out the grouped computations in C++, whereas |
| 559 |
<code>summarize</code> will apply the function to each group in the |
| 560 |
grouped tibble.</p> |
| 561 |
<!-- - It will also work with the fast functions, but is slower than using primitive base functions since the fast functions are S3 generic -. --> |
| 562 |
<hr /> |
| 563 |
<div id="excursus-what-is-happening-behind-the-scenes" class="section level4"> |
| 564 |
<h4>Excursus: What is Happening Behind the Scenes?</h4> |
| 565 |
<p>To better explain this point it is perhaps good to shed some light on |
| 566 |
what is happening behind the scenes of <em>dplyr</em> and |
| 567 |
<em>collapse</em>. Fundamentally both packages follow different |
| 568 |
computing paradigms:</p> |
| 569 |
<p><em>dplyr</em> is an efficient implementation of the |
| 570 |
Split-Apply-Combine computing paradigm. Data is split into groups, these |
| 571 |
data-chunks are then passed to a function carrying out the computation, |
| 572 |
and finally recombined to produce the aggregated data.frame. |
| 573 |
<!-- The efficiency of that process depends on the efficiency of the grouping, splitting, the function(s) applied and the recombining. --> |
| 574 |
This modus operandi is evident in the grouping mechanism of |
| 575 |
<em>dplyr</em>. When a data.frame is passed through <em>group_by</em>, a |
| 576 |
‘groups’ attribute is attached:</p> |
| 577 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">attr</span>(<span class="st">"groups"</span>)</span> |
| 578 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="co"># # A tibble: 85 × 3</span></span> |
| 579 |
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="co"># Variable Country .rows</span></span> |
| 580 |
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="co"># <chr> <chr> <list<int>></span></span> |
| 581 |
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="co"># 1 EMP ARG [62]</span></span> |
| 582 |
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="co"># 2 EMP BOL [61]</span></span> |
| 583 |
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co"># 3 EMP BRA [62]</span></span> |
| 584 |
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co"># 4 EMP BWA [52]</span></span> |
| 585 |
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co"># 5 EMP CHL [63]</span></span> |
| 586 |
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co"># 6 EMP CHN [62]</span></span> |
| 587 |
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co"># 7 EMP COL [61]</span></span> |
| 588 |
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="co"># 8 EMP CRI [62]</span></span> |
| 589 |
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a><span class="co"># 9 EMP DEW [61]</span></span> |
| 590 |
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a><span class="co"># 10 EMP DNK [64]</span></span> |
| 591 |
<span id="cb5-15"><a href="#cb5-15" tabindex="-1"></a><span class="co"># # ℹ 75 more rows</span></span></code></pre></div> |
| 592 |
<p>This object is a data.frame giving the unique groups and in the third |
| 593 |
(last) column vectors containing the indices of the rows belonging to |
| 594 |
that group. A command like <code>summarize</code> uses this information |
| 595 |
to split the data.frame into groups which are then passed sequentially |
| 596 |
to the function used and later recombined. These steps are also done in |
| 597 |
C++ which makes <em>dplyr</em> quite efficient.</p> |
| 598 |
<p>Now <em>collapse</em> is based around one-pass grouped computations |
| 599 |
at the C++ level using its own grouped statistical functions. In other |
| 600 |
words the data is not split and recombined at all but the entire |
| 601 |
computation is performed in a single C++ loop running through that data |
| 602 |
and completing the computations for each group simultaneously. This |
| 603 |
modus operandi is also evident in <em>collapse</em> grouping objects. |
| 604 |
The method <code>GRP.grouped_df</code> takes a <em>dplyr</em> grouping |
| 605 |
object from a grouped tibble and efficiently converts it to a |
| 606 |
<em>collapse</em> grouping object:</p> |
| 607 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> GRP <span class="sc">%>%</span> str</span> |
| 608 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co"># Class 'GRP' hidden list of 9</span></span> |
| 609 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co"># $ N.groups : int 85</span></span> |
| 610 |
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co"># $ group.id : int [1:5027] 46 46 46 46 46 46 46 46 46 46 ...</span></span> |
| 611 |
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="co"># $ group.sizes : int [1:85] 62 61 62 52 63 62 61 62 61 64 ...</span></span> |
| 612 |
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co"># $ groups :List of 2</span></span> |
| 613 |
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co"># ..$ Variable: chr [1:85] "EMP" "EMP" "EMP" "EMP" ...</span></span> |
| 614 |
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co"># .. ..- attr(*, "label")= chr "Variable"</span></span> |
| 615 |
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="co"># .. ..- attr(*, "format.stata")= chr "%9s"</span></span> |
| 616 |
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co"># ..$ Country : chr [1:85] "ARG" "BOL" "BRA" "BWA" ...</span></span> |
| 617 |
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a><span class="co"># .. ..- attr(*, "label")= chr "Country"</span></span> |
| 618 |
<span id="cb6-12"><a href="#cb6-12" tabindex="-1"></a><span class="co"># .. ..- attr(*, "format.stata")= chr "%9s"</span></span> |
| 619 |
<span id="cb6-13"><a href="#cb6-13" tabindex="-1"></a><span class="co"># $ group.vars : chr [1:2] "Variable" "Country"</span></span> |
| 620 |
<span id="cb6-14"><a href="#cb6-14" tabindex="-1"></a><span class="co"># $ ordered : Named logi [1:2] TRUE FALSE</span></span> |
| 621 |
<span id="cb6-15"><a href="#cb6-15" tabindex="-1"></a><span class="co"># ..- attr(*, "names")= chr [1:2] "ordered" "sorted"</span></span> |
| 622 |
<span id="cb6-16"><a href="#cb6-16" tabindex="-1"></a><span class="co"># $ order : NULL</span></span> |
| 623 |
<span id="cb6-17"><a href="#cb6-17" tabindex="-1"></a><span class="co"># $ group.starts: NULL</span></span> |
| 624 |
<span id="cb6-18"><a href="#cb6-18" tabindex="-1"></a><span class="co"># $ call : language GRP.grouped_df(X = .)</span></span></code></pre></div> |
| 625 |
<p>This object is a list where the first three elements give the number |
| 626 |
of groups, the group-id to which each row belongs and a vector of |
| 627 |
group-sizes. A function like <code>fsum</code> uses this information to |
| 628 |
(for each column) create a result vector of size ‘N.groups’ and the run |
| 629 |
through the column using the ‘group.id’ vector to add the i’th data |
| 630 |
point to the ’group.id[i]’th element of the result vector. When the loop |
| 631 |
is finished, the grouped computation is also finished.</p> |
| 632 |
<p>It is obvious that <em>collapse</em> is faster than <em>dplyr</em> |
| 633 |
since it’s method of computing involves less steps, and it does not need |
| 634 |
to call statistical functions multiple times. See the benchmark section. |
| 635 |
<!-- This performance gain is realized especially as data become large, since the conversion perfomed by `GRP.grouped_df` also involves a small computational cost. --></p> |
| 636 |
<hr /> |
| 637 |
</div> |
| 638 |
</div> |
| 639 |
<div id="more-speed-using-collapse-verbs" class="section level3"> |
| 640 |
<h3>1.2 More Speed using <em>collapse</em> Verbs</h3> |
| 641 |
<p><em>collapse</em> fast functions do not develop their maximal |
| 642 |
performance on a grouped tibble created with <code>group_by</code> |
| 643 |
because of the additional conversion cost of the grouping object |
| 644 |
incurred by <code>GRP.grouped_df</code>. This cost is already minimized |
| 645 |
through the use of C++, but we can do even better replacing |
| 646 |
<code>group_by</code> with <code>collapse::fgroup_by</code>. |
| 647 |
<code>fgroup_by</code> works like <code>group_by</code> but does the |
| 648 |
grouping with <code>collapse::GRP</code> (up to 10x faster than |
| 649 |
<code>group_by</code>) and simply attaches a <em>collapse</em> grouping |
| 650 |
object to the grouped_df. Thus the speed gain is 2-fold: Faster grouping |
| 651 |
and no conversion cost when calling <em>collapse</em> functions.</p> |
| 652 |
<p>Another improvement comes from replacing the <em>dplyr</em> verb |
| 653 |
<code>select</code> with <code>collapse::fselect</code>, and, for |
| 654 |
selection using column names, indices or functions use |
| 655 |
<code>collapse::get_vars</code> instead of <code>select_at</code> or |
| 656 |
<code>select_if</code>. Next to <code>get_vars</code>, <em>collapse</em> |
| 657 |
also introduces the predicates <code>num_vars</code>, |
| 658 |
<code>cat_vars</code>, <code>char_vars</code>, <code>fact_vars</code>, |
| 659 |
<code>logi_vars</code> and <code>date_vars</code> to efficiently select |
| 660 |
columns by type.</p> |
| 661 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fmedian</span> |
| 662 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co"># # A tibble: 85 × 13</span></span> |
| 663 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 664 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 665 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co"># 1 EMP ARG 1325. 47.4 1988. 1.05e2 7.82e2 1.85e3 5.80e2 464. 1739. 866. 9.74e3</span></span> |
| 666 |
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co"># 2 EMP BOL 943. 53.5 167. 4.46e0 6.60e1 1.32e2 9.70e1 15.3 NA 384. 1.84e3</span></span> |
| 667 |
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co"># 3 EMP BRA 17481. 225. 7208. 3.76e2 4.05e3 6.45e3 1.58e3 4355. 4450. 4479. 5.19e4</span></span> |
| 668 |
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co"># 4 EMP BWA 175. 12.2 13.1 3.71e0 1.90e1 2.11e1 6.75e0 10.4 53.8 31.2 3.61e2</span></span> |
| 669 |
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co"># 5 EMP CHL 690. 93.9 607. 2.58e1 2.30e2 4.84e2 2.05e2 106. NA 900. 3.31e3</span></span> |
| 670 |
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="co"># 6 EMP CHN 293915 8150. 61761. 1.14e3 1.06e4 1.70e4 9.56e3 4328. 19468. 9954. 4.45e5</span></span> |
| 671 |
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="co"># 7 EMP COL 3006. 84.0 1033. 3.71e1 4.19e2 1.55e3 3.91e2 655. NA 1430. 8.63e3</span></span> |
| 672 |
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a><span class="co"># 8 EMP CRI 216. 1.49 114. 7.92e0 5.50e1 8.98e1 2.55e1 19.6 122. 60.6 7.19e2</span></span> |
| 673 |
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a><span class="co"># 9 EMP DEW 2178 320. 8459. 2.47e2 2.10e3 4.45e3 1.53e3 1656 3700 900 2.65e4</span></span> |
| 674 |
<span id="cb7-14"><a href="#cb7-14" tabindex="-1"></a><span class="co"># 10 EMP DNK 187. 3.75 508. 1.36e1 1.65e2 4.61e2 1.61e2 169. 642. 104. 2.42e3</span></span> |
| 675 |
<span id="cb7-15"><a href="#cb7-15" tabindex="-1"></a><span class="co"># # ℹ 75 more rows</span></span> |
| 676 |
<span id="cb7-16"><a href="#cb7-16" tabindex="-1"></a></span> |
| 677 |
<span id="cb7-17"><a href="#cb7-17" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">collapse =</span> GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fmedian,</span> |
| 678 |
<span id="cb7-18"><a href="#cb7-18" tabindex="-1"></a> <span class="at">hybrid =</span> GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">select_at</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fmedian,</span> |
| 679 |
<span id="cb7-19"><a href="#cb7-19" tabindex="-1"></a> <span class="at">dplyr =</span> GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">select_at</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> <span class="fu">summarise_all</span>(median, <span class="at">na.rm =</span> <span class="cn">TRUE</span>))</span> |
| 680 |
<span id="cb7-20"><a href="#cb7-20" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 681 |
<span id="cb7-21"><a href="#cb7-21" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 682 |
<span id="cb7-22"><a href="#cb7-22" tabindex="-1"></a><span class="co"># collapse 236.406 263.6095 303.309 295.9175 337.061 419.635 100</span></span> |
| 683 |
<span id="cb7-23"><a href="#cb7-23" tabindex="-1"></a><span class="co"># hybrid 2699.317 2894.9690 3573.611 2998.3505 3119.772 56249.212 100</span></span> |
| 684 |
<span id="cb7-24"><a href="#cb7-24" tabindex="-1"></a><span class="co"># dplyr 15923.908 16297.8280 18810.943 16742.5140 18578.105 71125.939 100</span></span></code></pre></div> |
| 685 |
<p>Benchmarks on the different components of this code and with larger |
| 686 |
data are provided under ‘Benchmarks’. Note that a grouped tibble created |
| 687 |
with <code>fgroup_by</code> can no longer be used for grouped |
| 688 |
computations with <em>dplyr</em> verbs like <code>mutate</code> or |
| 689 |
<code>summarize</code>. <code>fgroup_by</code> first assigns the class |
| 690 |
<em>GDP_df</em> which is for printing grouping information and |
| 691 |
subsetting, then the object classes (<em>tbl_df</em>, |
| 692 |
<em>data.table</em> or whatever else), followed by classes |
| 693 |
<em>grouped_df</em> and <em>data.frame</em>, and adds the grouping |
| 694 |
object in a ‘groups’ attribute. Since <em>tbl_df</em> is assigned before |
| 695 |
<em>grouped_df</em>, the object is treated by the <em>dplyr</em> |
| 696 |
ecosystem like a normal tibble.</p> |
| 697 |
<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="fu">class</span>(<span class="fu">group_by</span>(GGDC10S, Variable, Country))</span> |
| 698 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="co"># [1] "grouped_df" "tbl_df" "tbl" "data.frame"</span></span> |
| 699 |
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a></span> |
| 700 |
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="fu">class</span>(<span class="fu">fgroup_by</span>(GGDC10S, Variable, Country))</span> |
| 701 |
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co"># [1] "GRP_df" "tbl_df" "tbl" "grouped_df" "data.frame"</span></span></code></pre></div> |
| 702 |
<p>The function <code>fungroup</code> removes classes ‘GDP_df’ and |
| 703 |
‘grouped_df’ and the ‘groups’ attribute (and can thus also be used for |
| 704 |
grouped tibbles created with <code>dplyr::group_by</code>).</p> |
| 705 |
<p>Note that any kind of data frame based class can be grouped with |
| 706 |
<code>fgroup_by</code>, and still retain full responsiveness to all |
| 707 |
methods defined for that class. Functions performing aggregation on the |
| 708 |
grouped data frame remove the grouping object and classes afterwards, |
| 709 |
yielding an object with the same class and attributes as the input.</p> |
| 710 |
<p>The print method shown below reports the grouping variables, and then |
| 711 |
in square brackets the information |
| 712 |
<code>[number of groups | average group size (standard-deviation of group sizes)]</code>:</p> |
| 713 |
<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">fgroup_by</span>(GGDC10S, Variable, Country)</span> |
| 714 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 16</span></span> |
| 715 |
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 716 |
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 717 |
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co"># 1 BWA SSA Sub-s… VA 1960 NA NA NA NA NA NA NA NA NA </span></span> |
| 718 |
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co"># 2 BWA SSA Sub-s… VA 1961 NA NA NA NA NA NA NA NA NA </span></span> |
| 719 |
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="co"># 3 BWA SSA Sub-s… VA 1962 NA NA NA NA NA NA NA NA NA </span></span> |
| 720 |
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co"># 4 BWA SSA Sub-s… VA 1963 NA NA NA NA NA NA NA NA NA </span></span> |
| 721 |
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co"># 5 BWA SSA Sub-s… VA 1964 16.3 3.49 0.737 0.104 0.660 6.24 1.66 1.12 4.82</span></span> |
| 722 |
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a><span class="co"># 6 BWA SSA Sub-s… VA 1965 15.7 2.50 1.02 0.135 1.35 7.06 1.94 1.25 5.70</span></span> |
| 723 |
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a><span class="co"># 7 BWA SSA Sub-s… VA 1966 17.7 1.97 0.804 0.203 1.35 8.27 2.15 1.36 6.37</span></span> |
| 724 |
<span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a><span class="co"># 8 BWA SSA Sub-s… VA 1967 19.1 2.30 0.938 0.203 0.897 4.31 1.72 1.54 7.04</span></span> |
| 725 |
<span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a><span class="co"># 9 BWA SSA Sub-s… VA 1968 21.1 1.84 0.750 0.203 1.22 5.17 2.44 1.03 5.03</span></span> |
| 726 |
<span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a><span class="co"># 10 BWA SSA Sub-s… VA 1969 21.9 5.24 2.14 0.578 3.47 5.75 2.72 1.23 5.59</span></span> |
| 727 |
<span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 728 |
<span id="cb9-16"><a href="#cb9-16" tabindex="-1"></a><span class="co"># # ℹ 2 more variables: OTH <dbl>, SUM <dbl></span></span> |
| 729 |
<span id="cb9-17"><a href="#cb9-17" tabindex="-1"></a><span class="co"># </span></span> |
| 730 |
<span id="cb9-18"><a href="#cb9-18" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 731 |
<p>Note further that <code>fselect</code> and <code>get_vars</code> are |
| 732 |
not full drop-in replacements for <code>select</code> because they do |
| 733 |
not have a grouped_df method:</p> |
| 734 |
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">select_at</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">3</span>)</span> |
| 735 |
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="co"># # A tibble: 3 × 13</span></span> |
| 736 |
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="co"># # Groups: Variable, Country [1]</span></span> |
| 737 |
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 738 |
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 739 |
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co"># 1 EMP EGY 5206. 29.0 2436. 307. 2733. 2977. 1992. 801. 5539. NA 22020.</span></span> |
| 740 |
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co"># 2 EMP EGY 5186. 27.6 2374. 318. 2795. 3020. 2048. 815. 5636. NA 22219.</span></span> |
| 741 |
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co"># 3 EMP EGY 5161. 24.8 2348. 325. 2931. 3110. 2065. 832. 5736. NA 22533.</span></span> |
| 742 |
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">3</span>)</span> |
| 743 |
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="co"># # A tibble: 3 × 11</span></span> |
| 744 |
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 745 |
<span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a><span class="co"># <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 746 |
<span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a><span class="co"># 1 5206. 29.0 2436. 307. 2733. 2977. 1992. 801. 5539. NA 22020.</span></span> |
| 747 |
<span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a><span class="co"># 2 5186. 27.6 2374. 318. 2795. 3020. 2048. 815. 5636. NA 22219.</span></span> |
| 748 |
<span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a><span class="co"># 3 5161. 24.8 2348. 325. 2931. 3110. 2065. 832. 5736. NA 22533.</span></span></code></pre></div> |
| 749 |
<p>Since by default <code>keep.group_vars = TRUE</code> in the <em>Fast |
| 750 |
Statistical Functions</em>, the end result is nevertheless the same:</p> |
| 751 |
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">select_at</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fmean <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">3</span>)</span> |
| 752 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="co"># # A tibble: 3 × 13</span></span> |
| 753 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 754 |
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 755 |
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="co"># 1 VA VEN 6860. 35478. 1.96e4 1.06e3 1.17e4 1.93e4 8.03e3 5.60e3 NA 19986. 1.28e5</span></span> |
| 756 |
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a><span class="co"># 2 VA ZAF 16419. 42928. 8.76e4 1.38e4 1.64e4 6.83e4 4.53e4 6.64e4 7.58e4 30167. 4.63e5</span></span> |
| 757 |
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co"># 3 VA ZMB 1268849. 1006099. 9.00e5 2.19e5 8.66e5 2.10e6 7.05e5 9.10e5 1.10e6 81871. 9.16e6</span></span> |
| 758 |
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fmean <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">3</span>)</span> |
| 759 |
<span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="co"># # A tibble: 3 × 13</span></span> |
| 760 |
<span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 761 |
<span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 762 |
<span id="cb11-12"><a href="#cb11-12" tabindex="-1"></a><span class="co"># 1 VA VEN 6860. 35478. 1.96e4 1.06e3 1.17e4 1.93e4 8.03e3 5.60e3 NA 19986. 1.28e5</span></span> |
| 763 |
<span id="cb11-13"><a href="#cb11-13" tabindex="-1"></a><span class="co"># 2 VA ZAF 16419. 42928. 8.76e4 1.38e4 1.64e4 6.83e4 4.53e4 6.64e4 7.58e4 30167. 4.63e5</span></span> |
| 764 |
<span id="cb11-14"><a href="#cb11-14" tabindex="-1"></a><span class="co"># 3 VA ZMB 1268849. 1006099. 9.00e5 2.19e5 8.66e5 2.10e6 7.05e5 9.10e5 1.10e6 81871. 9.16e6</span></span></code></pre></div> |
| 765 |
<p>Another useful verb introduced by <em>collapse</em> is |
| 766 |
<code>fgroup_vars</code>, which can be used to efficiently obtain the |
| 767 |
grouping columns or grouping variables from a grouped tibble:</p> |
| 768 |
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="co"># fgroup_by fully supports grouped tibbles created with group_by or fgroup_by: </span></span> |
| 769 |
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> fgroup_vars <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 770 |
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co"># # A tibble: 3 × 2</span></span> |
| 771 |
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="co"># Variable Country</span></span> |
| 772 |
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="co"># <chr> <chr> </span></span> |
| 773 |
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co"># 1 VA BWA </span></span> |
| 774 |
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co"># 2 VA BWA </span></span> |
| 775 |
<span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="co"># 3 VA BWA</span></span> |
| 776 |
<span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> fgroup_vars <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 777 |
<span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="co"># # A tibble: 3 × 2</span></span> |
| 778 |
<span id="cb12-11"><a href="#cb12-11" tabindex="-1"></a><span class="co"># Variable Country</span></span> |
| 779 |
<span id="cb12-12"><a href="#cb12-12" tabindex="-1"></a><span class="co"># <chr> <chr> </span></span> |
| 780 |
<span id="cb12-13"><a href="#cb12-13" tabindex="-1"></a><span class="co"># 1 VA BWA </span></span> |
| 781 |
<span id="cb12-14"><a href="#cb12-14" tabindex="-1"></a><span class="co"># 2 VA BWA </span></span> |
| 782 |
<span id="cb12-15"><a href="#cb12-15" tabindex="-1"></a><span class="co"># 3 VA BWA</span></span> |
| 783 |
<span id="cb12-16"><a href="#cb12-16" tabindex="-1"></a></span> |
| 784 |
<span id="cb12-17"><a href="#cb12-17" tabindex="-1"></a><span class="co"># The other possibilities:</span></span> |
| 785 |
<span id="cb12-18"><a href="#cb12-18" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgroup_vars</span>(<span class="st">"unique"</span>) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 786 |
<span id="cb12-19"><a href="#cb12-19" tabindex="-1"></a><span class="co"># # A tibble: 3 × 2</span></span> |
| 787 |
<span id="cb12-20"><a href="#cb12-20" tabindex="-1"></a><span class="co"># Variable Country</span></span> |
| 788 |
<span id="cb12-21"><a href="#cb12-21" tabindex="-1"></a><span class="co"># <chr> <chr> </span></span> |
| 789 |
<span id="cb12-22"><a href="#cb12-22" tabindex="-1"></a><span class="co"># 1 EMP ARG </span></span> |
| 790 |
<span id="cb12-23"><a href="#cb12-23" tabindex="-1"></a><span class="co"># 2 EMP BOL </span></span> |
| 791 |
<span id="cb12-24"><a href="#cb12-24" tabindex="-1"></a><span class="co"># 3 EMP BRA</span></span> |
| 792 |
<span id="cb12-25"><a href="#cb12-25" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgroup_vars</span>(<span class="st">"names"</span>)</span> |
| 793 |
<span id="cb12-26"><a href="#cb12-26" tabindex="-1"></a><span class="co"># [1] "Variable" "Country"</span></span> |
| 794 |
<span id="cb12-27"><a href="#cb12-27" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgroup_vars</span>(<span class="st">"indices"</span>)</span> |
| 795 |
<span id="cb12-28"><a href="#cb12-28" tabindex="-1"></a><span class="co"># [1] 4 1</span></span> |
| 796 |
<span id="cb12-29"><a href="#cb12-29" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgroup_vars</span>(<span class="st">"named_indices"</span>)</span> |
| 797 |
<span id="cb12-30"><a href="#cb12-30" tabindex="-1"></a><span class="co"># Variable Country </span></span> |
| 798 |
<span id="cb12-31"><a href="#cb12-31" tabindex="-1"></a><span class="co"># 4 1</span></span> |
| 799 |
<span id="cb12-32"><a href="#cb12-32" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgroup_vars</span>(<span class="st">"logical"</span>)</span> |
| 800 |
<span id="cb12-33"><a href="#cb12-33" tabindex="-1"></a><span class="co"># [1] TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE</span></span> |
| 801 |
<span id="cb12-34"><a href="#cb12-34" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgroup_vars</span>(<span class="st">"named_logical"</span>)</span> |
| 802 |
<span id="cb12-35"><a href="#cb12-35" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU </span></span> |
| 803 |
<span id="cb12-36"><a href="#cb12-36" tabindex="-1"></a><span class="co"># TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE </span></span> |
| 804 |
<span id="cb12-37"><a href="#cb12-37" tabindex="-1"></a><span class="co"># CON WRT TRA FIRE GOV OTH SUM </span></span> |
| 805 |
<span id="cb12-38"><a href="#cb12-38" tabindex="-1"></a><span class="co"># FALSE FALSE FALSE FALSE FALSE FALSE FALSE</span></span></code></pre></div> |
| 806 |
<p>Another <em>collapse</em> verb to mention here is |
| 807 |
<code>fsubset</code>, a faster alternative to <code>dplyr::filter</code> |
| 808 |
which also provides an option to flexibly subset columns after the |
| 809 |
select argument:</p> |
| 810 |
<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="co"># Two equivalent calls, the first is substantially faster</span></span> |
| 811 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fsubset</span>(Variable <span class="sc">==</span> <span class="st">"VA"</span> <span class="sc">&</span> Year <span class="sc">></span> <span class="dv">1990</span>, Country, Year, AGR<span class="sc">:</span>GOV) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 812 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="co"># # A tibble: 3 × 11</span></span> |
| 813 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="co"># Country Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 814 |
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co"># <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 815 |
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co"># 1 BWA 1991 303. 2647. 473. 161. 580. 807. 233. 433. 1073.</span></span> |
| 816 |
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a><span class="co"># 2 BWA 1992 333. 2691. 537. 178. 679. 725. 285. 517. 1234.</span></span> |
| 817 |
<span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a><span class="co"># 3 BWA 1993 405. 2625. 567. 219. 634. 772. 350. 673. 1487.</span></span> |
| 818 |
<span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a></span> |
| 819 |
<span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">filter</span>(Variable <span class="sc">==</span> <span class="st">"VA"</span> <span class="sc">&</span> Year <span class="sc">></span> <span class="dv">1990</span>) <span class="sc">%>%</span> <span class="fu">select</span>(Country, Year, AGR<span class="sc">:</span>GOV) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 820 |
<span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a><span class="co"># # A tibble: 3 × 11</span></span> |
| 821 |
<span id="cb13-12"><a href="#cb13-12" tabindex="-1"></a><span class="co"># Country Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 822 |
<span id="cb13-13"><a href="#cb13-13" tabindex="-1"></a><span class="co"># <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 823 |
<span id="cb13-14"><a href="#cb13-14" tabindex="-1"></a><span class="co"># 1 BWA 1991 303. 2647. 473. 161. 580. 807. 233. 433. 1073.</span></span> |
| 824 |
<span id="cb13-15"><a href="#cb13-15" tabindex="-1"></a><span class="co"># 2 BWA 1992 333. 2691. 537. 178. 679. 725. 285. 517. 1234.</span></span> |
| 825 |
<span id="cb13-16"><a href="#cb13-16" tabindex="-1"></a><span class="co"># 3 BWA 1993 405. 2625. 567. 219. 634. 772. 350. 673. 1487.</span></span></code></pre></div> |
| 826 |
<p><em>collapse</em> also offers <code>roworder</code>, |
| 827 |
<code>frename</code>, <code>colorder</code> and |
| 828 |
<code>ftransform</code>/<code>TRA</code> as fast replacements for |
| 829 |
<code>dplyr::arrange</code>, <code>dplyr::rename</code>, |
| 830 |
<code>dplyr::relocate</code> and <code>dplyr::mutate</code>.</p> |
| 831 |
</div> |
| 832 |
<div id="multi-function-aggregations" class="section level3"> |
| 833 |
<h3>1.3 Multi-Function Aggregations</h3> |
| 834 |
<p>One can also aggregate with multiple functions at the same time. For |
| 835 |
such operations it is often necessary to use curly braces <code>{</code> |
| 836 |
to prevent first argument injection so that |
| 837 |
<code>%>% cbind(FUN1(.), FUN2(.))</code> does not evaluate as |
| 838 |
<code>%>% cbind(., FUN1(.), FUN2(.))</code>:</p> |
| 839 |
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 840 |
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 841 |
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> {</span> |
| 842 |
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a> <span class="fu">cbind</span>(<span class="fu">fmedian</span>(.),</span> |
| 843 |
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a> <span class="fu">add_stub</span>(<span class="fu">fmean</span>(., <span class="at">keep.group_vars =</span> <span class="cn">FALSE</span>), <span class="st">"mean_"</span>))</span> |
| 844 |
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a> } <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 845 |
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA</span></span> |
| 846 |
<span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="co"># 1 EMP ARG 1324.5255 47.35255 1987.5912 104.738825 782.40283 1854.612 579.93982</span></span> |
| 847 |
<span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="co"># 2 EMP BOL 943.1612 53.53538 167.1502 4.457895 65.97904 132.225 96.96828</span></span> |
| 848 |
<span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="co"># 3 EMP BRA 17480.9810 225.43693 7207.7915 375.851832 4054.66103 6454.523 1580.81120</span></span> |
| 849 |
<span id="cb14-11"><a href="#cb14-11" tabindex="-1"></a><span class="co"># FIRE GOV OTH SUM mean_AGR mean_MIN mean_MAN mean_PU mean_CON</span></span> |
| 850 |
<span id="cb14-12"><a href="#cb14-12" tabindex="-1"></a><span class="co"># 1 464.39920 1738.836 866.1119 9743.223 1419.8013 52.08903 1931.7602 101.720936 742.4044</span></span> |
| 851 |
<span id="cb14-13"><a href="#cb14-13" tabindex="-1"></a><span class="co"># 2 15.34259 NA 384.0678 1842.055 964.2103 56.03295 235.0332 5.346433 122.7827</span></span> |
| 852 |
<span id="cb14-14"><a href="#cb14-14" tabindex="-1"></a><span class="co"># 3 4354.86210 4449.942 4478.6927 51881.110 17191.3529 206.02389 6991.3710 364.573404 3524.7384</span></span> |
| 853 |
<span id="cb14-15"><a href="#cb14-15" tabindex="-1"></a><span class="co"># mean_WRT mean_TRA mean_FIRE mean_GOV mean_OTH mean_SUM</span></span> |
| 854 |
<span id="cb14-16"><a href="#cb14-16" tabindex="-1"></a><span class="co"># 1 1982.1775 648.5119 627.79291 2043.471 992.4475 10542.177</span></span> |
| 855 |
<span id="cb14-17"><a href="#cb14-17" tabindex="-1"></a><span class="co"># 2 281.5164 115.4728 44.56442 NA 395.5650 2220.524</span></span> |
| 856 |
<span id="cb14-18"><a href="#cb14-18" tabindex="-1"></a><span class="co"># 3 8509.4612 2054.3731 4413.54448 5307.280 5710.2665 54272.985</span></span></code></pre></div> |
| 857 |
<p>The function <code>add_stub</code> used above is a <em>collapse</em> |
| 858 |
function adding a prefix (default) or suffix to variables names. The |
| 859 |
<em>collapse</em> predicate <code>add_vars</code> provides a more |
| 860 |
efficient alternative to <code>cbind.data.frame</code>. The idea here is |
| 861 |
‘adding’ variables to the data.frame in the first argument i.e. the |
| 862 |
attributes of the first argument are preserved, so the expression below |
| 863 |
still gives a tibble instead of a data.frame:</p> |
| 864 |
<!-- A slightly more elegant solution to such multi-function aggregations can be found using `get_vars`, a collapse predicate to efficiently select variables. In contrast to `select_at`, `get_vars` does not automatically add the grouping columns to the selection. --> |
| 865 |
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 866 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> {</span> |
| 867 |
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> <span class="fu">add_vars</span>(<span class="fu">get_vars</span>(., <span class="st">"Reg"</span>, <span class="at">regex =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span> ffirst, <span class="co"># Regular expression matching column names</span></span> |
| 868 |
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a> <span class="fu">num_vars</span>(.) <span class="sc">%>%</span> <span class="fu">fmean</span>(<span class="at">keep.group_vars =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> <span class="fu">add_stub</span>(<span class="st">"mean_"</span>), <span class="co"># num_vars selects all numeric variables</span></span> |
| 869 |
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a> <span class="fu">fselect</span>(., PU<span class="sc">:</span>TRA) <span class="sc">%>%</span> <span class="fu">fmedian</span>(<span class="at">keep.group_vars =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> <span class="fu">add_stub</span>(<span class="st">"median_"</span>), </span> |
| 870 |
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a> <span class="fu">fselect</span>(., PU<span class="sc">:</span>CON) <span class="sc">%>%</span> <span class="fu">fmin</span>(<span class="at">keep.group_vars =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> <span class="fu">add_stub</span>(<span class="st">"min_"</span>)) </span> |
| 871 |
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a> } <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 872 |
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a><span class="co"># # A tibble: 3 × 22</span></span> |
| 873 |
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a><span class="co"># Variable Country Regioncode Region mean_Year mean_AGR mean_MIN mean_MAN mean_PU mean_CON mean_WRT</span></span> |
| 874 |
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 875 |
<span id="cb15-11"><a href="#cb15-11" tabindex="-1"></a><span class="co"># 1 EMP ARG LAM Latin … 1980. 1420. 52.1 1932. 102. 742. 1982.</span></span> |
| 876 |
<span id="cb15-12"><a href="#cb15-12" tabindex="-1"></a><span class="co"># 2 EMP BOL LAM Latin … 1980 964. 56.0 235. 5.35 123. 282.</span></span> |
| 877 |
<span id="cb15-13"><a href="#cb15-13" tabindex="-1"></a><span class="co"># 3 EMP BRA LAM Latin … 1980. 17191. 206. 6991. 365. 3525. 8509.</span></span> |
| 878 |
<span id="cb15-14"><a href="#cb15-14" tabindex="-1"></a><span class="co"># # ℹ 11 more variables: mean_TRA <dbl>, mean_FIRE <dbl>, mean_GOV <dbl>, mean_OTH <dbl>,</span></span> |
| 879 |
<span id="cb15-15"><a href="#cb15-15" tabindex="-1"></a><span class="co"># # mean_SUM <dbl>, median_PU <dbl>, median_CON <dbl>, median_WRT <dbl>, median_TRA <dbl>,</span></span> |
| 880 |
<span id="cb15-16"><a href="#cb15-16" tabindex="-1"></a><span class="co"># # min_PU <dbl>, min_CON <dbl></span></span></code></pre></div> |
| 881 |
<p>Another nice feature of <code>add_vars</code> is that it can also |
| 882 |
very efficiently reorder columns i.e. bind columns in a different order |
| 883 |
than they are passed. This can be done by simply specifying the |
| 884 |
positions the added columns should have in the final data frame, and |
| 885 |
then <code>add_vars</code> shifts the first argument columns to the |
| 886 |
right to fill in the gaps.</p> |
| 887 |
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 888 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> <span class="fu">fsubset</span>(Variable <span class="sc">==</span> <span class="st">"VA"</span>, Country, AGR, SUM) <span class="sc">%>%</span> </span> |
| 889 |
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Country) <span class="sc">%>%</span> {</span> |
| 890 |
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a> <span class="fu">add_vars</span>(<span class="fu">fgroup_vars</span>(.,<span class="st">"unique"</span>),</span> |
| 891 |
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a> <span class="fu">fmean</span>(., <span class="at">keep.group_vars =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> <span class="fu">add_stub</span>(<span class="st">"mean_"</span>),</span> |
| 892 |
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a> <span class="fu">fsd</span>(., <span class="at">keep.group_vars =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> <span class="fu">add_stub</span>(<span class="st">"sd_"</span>), </span> |
| 893 |
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a> <span class="at">pos =</span> <span class="fu">c</span>(<span class="dv">2</span>,<span class="dv">4</span>,<span class="dv">3</span>,<span class="dv">5</span>))</span> |
| 894 |
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a> } <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 895 |
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a><span class="co"># # A tibble: 3 × 5</span></span> |
| 896 |
<span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a><span class="co"># Country mean_AGR sd_AGR mean_SUM sd_SUM</span></span> |
| 897 |
<span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a><span class="co"># <chr> <dbl> <dbl> <dbl> <dbl></span></span> |
| 898 |
<span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a><span class="co"># 1 ARG 14951. 33061. 152534. 301316.</span></span> |
| 899 |
<span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a><span class="co"># 2 BOL 3300. 4456. 22619. 33173.</span></span> |
| 900 |
<span id="cb16-14"><a href="#cb16-14" tabindex="-1"></a><span class="co"># 3 BRA 76870. 59442. 1200563. 976963.</span></span></code></pre></div> |
| 901 |
<p>A much more compact solution to multi-function and multi-type |
| 902 |
aggregation is offered by the function <em>collapg</em>:</p> |
| 903 |
<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="co"># This aggregates numeric colums using the mean (fmean) and categorical columns with the mode (fmode)</span></span> |
| 904 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> collapg <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 905 |
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a><span class="co"># # A tibble: 3 × 16</span></span> |
| 906 |
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a><span class="co"># Variable Country Regioncode Region Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 907 |
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 908 |
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co"># 1 EMP ARG LAM Latin … 1980. 1420. 52.1 1932. 102. 742. 1982. 649. 628. 2043.</span></span> |
| 909 |
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a><span class="co"># 2 EMP BOL LAM Latin … 1980 964. 56.0 235. 5.35 123. 282. 115. 44.6 NA </span></span> |
| 910 |
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a><span class="co"># 3 EMP BRA LAM Latin … 1980. 17191. 206. 6991. 365. 3525. 8509. 2054. 4414. 5307.</span></span> |
| 911 |
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a><span class="co"># # ℹ 2 more variables: OTH <dbl>, SUM <dbl></span></span></code></pre></div> |
| 912 |
<p>By default it aggregates numeric columns using the <code>fmean</code> |
| 913 |
and categorical columns using <code>fmode</code>, and preserves the |
| 914 |
order of all columns. Changing these defaults is very easy:</p> |
| 915 |
<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="co"># This aggregates numeric colums using the median and categorical columns using the first value</span></span> |
| 916 |
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">collapg</span>(fmedian, flast) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 917 |
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="co"># # A tibble: 3 × 16</span></span> |
| 918 |
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a><span class="co"># Variable Country Regioncode Region Year AGR MIN MAN PU CON WRT TRA FIRE</span></span> |
| 919 |
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 920 |
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a><span class="co"># 1 EMP ARG LAM Latin Amer… 1980. 1325. 47.4 1988. 105. 782. 1855. 580. 464. </span></span> |
| 921 |
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="co"># 2 EMP BOL LAM Latin Amer… 1980 943. 53.5 167. 4.46 66.0 132. 97.0 15.3</span></span> |
| 922 |
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a><span class="co"># 3 EMP BRA LAM Latin Amer… 1980. 17481. 225. 7208. 376. 4055. 6455. 1581. 4355. </span></span> |
| 923 |
<span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a><span class="co"># # ℹ 3 more variables: GOV <dbl>, OTH <dbl>, SUM <dbl></span></span></code></pre></div> |
| 924 |
<p>One can apply multiple functions to both numeric and/or categorical |
| 925 |
data:</p> |
| 926 |
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 927 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a> <span class="fu">collapg</span>(<span class="fu">list</span>(fmean, fmedian), <span class="fu">list</span>(first, fmode, flast)) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 928 |
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a><span class="co"># # A tibble: 3 × 32</span></span> |
| 929 |
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="co"># Variable Country first.Regioncode fmode.Regioncode flast.Regioncode first.Region fmode.Region </span></span> |
| 930 |
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <chr> <chr> <chr> </span></span> |
| 931 |
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a><span class="co"># 1 EMP ARG LAM LAM LAM Latin America Latin America</span></span> |
| 932 |
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a><span class="co"># 2 EMP BOL LAM LAM LAM Latin America Latin America</span></span> |
| 933 |
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a><span class="co"># 3 EMP BRA LAM LAM LAM Latin America Latin America</span></span> |
| 934 |
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a><span class="co"># # ℹ 25 more variables: flast.Region <chr>, fmean.Year <dbl>, fmedian.Year <dbl>, fmean.AGR <dbl>,</span></span> |
| 935 |
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a><span class="co"># # fmedian.AGR <dbl>, fmean.MIN <dbl>, fmedian.MIN <dbl>, fmean.MAN <dbl>, fmedian.MAN <dbl>,</span></span> |
| 936 |
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a><span class="co"># # fmean.PU <dbl>, fmedian.PU <dbl>, fmean.CON <dbl>, fmedian.CON <dbl>, fmean.WRT <dbl>,</span></span> |
| 937 |
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a><span class="co"># # fmedian.WRT <dbl>, fmean.TRA <dbl>, fmedian.TRA <dbl>, fmean.FIRE <dbl>, fmedian.FIRE <dbl>,</span></span> |
| 938 |
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a><span class="co"># # fmean.GOV <dbl>, fmedian.GOV <dbl>, fmean.OTH <dbl>, fmedian.OTH <dbl>, fmean.SUM <dbl>,</span></span> |
| 939 |
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a><span class="co"># # fmedian.SUM <dbl></span></span></code></pre></div> |
| 940 |
<p>Applying multiple functions to only numeric (or only categorical) |
| 941 |
data allows return in a long format:</p> |
| 942 |
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 943 |
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a> <span class="fu">collapg</span>(<span class="fu">list</span>(fmean, fmedian), <span class="at">cols =</span> is.numeric, <span class="at">return =</span> <span class="st">"long"</span>) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 944 |
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="co"># # A tibble: 3 × 15</span></span> |
| 945 |
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a><span class="co"># Function Variable Country Year AGR MIN MAN PU CON WRT TRA FIRE GOV OTH</span></span> |
| 946 |
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 947 |
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a><span class="co"># 1 fmean EMP ARG 1980. 1420. 52.1 1932. 102. 742. 1982. 649. 628. 2043. 992.</span></span> |
| 948 |
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a><span class="co"># 2 fmean EMP BOL 1980 964. 56.0 235. 5.35 123. 282. 115. 44.6 NA 396.</span></span> |
| 949 |
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a><span class="co"># 3 fmean EMP BRA 1980. 17191. 206. 6991. 365. 3525. 8509. 2054. 4414. 5307. 5710.</span></span> |
| 950 |
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a><span class="co"># # ℹ 1 more variable: SUM <dbl></span></span></code></pre></div> |
| 951 |
<p>Finally, <code>collapg</code> also makes it very easy to apply |
| 952 |
aggregator functions to certain columns only:</p> |
| 953 |
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 954 |
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a> <span class="fu">collapg</span>(<span class="at">custom =</span> <span class="fu">list</span>(<span class="at">fmean =</span> <span class="dv">6</span><span class="sc">:</span><span class="dv">8</span>, <span class="at">fmedian =</span> <span class="dv">10</span><span class="sc">:</span><span class="dv">12</span>)) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 955 |
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a><span class="co"># # A tibble: 3 × 8</span></span> |
| 956 |
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN CON WRT TRA</span></span> |
| 957 |
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 958 |
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a><span class="co"># 1 EMP ARG 1420. 52.1 1932. 782. 1855. 580. </span></span> |
| 959 |
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="co"># 2 EMP BOL 964. 56.0 235. 66.0 132. 97.0</span></span> |
| 960 |
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="co"># 3 EMP BRA 17191. 206. 6991. 4055. 6455. 1581.</span></span></code></pre></div> |
| 961 |
<p>To understand more about <code>collapg</code>, look it up in the |
| 962 |
documentation (<code>?collapg</code>).</p> |
| 963 |
</div> |
| 964 |
<div id="weighted-aggregations" class="section level3"> |
| 965 |
<h3>1.4 Weighted Aggregations</h3> |
| 966 |
<p>Weighted aggregations are possible with the functions |
| 967 |
<code>fsum, fprod, fmean, fmedian, fnth, fmode, fvar</code> and |
| 968 |
<code>fsd</code>. The implementation is such that by default (option |
| 969 |
<code>keep.w = TRUE</code>) these functions also aggregate the weights, |
| 970 |
so that further weighted computations can be performed on the aggregated |
| 971 |
data. <code>fprod</code> saves the product of the weights, whereas the |
| 972 |
other functions save the sum of the weights in a column next to the |
| 973 |
grouping variables. If <code>na.rm = TRUE</code> (the default), rows |
| 974 |
with missing weights are omitted from the computation.</p> |
| 975 |
<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="co"># This computes a frequency-weighted grouped standard-deviation, taking the total EMP / VA as weight</span></span> |
| 976 |
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 977 |
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 978 |
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a> <span class="fu">fselect</span>(AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> <span class="fu">fsd</span>(SUM) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 979 |
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="co"># # A tibble: 3 × 13</span></span> |
| 980 |
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a><span class="co"># Variable Country sum.SUM AGR MIN MAN PU CON WRT TRA FIRE GOV OTH</span></span> |
| 981 |
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 982 |
<span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a><span class="co"># 1 EMP ARG 653615. 225. 22.2 176. 20.5 285. 856. 195. 493. 1123. 506.</span></span> |
| 983 |
<span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a><span class="co"># 2 EMP BOL 135452. 99.7 17.1 168. 4.87 123. 324. 98.1 69.8 NA 258.</span></span> |
| 984 |
<span id="cb22-10"><a href="#cb22-10" tabindex="-1"></a><span class="co"># 3 EMP BRA 3364925. 1587. 73.8 2952. 93.8 1861. 6285. 1306. 3003. 3621. 4257.</span></span> |
| 985 |
<span id="cb22-11"><a href="#cb22-11" tabindex="-1"></a></span> |
| 986 |
<span id="cb22-12"><a href="#cb22-12" tabindex="-1"></a><span class="co"># This computes a weighted grouped mode, taking the total EMP / VA as weight</span></span> |
| 987 |
<span id="cb22-13"><a href="#cb22-13" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 988 |
<span id="cb22-14"><a href="#cb22-14" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 989 |
<span id="cb22-15"><a href="#cb22-15" tabindex="-1"></a> <span class="fu">fselect</span>(AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> <span class="fu">fmode</span>(SUM) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 990 |
<span id="cb22-16"><a href="#cb22-16" tabindex="-1"></a><span class="co"># # A tibble: 3 × 13</span></span> |
| 991 |
<span id="cb22-17"><a href="#cb22-17" tabindex="-1"></a><span class="co"># Variable Country sum.SUM AGR MIN MAN PU CON WRT TRA FIRE GOV OTH</span></span> |
| 992 |
<span id="cb22-18"><a href="#cb22-18" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 993 |
<span id="cb22-19"><a href="#cb22-19" tabindex="-1"></a><span class="co"># 1 EMP ARG 653615. 1162. 127. 2164. 152. 1415. 3768. 1060. 1748. 4336. 1999.</span></span> |
| 994 |
<span id="cb22-20"><a href="#cb22-20" tabindex="-1"></a><span class="co"># 2 EMP BOL 135452. 819. 37.6 604. 10.8 433. 893. 333. 321. NA 1057.</span></span> |
| 995 |
<span id="cb22-21"><a href="#cb22-21" tabindex="-1"></a><span class="co"># 3 EMP BRA 3364925. 16451. 313. 11841. 388. 8154. 21860. 5169. 12011. 12149. 14235.</span></span></code></pre></div> |
| 996 |
<p>The weighted variance / standard deviation is currently only |
| 997 |
implemented with frequency weights. |
| 998 |
<!-- Reliability weights may be implemented in a future update of *collapse*, if this is a strongly requested feature. --></p> |
| 999 |
<p>Weighted aggregations may also be performed with |
| 1000 |
<code>collapg</code>. By default <code>fsum</code> is used to compute a |
| 1001 |
sum of the weights, but it is also possible here to aggregate the |
| 1002 |
weights with other functions:</p> |
| 1003 |
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a><span class="co"># This aggregates numeric colums using the weighted mean (the default) and categorical columns using the weighted mode (the default).</span></span> |
| 1004 |
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="co"># Weights (column SUM) are aggregated using both the sum and the maximum. </span></span> |
| 1005 |
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">group_by</span>(Variable, Country) <span class="sc">%>%</span> </span> |
| 1006 |
<span id="cb23-4"><a href="#cb23-4" tabindex="-1"></a> <span class="fu">collapg</span>(<span class="at">w =</span> SUM, <span class="at">wFUN =</span> <span class="fu">list</span>(fsum, fmax)) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 1007 |
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a><span class="co"># # A tibble: 3 × 17</span></span> |
| 1008 |
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="co"># Variable Country fsum.SUM fmax.SUM Regioncode Region Year AGR MIN MAN PU CON WRT</span></span> |
| 1009 |
<span id="cb23-7"><a href="#cb23-7" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1010 |
<span id="cb23-8"><a href="#cb23-8" tabindex="-1"></a><span class="co"># 1 EMP ARG 653615. 17929. LAM Latin … 1985. 1361. 56.5 1935. 105. 811. 2217.</span></span> |
| 1011 |
<span id="cb23-9"><a href="#cb23-9" tabindex="-1"></a><span class="co"># 2 EMP BOL 135452. 4508. LAM Latin … 1987. 977. 57.9 296. 7.07 167. 400.</span></span> |
| 1012 |
<span id="cb23-10"><a href="#cb23-10" tabindex="-1"></a><span class="co"># 3 EMP BRA 3364925. 102572. LAM Latin … 1989. 17746. 238. 8466. 389. 4436. 11376.</span></span> |
| 1013 |
<span id="cb23-11"><a href="#cb23-11" tabindex="-1"></a><span class="co"># # ℹ 4 more variables: TRA <dbl>, FIRE <dbl>, GOV <dbl>, OTH <dbl></span></span></code></pre></div> |
| 1014 |
<!-- Thus to aggregate the entire data and save the weights one would need to opt for a manual solution: --> |
| 1015 |
<!-- ```{r} --> |
| 1016 |
<!-- GGDC10S %>% --> |
| 1017 |
<!-- fgroup_by(Variable, Country) %>% { --> |
| 1018 |
<!-- add_vars(fmean(get_vars(., 6:16), SUM), --> |
| 1019 |
<!-- fmode(get_vars(., c(2:3,16)), SUM, keep.group_vars = FALSE), --> |
| 1020 |
<!-- pos = c(5, 2:3)) --> |
| 1021 |
<!-- } --> |
| 1022 |
<!-- ``` --> |
| 1023 |
<!-- <!-- ```{r} --> |
| 1024 |
<!-- <!-- GGDC10S %>% --> |
| 1025 |
<!-- <!-- group_by(Variable, Country) %>% collapg(w = .$SUM) --> |
| 1026 |
<!-- ``` --> |
| 1027 |
</div> |
| 1028 |
</div> |
| 1029 |
<div id="fast-transformations" class="section level2"> |
| 1030 |
<h2>2. Fast Transformations</h2> |
| 1031 |
<p><em>collapse</em> also provides some fast transformations that |
| 1032 |
significantly extend the scope and speed of manipulations that can be |
| 1033 |
performed with <code>dplyr::mutate</code>. |
| 1034 |
<!-- bring to *dplyr* in terms of grouped transformations. --></p> |
| 1035 |
<div id="fast-transform-and-compute-variables" class="section level3"> |
| 1036 |
<h3>2.1 Fast Transform and Compute Variables</h3> |
| 1037 |
<p>The function <code>ftransform</code> can be used to manipulate |
| 1038 |
columns in the same ways as <code>mutate</code>:</p> |
| 1039 |
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fsubset</span>(Variable <span class="sc">==</span> <span class="st">"VA"</span>, Country, Year, AGR, SUM) <span class="sc">%>%</span></span> |
| 1040 |
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a> <span class="fu">ftransform</span>(<span class="at">AGR_perc =</span> AGR <span class="sc">/</span> SUM <span class="sc">*</span> <span class="dv">100</span>, <span class="co"># Computing % of VA in Agriculture</span></span> |
| 1041 |
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a> <span class="at">AGR_mean =</span> <span class="fu">fmean</span>(AGR), <span class="co"># Average Agricultural VA</span></span> |
| 1042 |
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a> <span class="at">AGR =</span> <span class="cn">NULL</span>, <span class="at">SUM =</span> <span class="cn">NULL</span>) <span class="sc">%>%</span> <span class="co"># Deleting columns AGR and SUM</span></span> |
| 1043 |
<span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a> head</span> |
| 1044 |
<span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a><span class="co"># # A tibble: 6 × 4</span></span> |
| 1045 |
<span id="cb24-7"><a href="#cb24-7" tabindex="-1"></a><span class="co"># Country Year AGR_perc AGR_mean</span></span> |
| 1046 |
<span id="cb24-8"><a href="#cb24-8" tabindex="-1"></a><span class="co"># <chr> <dbl> <dbl> <dbl></span></span> |
| 1047 |
<span id="cb24-9"><a href="#cb24-9" tabindex="-1"></a><span class="co"># 1 BWA 1960 NA 5137561.</span></span> |
| 1048 |
<span id="cb24-10"><a href="#cb24-10" tabindex="-1"></a><span class="co"># 2 BWA 1961 NA 5137561.</span></span> |
| 1049 |
<span id="cb24-11"><a href="#cb24-11" tabindex="-1"></a><span class="co"># 3 BWA 1962 NA 5137561.</span></span> |
| 1050 |
<span id="cb24-12"><a href="#cb24-12" tabindex="-1"></a><span class="co"># 4 BWA 1963 NA 5137561.</span></span> |
| 1051 |
<span id="cb24-13"><a href="#cb24-13" tabindex="-1"></a><span class="co"># 5 BWA 1964 43.5 5137561.</span></span> |
| 1052 |
<span id="cb24-14"><a href="#cb24-14" tabindex="-1"></a><span class="co"># 6 BWA 1965 40.0 5137561.</span></span></code></pre></div> |
| 1053 |
<p>The modification brought by <code>ftransformv</code> enables |
| 1054 |
transformations of groups of columns like <code>dplyr::mutate_at</code> |
| 1055 |
and <code>dplyr::mutate_if</code>:</p> |
| 1056 |
<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="co"># This replaces variables mpg, carb and wt by their log (.c turns expressions into character vectors)</span></span> |
| 1057 |
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a>mtcars <span class="sc">%>%</span> <span class="fu">ftransformv</span>(<span class="fu">.c</span>(mpg, carb, wt), log) <span class="sc">%>%</span> head</span> |
| 1058 |
<span id="cb25-3"><a href="#cb25-3" tabindex="-1"></a><span class="co"># mpg cyl disp hp drat wt qsec vs am gear carb</span></span> |
| 1059 |
<span id="cb25-4"><a href="#cb25-4" tabindex="-1"></a><span class="co"># Mazda RX4 3.044522 6 160 110 3.90 0.9631743 16.46 0 1 4 1.3862944</span></span> |
| 1060 |
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a><span class="co"># Mazda RX4 Wag 3.044522 6 160 110 3.90 1.0560527 17.02 0 1 4 1.3862944</span></span> |
| 1061 |
<span id="cb25-6"><a href="#cb25-6" tabindex="-1"></a><span class="co"># Datsun 710 3.126761 4 108 93 3.85 0.8415672 18.61 1 1 4 0.0000000</span></span> |
| 1062 |
<span id="cb25-7"><a href="#cb25-7" tabindex="-1"></a><span class="co"># Hornet 4 Drive 3.063391 6 258 110 3.08 1.1678274 19.44 1 0 3 0.0000000</span></span> |
| 1063 |
<span id="cb25-8"><a href="#cb25-8" tabindex="-1"></a><span class="co"># Hornet Sportabout 2.928524 8 360 175 3.15 1.2354715 17.02 0 0 3 0.6931472</span></span> |
| 1064 |
<span id="cb25-9"><a href="#cb25-9" tabindex="-1"></a><span class="co"># Valiant 2.895912 6 225 105 2.76 1.2412686 20.22 1 0 3 0.0000000</span></span> |
| 1065 |
<span id="cb25-10"><a href="#cb25-10" tabindex="-1"></a></span> |
| 1066 |
<span id="cb25-11"><a href="#cb25-11" tabindex="-1"></a><span class="co"># Logging numeric variables</span></span> |
| 1067 |
<span id="cb25-12"><a href="#cb25-12" tabindex="-1"></a>iris <span class="sc">%>%</span> <span class="fu">ftransformv</span>(is.numeric, log) <span class="sc">%>%</span> head</span> |
| 1068 |
<span id="cb25-13"><a href="#cb25-13" tabindex="-1"></a><span class="co"># Sepal.Length Sepal.Width Petal.Length Petal.Width Species</span></span> |
| 1069 |
<span id="cb25-14"><a href="#cb25-14" tabindex="-1"></a><span class="co"># 1 1.629241 1.252763 0.3364722 -1.6094379 setosa</span></span> |
| 1070 |
<span id="cb25-15"><a href="#cb25-15" tabindex="-1"></a><span class="co"># 2 1.589235 1.098612 0.3364722 -1.6094379 setosa</span></span> |
| 1071 |
<span id="cb25-16"><a href="#cb25-16" tabindex="-1"></a><span class="co"># 3 1.547563 1.163151 0.2623643 -1.6094379 setosa</span></span> |
| 1072 |
<span id="cb25-17"><a href="#cb25-17" tabindex="-1"></a><span class="co"># 4 1.526056 1.131402 0.4054651 -1.6094379 setosa</span></span> |
| 1073 |
<span id="cb25-18"><a href="#cb25-18" tabindex="-1"></a><span class="co"># 5 1.609438 1.280934 0.3364722 -1.6094379 setosa</span></span> |
| 1074 |
<span id="cb25-19"><a href="#cb25-19" tabindex="-1"></a><span class="co"># 6 1.686399 1.360977 0.5306283 -0.9162907 setosa</span></span></code></pre></div> |
| 1075 |
<p>Instead of <code>column = value</code> type arguments, it is also |
| 1076 |
possible to pass a single list of transformed variables to |
| 1077 |
<code>ftransform</code>, which will be regarded in the same way as an |
| 1078 |
evaluated list of <code>column = value</code> arguments. It can be used |
| 1079 |
for more complex transformations:</p> |
| 1080 |
<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="co"># Logging values and replacing generated Inf values</span></span> |
| 1081 |
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a>mtcars <span class="sc">%>%</span> <span class="fu">ftransform</span>(<span class="fu">fselect</span>(., mpg, cyl, vs<span class="sc">:</span>gear) <span class="sc">%>%</span> <span class="fu">lapply</span>(log) <span class="sc">%>%</span> replace_Inf) <span class="sc">%>%</span> head</span> |
| 1082 |
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a><span class="co"># mpg cyl disp hp drat wt qsec vs am gear carb</span></span> |
| 1083 |
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a><span class="co"># Mazda RX4 3.044522 1.791759 160 110 3.90 2.620 16.46 NA 0 1.386294 4</span></span> |
| 1084 |
<span id="cb26-5"><a href="#cb26-5" tabindex="-1"></a><span class="co"># Mazda RX4 Wag 3.044522 1.791759 160 110 3.90 2.875 17.02 NA 0 1.386294 4</span></span> |
| 1085 |
<span id="cb26-6"><a href="#cb26-6" tabindex="-1"></a><span class="co"># Datsun 710 3.126761 1.386294 108 93 3.85 2.320 18.61 0 0 1.386294 1</span></span> |
| 1086 |
<span id="cb26-7"><a href="#cb26-7" tabindex="-1"></a><span class="co"># Hornet 4 Drive 3.063391 1.791759 258 110 3.08 3.215 19.44 0 NA 1.098612 1</span></span> |
| 1087 |
<span id="cb26-8"><a href="#cb26-8" tabindex="-1"></a><span class="co"># Hornet Sportabout 2.928524 2.079442 360 175 3.15 3.440 17.02 NA NA 1.098612 2</span></span> |
| 1088 |
<span id="cb26-9"><a href="#cb26-9" tabindex="-1"></a><span class="co"># Valiant 2.895912 1.791759 225 105 2.76 3.460 20.22 0 NA 1.098612 1</span></span></code></pre></div> |
| 1089 |
<p>If only the computed columns need to be returned, |
| 1090 |
<code>fcompute</code> provides an efficient alternative:</p> |
| 1091 |
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">fsubset</span>(Variable <span class="sc">==</span> <span class="st">"VA"</span>, Country, Year, AGR, SUM) <span class="sc">%>%</span></span> |
| 1092 |
<span id="cb27-2"><a href="#cb27-2" tabindex="-1"></a> <span class="fu">fcompute</span>(<span class="at">AGR_perc =</span> AGR <span class="sc">/</span> SUM <span class="sc">*</span> <span class="dv">100</span>,</span> |
| 1093 |
<span id="cb27-3"><a href="#cb27-3" tabindex="-1"></a> <span class="at">AGR_mean =</span> <span class="fu">fmean</span>(AGR)) <span class="sc">%>%</span> head</span> |
| 1094 |
<span id="cb27-4"><a href="#cb27-4" tabindex="-1"></a><span class="co"># # A tibble: 6 × 2</span></span> |
| 1095 |
<span id="cb27-5"><a href="#cb27-5" tabindex="-1"></a><span class="co"># AGR_perc AGR_mean</span></span> |
| 1096 |
<span id="cb27-6"><a href="#cb27-6" tabindex="-1"></a><span class="co"># <dbl> <dbl></span></span> |
| 1097 |
<span id="cb27-7"><a href="#cb27-7" tabindex="-1"></a><span class="co"># 1 NA 5137561.</span></span> |
| 1098 |
<span id="cb27-8"><a href="#cb27-8" tabindex="-1"></a><span class="co"># 2 NA 5137561.</span></span> |
| 1099 |
<span id="cb27-9"><a href="#cb27-9" tabindex="-1"></a><span class="co"># 3 NA 5137561.</span></span> |
| 1100 |
<span id="cb27-10"><a href="#cb27-10" tabindex="-1"></a><span class="co"># 4 NA 5137561.</span></span> |
| 1101 |
<span id="cb27-11"><a href="#cb27-11" tabindex="-1"></a><span class="co"># 5 43.5 5137561.</span></span> |
| 1102 |
<span id="cb27-12"><a href="#cb27-12" tabindex="-1"></a><span class="co"># 6 40.0 5137561.</span></span></code></pre></div> |
| 1103 |
<p><code>ftransform</code> and <code>fcompute</code> are an order of |
| 1104 |
magnitude faster than <code>mutate</code>, but they do not support |
| 1105 |
grouped computations using arbitrary functions. We will see that this is |
| 1106 |
hardly a limitation as <em>collapse</em> provides very efficient and |
| 1107 |
elegant alternative programming mechanisms…</p> |
| 1108 |
</div> |
| 1109 |
<div id="replacing-and-sweeping-out-statistics" class="section level3"> |
| 1110 |
<h3>2.2 Replacing and Sweeping out Statistics</h3> |
| 1111 |
<!-- using Fast Statistical Functions --> |
| 1112 |
<p>All statistical (scalar-valued) functions in the collapse package |
| 1113 |
(<code>fsum, fprod, fmean, fmedian, fmode, fvar, fsd, fmin, fmax, fnth, ffirst, flast, fnobs, fndistinct</code>) |
| 1114 |
have a <code>TRA</code> argument which can be used to efficiently |
| 1115 |
transform data by either (column-wise) replacing data values with |
| 1116 |
computed statistics or sweeping the statistics out of the data. |
| 1117 |
Operations can be specified using either an integer or quoted operator / |
| 1118 |
string. The 10 operations supported by <code>TRA</code> are:</p> |
| 1119 |
<ul> |
| 1120 |
<li><p>1 - “replace_fill” : replace and overwrite missing values (same |
| 1121 |
as <code>mutate</code>)</p></li> |
| 1122 |
<li><p>2 - “replace” : replace but preserve missing values</p></li> |
| 1123 |
<li><p>3 - “-” : subtract (center)</p></li> |
| 1124 |
<li><p>4 - “-+” : subtract group-statistics but add average of group |
| 1125 |
statistics</p></li> |
| 1126 |
<li><p>5 - “/” : divide (scale)</p></li> |
| 1127 |
<li><p>6 - “%” : compute percentages (divide and multiply by |
| 1128 |
100)</p></li> |
| 1129 |
<li><p>7 - “+” : add</p></li> |
| 1130 |
<li><p>8 - “*” : multiply</p></li> |
| 1131 |
<li><p>9 - “%%” : modulus</p></li> |
| 1132 |
<li><p>10 - “-%%” : subtract modulus</p></li> |
| 1133 |
</ul> |
| 1134 |
<!-- For functions supporting weights (`fsum, fprod, fmean, fmode, fvar` and `fsd`) the `TRA` argument is in the third position following the data and weight vector (in the *grouped_df* method), whereas functions not supporting weights have the argument in the second position. --> |
| 1135 |
<p>Simple transformations are again straightforward to specify:</p> |
| 1136 |
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a><span class="co"># This subtracts the median value from all data points i.e. centers on the median</span></span> |
| 1137 |
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> num_vars <span class="sc">%>%</span> <span class="fu">fmedian</span>(<span class="at">TRA =</span> <span class="st">"-"</span>) <span class="sc">%>%</span> head</span> |
| 1138 |
<span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a><span class="co"># # A tibble: 6 × 12</span></span> |
| 1139 |
<span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a><span class="co"># Year AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1140 |
<span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a><span class="co"># <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1141 |
<span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a><span class="co"># 1 -22 NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1142 |
<span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a><span class="co"># 2 -21 NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1143 |
<span id="cb28-8"><a href="#cb28-8" tabindex="-1"></a><span class="co"># 3 -20 NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1144 |
<span id="cb28-9"><a href="#cb28-9" tabindex="-1"></a><span class="co"># 4 -19 NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1145 |
<span id="cb28-10"><a href="#cb28-10" tabindex="-1"></a><span class="co"># 5 -18 -4378. -170. -3717. -168. -1473. -3767. -1173. -959. -3924. -1431. -23149.</span></span> |
| 1146 |
<span id="cb28-11"><a href="#cb28-11" tabindex="-1"></a><span class="co"># 6 -17 -4379. -171. -3717. -168. -1472. -3767. -1173. -959. -3923. -1430. -23147.</span></span> |
| 1147 |
<span id="cb28-12"><a href="#cb28-12" tabindex="-1"></a></span> |
| 1148 |
<span id="cb28-13"><a href="#cb28-13" tabindex="-1"></a><span class="co"># This replaces all data points with the mode</span></span> |
| 1149 |
<span id="cb28-14"><a href="#cb28-14" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> char_vars <span class="sc">%>%</span> <span class="fu">fmode</span>(<span class="at">TRA =</span> <span class="st">"replace"</span>) <span class="sc">%>%</span> head</span> |
| 1150 |
<span id="cb28-15"><a href="#cb28-15" tabindex="-1"></a><span class="co"># # A tibble: 6 × 4</span></span> |
| 1151 |
<span id="cb28-16"><a href="#cb28-16" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable</span></span> |
| 1152 |
<span id="cb28-17"><a href="#cb28-17" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> </span></span> |
| 1153 |
<span id="cb28-18"><a href="#cb28-18" tabindex="-1"></a><span class="co"># 1 USA ASI Asia EMP </span></span> |
| 1154 |
<span id="cb28-19"><a href="#cb28-19" tabindex="-1"></a><span class="co"># 2 USA ASI Asia EMP </span></span> |
| 1155 |
<span id="cb28-20"><a href="#cb28-20" tabindex="-1"></a><span class="co"># 3 USA ASI Asia EMP </span></span> |
| 1156 |
<span id="cb28-21"><a href="#cb28-21" tabindex="-1"></a><span class="co"># 4 USA ASI Asia EMP </span></span> |
| 1157 |
<span id="cb28-22"><a href="#cb28-22" tabindex="-1"></a><span class="co"># 5 USA ASI Asia EMP </span></span> |
| 1158 |
<span id="cb28-23"><a href="#cb28-23" tabindex="-1"></a><span class="co"># 6 USA ASI Asia EMP</span></span></code></pre></div> |
| 1159 |
<p>Similarly for grouped transformations:</p> |
| 1160 |
<!-- We can also easily specify code to grouped demean, scale or compute percentages by groups: --> |
| 1161 |
<!-- ^[100% being the median of all annual VA/EMP values in a given sector and country across all years, not the sectoral output share which would have to be obtained using `sweep(GGDC10S[6:16], 1, GGDC10S$SUM, "/")`] --> |
| 1162 |
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" tabindex="-1"></a><span class="co"># Replacing data with the 2nd quartile (25%)</span></span> |
| 1163 |
<span id="cb29-2"><a href="#cb29-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1164 |
<span id="cb29-3"><a href="#cb29-3" tabindex="-1"></a> <span class="fu">fselect</span>(Variable, Country, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 1165 |
<span id="cb29-4"><a href="#cb29-4" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fnth</span>(<span class="fl">0.25</span>, <span class="at">TRA =</span> <span class="st">"replace_fill"</span>) <span class="sc">%>%</span> <span class="fu">head</span>(<span class="dv">3</span>)</span> |
| 1166 |
<span id="cb29-5"><a href="#cb29-5" tabindex="-1"></a><span class="co"># # A tibble: 3 × 13</span></span> |
| 1167 |
<span id="cb29-6"><a href="#cb29-6" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1168 |
<span id="cb29-7"><a href="#cb29-7" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1169 |
<span id="cb29-8"><a href="#cb29-8" tabindex="-1"></a><span class="co"># 1 VA BWA 63.5 33.1 27.3 7.36 26.8 31.1 13.2 12.0 33.6 11.5 262.</span></span> |
| 1170 |
<span id="cb29-9"><a href="#cb29-9" tabindex="-1"></a><span class="co"># 2 VA BWA 63.5 33.1 27.3 7.36 26.8 31.1 13.2 12.0 33.6 11.5 262.</span></span> |
| 1171 |
<span id="cb29-10"><a href="#cb29-10" tabindex="-1"></a><span class="co"># 3 VA BWA 63.5 33.1 27.3 7.36 26.8 31.1 13.2 12.0 33.6 11.5 262.</span></span> |
| 1172 |
<span id="cb29-11"><a href="#cb29-11" tabindex="-1"></a></span> |
| 1173 |
<span id="cb29-12"><a href="#cb29-12" tabindex="-1"></a><span class="co"># Scaling sectoral data by Variable and Country</span></span> |
| 1174 |
<span id="cb29-13"><a href="#cb29-13" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1175 |
<span id="cb29-14"><a href="#cb29-14" tabindex="-1"></a> <span class="fu">fselect</span>(Variable, Country, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 1176 |
<span id="cb29-15"><a href="#cb29-15" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fsd</span>(<span class="at">TRA =</span> <span class="st">"/"</span>) <span class="sc">%>%</span> head</span> |
| 1177 |
<span id="cb29-16"><a href="#cb29-16" tabindex="-1"></a><span class="co"># # A tibble: 6 × 13</span></span> |
| 1178 |
<span id="cb29-17"><a href="#cb29-17" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 1179 |
<span id="cb29-18"><a href="#cb29-18" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1180 |
<span id="cb29-19"><a href="#cb29-19" tabindex="-1"></a><span class="co"># 1 VA BWA NA NA NA NA NA NA NA NA NA </span></span> |
| 1181 |
<span id="cb29-20"><a href="#cb29-20" tabindex="-1"></a><span class="co"># 2 VA BWA NA NA NA NA NA NA NA NA NA </span></span> |
| 1182 |
<span id="cb29-21"><a href="#cb29-21" tabindex="-1"></a><span class="co"># 3 VA BWA NA NA NA NA NA NA NA NA NA </span></span> |
| 1183 |
<span id="cb29-22"><a href="#cb29-22" tabindex="-1"></a><span class="co"># 4 VA BWA NA NA NA NA NA NA NA NA NA </span></span> |
| 1184 |
<span id="cb29-23"><a href="#cb29-23" tabindex="-1"></a><span class="co"># 5 VA BWA 0.0270 0.000556 0.000523 3.88e-4 5.11e-4 0.00194 0.00154 5.23e-4 0.00134</span></span> |
| 1185 |
<span id="cb29-24"><a href="#cb29-24" tabindex="-1"></a><span class="co"># 6 VA BWA 0.0260 0.000397 0.000723 5.03e-4 1.04e-3 0.00220 0.00180 5.83e-4 0.00158</span></span> |
| 1186 |
<span id="cb29-25"><a href="#cb29-25" tabindex="-1"></a><span class="co"># # ℹ 2 more variables: OTH <dbl>, SUM <dbl></span></span></code></pre></div> |
| 1187 |
<!-- # Normalizing Data by expressing them in percentages of the median value within each country and sector (i.e. the median is 100%) --> |
| 1188 |
<!-- GGDC10S %>% --> |
| 1189 |
<!-- fselect(Variable, Country, AGR:SUM) %>% --> |
| 1190 |
<!-- fgroup_by(Variable, Country) %>% fmedian(TRA = "%") %>% head(3) --> |
| 1191 |
<p>The benchmarks below will demonstrate that these internal sweeping |
| 1192 |
and replacement operations fully performed in C++ compute significantly |
| 1193 |
faster than using <code>dplyr::mutate</code>, especially as the number |
| 1194 |
of groups grows large. The S3 generic nature of the <em>Fast Statistical |
| 1195 |
Functions</em> further allows us to perform grouped mutations on the fly |
| 1196 |
(together with <code>ftransform</code> or <code>fcompute</code>), |
| 1197 |
without the need of first creating a grouped tibble:</p> |
| 1198 |
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a><span class="co"># AGR_gmed = TRUE if AGR is greater than it's median value, grouped by Variable and Country</span></span> |
| 1199 |
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a><span class="co"># Note: This calls fmedian.default</span></span> |
| 1200 |
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a><span class="fu">settransform</span>(GGDC10S, <span class="at">AGR_gmed =</span> AGR <span class="sc">></span> <span class="fu">fmedian</span>(AGR, <span class="fu">list</span>(Variable, Country), <span class="at">TRA =</span> <span class="st">"replace"</span>))</span> |
| 1201 |
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a><span class="fu">tail</span>(GGDC10S, <span class="dv">3</span>)</span> |
| 1202 |
<span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a><span class="co"># # A tibble: 3 × 17</span></span> |
| 1203 |
<span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 1204 |
<span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1205 |
<span id="cb30-8"><a href="#cb30-8" tabindex="-1"></a><span class="co"># 1 EGY MENA Middle Ea… EMP 2010 5206. 29.0 2436. 307. 2733. 2977. 1992. 801. 5539.</span></span> |
| 1206 |
<span id="cb30-9"><a href="#cb30-9" tabindex="-1"></a><span class="co"># 2 EGY MENA Middle Ea… EMP 2011 5186. 27.6 2374. 318. 2795. 3020. 2048. 815. 5636.</span></span> |
| 1207 |
<span id="cb30-10"><a href="#cb30-10" tabindex="-1"></a><span class="co"># 3 EGY MENA Middle Ea… EMP 2012 5161. 24.8 2348. 325. 2931. 3110. 2065. 832. 5736.</span></span> |
| 1208 |
<span id="cb30-11"><a href="#cb30-11" tabindex="-1"></a><span class="co"># # ℹ 3 more variables: OTH <dbl>, SUM <dbl>, AGR_gmed <lgl></span></span> |
| 1209 |
<span id="cb30-12"><a href="#cb30-12" tabindex="-1"></a></span> |
| 1210 |
<span id="cb30-13"><a href="#cb30-13" tabindex="-1"></a><span class="co"># Dividing (scaling) the sectoral data (columns 6 through 16) by their grouped standard deviation</span></span> |
| 1211 |
<span id="cb30-14"><a href="#cb30-14" tabindex="-1"></a><span class="fu">settransformv</span>(GGDC10S, <span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>, fsd, <span class="fu">list</span>(Variable, Country), <span class="at">TRA =</span> <span class="st">"/"</span>, <span class="at">apply =</span> <span class="cn">FALSE</span>)</span> |
| 1212 |
<span id="cb30-15"><a href="#cb30-15" tabindex="-1"></a><span class="fu">tail</span>(GGDC10S, <span class="dv">3</span>)</span> |
| 1213 |
<span id="cb30-16"><a href="#cb30-16" tabindex="-1"></a><span class="co"># # A tibble: 3 × 17</span></span> |
| 1214 |
<span id="cb30-17"><a href="#cb30-17" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 1215 |
<span id="cb30-18"><a href="#cb30-18" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1216 |
<span id="cb30-19"><a href="#cb30-19" tabindex="-1"></a><span class="co"># 1 EGY MENA Middle Ea… EMP 2010 8.41 2.28 4.32 3.56 3.62 3.75 3.75 3.14 3.80</span></span> |
| 1217 |
<span id="cb30-20"><a href="#cb30-20" tabindex="-1"></a><span class="co"># 2 EGY MENA Middle Ea… EMP 2011 8.38 2.17 4.21 3.68 3.70 3.81 3.86 3.19 3.86</span></span> |
| 1218 |
<span id="cb30-21"><a href="#cb30-21" tabindex="-1"></a><span class="co"># 3 EGY MENA Middle Ea… EMP 2012 8.34 1.95 4.17 3.76 3.88 3.92 3.89 3.26 3.93</span></span> |
| 1219 |
<span id="cb30-22"><a href="#cb30-22" tabindex="-1"></a><span class="co"># # ℹ 3 more variables: OTH <dbl>, SUM <dbl>, AGR_gmed <lgl></span></span> |
| 1220 |
<span id="cb30-23"><a href="#cb30-23" tabindex="-1"></a><span class="fu">rm</span>(GGDC10S)</span></code></pre></div> |
| 1221 |
<p>Weights are easily added to any grouped transformation:</p> |
| 1222 |
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" tabindex="-1"></a><span class="co"># This subtracts weighted group means from the data, using SUM column as weights.. </span></span> |
| 1223 |
<span id="cb31-2"><a href="#cb31-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1224 |
<span id="cb31-3"><a href="#cb31-3" tabindex="-1"></a> <span class="fu">fselect</span>(Variable, Country, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 1225 |
<span id="cb31-4"><a href="#cb31-4" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fmean</span>(SUM, <span class="st">"-"</span>) <span class="sc">%>%</span> head</span> |
| 1226 |
<span id="cb31-5"><a href="#cb31-5" tabindex="-1"></a><span class="co"># # A tibble: 6 × 13</span></span> |
| 1227 |
<span id="cb31-6"><a href="#cb31-6" tabindex="-1"></a><span class="co"># Variable Country SUM AGR MIN MAN PU CON WRT TRA FIRE GOV OTH</span></span> |
| 1228 |
<span id="cb31-7"><a href="#cb31-7" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1229 |
<span id="cb31-8"><a href="#cb31-8" tabindex="-1"></a><span class="co"># 1 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1230 |
<span id="cb31-9"><a href="#cb31-9" tabindex="-1"></a><span class="co"># 2 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1231 |
<span id="cb31-10"><a href="#cb31-10" tabindex="-1"></a><span class="co"># 3 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1232 |
<span id="cb31-11"><a href="#cb31-11" tabindex="-1"></a><span class="co"># 4 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1233 |
<span id="cb31-12"><a href="#cb31-12" tabindex="-1"></a><span class="co"># 5 VA BWA 37.5 -1301. -13317. -2965. -529. -2746. -6540. -2157. -4431. -7551. -2613.</span></span> |
| 1234 |
<span id="cb31-13"><a href="#cb31-13" tabindex="-1"></a><span class="co"># 6 VA BWA 39.3 -1302. -13318. -2964. -529. -2745. -6540. -2156. -4431. -7550. -2613.</span></span></code></pre></div> |
| 1235 |
<!-- # Weighted scaling, weighted by SUM --> |
| 1236 |
<!-- GGDC10S %>% --> |
| 1237 |
<!-- fselect(Variable, Country, AGR:SUM) %>% --> |
| 1238 |
<!-- fgroup_by(Variable, Country) %>% fsd(SUM, "/") %>% head(3) --> |
| 1239 |
<!-- Alternatively we could also replace data points with their groupwise weighted mean or standard deviation: --> |
| 1240 |
<!-- ```{r} --> |
| 1241 |
<!-- # This conducts a weighted between transformation (replacing with weighted mean) --> |
| 1242 |
<!-- GGDC10S %>% --> |
| 1243 |
<!-- fselect(Variable, Country, AGR:SUM) %>% --> |
| 1244 |
<!-- fgroup_by(Variable, Country) %>% fmean(SUM, "replace") --> |
| 1245 |
<!-- # This also replaces missing values in each group --> |
| 1246 |
<!-- GGDC10S %>% --> |
| 1247 |
<!-- fselect(Variable, Country, AGR:SUM) %>% --> |
| 1248 |
<!-- fgroup_by(Variable, Country) %>% fmean(SUM, "replace_fill") --> |
| 1249 |
<!-- ``` --> |
| 1250 |
<!-- It is also possible to center data points on the overall mean, which is achieved by subtracting out group means and adding the overall mean of the data again: --> |
| 1251 |
<!-- ```{r} --> |
| 1252 |
<!-- # This group-centers data on the overall mean of the data --> |
| 1253 |
<!-- GGDC10S %>% --> |
| 1254 |
<!-- group_by(Variable, Country) %>% --> |
| 1255 |
<!-- select_at(6:16) %>% fmean(TRA = "-+") --> |
| 1256 |
<!-- ``` --> |
| 1257 |
<p>Sequential operations are also easily performed:</p> |
| 1258 |
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" tabindex="-1"></a><span class="co"># This scales and then subtracts the median</span></span> |
| 1259 |
<span id="cb32-2"><a href="#cb32-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1260 |
<span id="cb32-3"><a href="#cb32-3" tabindex="-1"></a> <span class="fu">fselect</span>(Variable, Country, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 1261 |
<span id="cb32-4"><a href="#cb32-4" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fsd</span>(<span class="at">TRA =</span> <span class="st">"/"</span>) <span class="sc">%>%</span> <span class="fu">fmedian</span>(<span class="at">TRA =</span> <span class="st">"-"</span>)</span> |
| 1262 |
<span id="cb32-5"><a href="#cb32-5" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 13</span></span> |
| 1263 |
<span id="cb32-6"><a href="#cb32-6" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1264 |
<span id="cb32-7"><a href="#cb32-7" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1265 |
<span id="cb32-8"><a href="#cb32-8" tabindex="-1"></a><span class="co"># 1 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1266 |
<span id="cb32-9"><a href="#cb32-9" tabindex="-1"></a><span class="co"># 2 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1267 |
<span id="cb32-10"><a href="#cb32-10" tabindex="-1"></a><span class="co"># 3 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1268 |
<span id="cb32-11"><a href="#cb32-11" tabindex="-1"></a><span class="co"># 4 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1269 |
<span id="cb32-12"><a href="#cb32-12" tabindex="-1"></a><span class="co"># 5 VA BWA -0.182 -0.235 -0.183 -0.245 -0.118 -0.0820 -0.0724 -0.0661 -0.108 -0.0848 -0.146</span></span> |
| 1270 |
<span id="cb32-13"><a href="#cb32-13" tabindex="-1"></a><span class="co"># 6 VA BWA -0.183 -0.235 -0.183 -0.245 -0.117 -0.0817 -0.0722 -0.0660 -0.108 -0.0846 -0.146</span></span> |
| 1271 |
<span id="cb32-14"><a href="#cb32-14" tabindex="-1"></a><span class="co"># 7 VA BWA -0.180 -0.235 -0.183 -0.245 -0.117 -0.0813 -0.0720 -0.0659 -0.107 -0.0843 -0.145</span></span> |
| 1272 |
<span id="cb32-15"><a href="#cb32-15" tabindex="-1"></a><span class="co"># 8 VA BWA -0.177 -0.235 -0.183 -0.245 -0.117 -0.0826 -0.0724 -0.0659 -0.107 -0.0841 -0.146</span></span> |
| 1273 |
<span id="cb32-16"><a href="#cb32-16" tabindex="-1"></a><span class="co"># 9 VA BWA -0.174 -0.235 -0.183 -0.245 -0.117 -0.0823 -0.0717 -0.0661 -0.108 -0.0848 -0.146</span></span> |
| 1274 |
<span id="cb32-17"><a href="#cb32-17" tabindex="-1"></a><span class="co"># 10 VA BWA -0.173 -0.234 -0.182 -0.243 -0.115 -0.0821 -0.0715 -0.0660 -0.108 -0.0846 -0.145</span></span> |
| 1275 |
<span id="cb32-18"><a href="#cb32-18" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1276 |
<span id="cb32-19"><a href="#cb32-19" tabindex="-1"></a><span class="co"># </span></span> |
| 1277 |
<span id="cb32-20"><a href="#cb32-20" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1278 |
<p>Of course it is also possible to combine multiple functions as in the |
| 1279 |
aggregation section, or to add variables to existing data:</p> |
| 1280 |
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" tabindex="-1"></a><span class="co"># This adds a groupwise observation count next to each column</span></span> |
| 1281 |
<span id="cb33-2"><a href="#cb33-2" tabindex="-1"></a><span class="fu">add_vars</span>(GGDC10S, <span class="fu">seq</span>(<span class="dv">7</span>,<span class="dv">27</span>,<span class="dv">2</span>)) <span class="ot"><-</span> GGDC10S <span class="sc">%>%</span></span> |
| 1282 |
<span id="cb33-3"><a href="#cb33-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fselect</span>(AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span></span> |
| 1283 |
<span id="cb33-4"><a href="#cb33-4" tabindex="-1"></a> <span class="fu">fnobs</span>(<span class="st">"replace_fill"</span>) <span class="sc">%>%</span> <span class="fu">add_stub</span>(<span class="st">"N_"</span>)</span> |
| 1284 |
<span id="cb33-5"><a href="#cb33-5" tabindex="-1"></a></span> |
| 1285 |
<span id="cb33-6"><a href="#cb33-6" tabindex="-1"></a><span class="fu">head</span>(GGDC10S)</span> |
| 1286 |
<span id="cb33-7"><a href="#cb33-7" tabindex="-1"></a><span class="co"># # A tibble: 6 × 27</span></span> |
| 1287 |
<span id="cb33-8"><a href="#cb33-8" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR N_AGR MIN N_MIN MAN N_MAN PU N_PU CON</span></span> |
| 1288 |
<span id="cb33-9"><a href="#cb33-9" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <int> <dbl> <int> <dbl> <int> <dbl> <int> <dbl></span></span> |
| 1289 |
<span id="cb33-10"><a href="#cb33-10" tabindex="-1"></a><span class="co"># 1 BWA SSA Sub-sa… VA 1960 NA 47 NA 47 NA 47 NA 47 NA </span></span> |
| 1290 |
<span id="cb33-11"><a href="#cb33-11" tabindex="-1"></a><span class="co"># 2 BWA SSA Sub-sa… VA 1961 NA 47 NA 47 NA 47 NA 47 NA </span></span> |
| 1291 |
<span id="cb33-12"><a href="#cb33-12" tabindex="-1"></a><span class="co"># 3 BWA SSA Sub-sa… VA 1962 NA 47 NA 47 NA 47 NA 47 NA </span></span> |
| 1292 |
<span id="cb33-13"><a href="#cb33-13" tabindex="-1"></a><span class="co"># 4 BWA SSA Sub-sa… VA 1963 NA 47 NA 47 NA 47 NA 47 NA </span></span> |
| 1293 |
<span id="cb33-14"><a href="#cb33-14" tabindex="-1"></a><span class="co"># 5 BWA SSA Sub-sa… VA 1964 16.3 47 3.49 47 0.737 47 0.104 47 0.660</span></span> |
| 1294 |
<span id="cb33-15"><a href="#cb33-15" tabindex="-1"></a><span class="co"># 6 BWA SSA Sub-sa… VA 1965 15.7 47 2.50 47 1.02 47 0.135 47 1.35 </span></span> |
| 1295 |
<span id="cb33-16"><a href="#cb33-16" tabindex="-1"></a><span class="co"># # ℹ 13 more variables: N_CON <int>, WRT <dbl>, N_WRT <int>, TRA <dbl>, N_TRA <int>, FIRE <dbl>,</span></span> |
| 1296 |
<span id="cb33-17"><a href="#cb33-17" tabindex="-1"></a><span class="co"># # N_FIRE <int>, GOV <dbl>, N_GOV <int>, OTH <dbl>, N_OTH <int>, SUM <dbl>, N_SUM <int></span></span> |
| 1297 |
<span id="cb33-18"><a href="#cb33-18" tabindex="-1"></a><span class="fu">rm</span>(GGDC10S)</span></code></pre></div> |
| 1298 |
<p>There are lots of other examples one could construct using the 10 |
| 1299 |
operations and 14 functions listed above, the examples provided just |
| 1300 |
outline the suggested programming basics. Performance considerations |
| 1301 |
make it very much worthwhile to spend some time and think how complex |
| 1302 |
operations can be implemented in this programming framework, before |
| 1303 |
defining some function in R and applying it to data using |
| 1304 |
<code>dplyr::mutate</code>.</p> |
| 1305 |
<!-- ***could add add_vars example again*** --> |
| 1306 |
</div> |
| 1307 |
<div id="more-control-using-the-tra-function" class="section level3"> |
| 1308 |
<h3>2.3 More Control using the <code>TRA</code> Function</h3> |
| 1309 |
<p>Towards this end, calling <code>TRA()</code> directly also |
| 1310 |
facilitates more complex and customized operations. Behind the scenes of |
| 1311 |
the <code>TRA = ...</code> argument, the <em>Fast Statistical |
| 1312 |
Functions</em> first compute the grouped statistics on all columns of |
| 1313 |
the data, and these statistics are then directly fed into a C++ function |
| 1314 |
that uses them to replace or sweep them out of data points in one of the |
| 1315 |
10 ways described above. This function can also be called directly by |
| 1316 |
the name of <code>TRA</code>. |
| 1317 |
<!-- (shorthand for 'transforming' data by replacing or sweeping out statistics). --></p> |
| 1318 |
<p>Fundamentally, <code>TRA</code> is a generalization of |
| 1319 |
<code>base::sweep</code> for column-wise grouped operations<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>. Direct |
| 1320 |
calls to <code>TRA</code> enable more control over inputs and |
| 1321 |
outputs.</p> |
| 1322 |
<p>The two operations below are equivalent, although the first is |
| 1323 |
slightly more efficient as it only requires one method dispatch and one |
| 1324 |
check of the inputs:</p> |
| 1325 |
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" tabindex="-1"></a><span class="co"># This divides by the product</span></span> |
| 1326 |
<span id="cb34-2"><a href="#cb34-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1327 |
<span id="cb34-3"><a href="#cb34-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 1328 |
<span id="cb34-4"><a href="#cb34-4" tabindex="-1"></a> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> <span class="fu">fprod</span>(<span class="at">TRA =</span> <span class="st">"/"</span>) <span class="sc">%>%</span> head</span> |
| 1329 |
<span id="cb34-5"><a href="#cb34-5" tabindex="-1"></a><span class="co"># # A tibble: 6 × 11</span></span> |
| 1330 |
<span id="cb34-6"><a href="#cb34-6" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 1331 |
<span id="cb34-7"><a href="#cb34-7" tabindex="-1"></a><span class="co"># <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1332 |
<span id="cb34-8"><a href="#cb34-8" tabindex="-1"></a><span class="co"># 1 NA NA NA NA NA NA NA NA NA </span></span> |
| 1333 |
<span id="cb34-9"><a href="#cb34-9" tabindex="-1"></a><span class="co"># 2 NA NA NA NA NA NA NA NA NA </span></span> |
| 1334 |
<span id="cb34-10"><a href="#cb34-10" tabindex="-1"></a><span class="co"># 3 NA NA NA NA NA NA NA NA NA </span></span> |
| 1335 |
<span id="cb34-11"><a href="#cb34-11" tabindex="-1"></a><span class="co"># 4 NA NA NA NA NA NA NA NA NA </span></span> |
| 1336 |
<span id="cb34-12"><a href="#cb34-12" tabindex="-1"></a><span class="co"># 5 1.29e-105 2.81e-127 1.40e-101 4.44e-74 4.19e-102 3.97e-113 6.91e-92 1.01e-97 2.51e-117</span></span> |
| 1337 |
<span id="cb34-13"><a href="#cb34-13" tabindex="-1"></a><span class="co"># 6 1.24e-105 2.00e-127 1.94e-101 5.75e-74 8.55e-102 4.49e-113 8.08e-92 1.13e-97 2.96e-117</span></span> |
| 1338 |
<span id="cb34-14"><a href="#cb34-14" tabindex="-1"></a><span class="co"># # ℹ 2 more variables: OTH <dbl>, SUM <dbl></span></span> |
| 1339 |
<span id="cb34-15"><a href="#cb34-15" tabindex="-1"></a></span> |
| 1340 |
<span id="cb34-16"><a href="#cb34-16" tabindex="-1"></a><span class="co"># Same thing</span></span> |
| 1341 |
<span id="cb34-17"><a href="#cb34-17" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1342 |
<span id="cb34-18"><a href="#cb34-18" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 1343 |
<span id="cb34-19"><a href="#cb34-19" tabindex="-1"></a> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> </span> |
| 1344 |
<span id="cb34-20"><a href="#cb34-20" tabindex="-1"></a> <span class="fu">TRA</span>(<span class="fu">fprod</span>(., <span class="at">keep.group_vars =</span> <span class="cn">FALSE</span>), <span class="st">"/"</span>) <span class="sc">%>%</span> head <span class="co"># [same as TRA(.,fprod(., keep.group_vars = FALSE),"/")]</span></span> |
| 1345 |
<span id="cb34-21"><a href="#cb34-21" tabindex="-1"></a><span class="co"># # A tibble: 6 × 11</span></span> |
| 1346 |
<span id="cb34-22"><a href="#cb34-22" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 1347 |
<span id="cb34-23"><a href="#cb34-23" tabindex="-1"></a><span class="co"># <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1348 |
<span id="cb34-24"><a href="#cb34-24" tabindex="-1"></a><span class="co"># 1 NA NA NA NA NA NA NA NA NA </span></span> |
| 1349 |
<span id="cb34-25"><a href="#cb34-25" tabindex="-1"></a><span class="co"># 2 NA NA NA NA NA NA NA NA NA </span></span> |
| 1350 |
<span id="cb34-26"><a href="#cb34-26" tabindex="-1"></a><span class="co"># 3 NA NA NA NA NA NA NA NA NA </span></span> |
| 1351 |
<span id="cb34-27"><a href="#cb34-27" tabindex="-1"></a><span class="co"># 4 NA NA NA NA NA NA NA NA NA </span></span> |
| 1352 |
<span id="cb34-28"><a href="#cb34-28" tabindex="-1"></a><span class="co"># 5 1.29e-105 2.81e-127 1.40e-101 4.44e-74 4.19e-102 3.97e-113 6.91e-92 1.01e-97 2.51e-117</span></span> |
| 1353 |
<span id="cb34-29"><a href="#cb34-29" tabindex="-1"></a><span class="co"># 6 1.24e-105 2.00e-127 1.94e-101 5.75e-74 8.55e-102 4.49e-113 8.08e-92 1.13e-97 2.96e-117</span></span> |
| 1354 |
<span id="cb34-30"><a href="#cb34-30" tabindex="-1"></a><span class="co"># # ℹ 2 more variables: OTH <dbl>, SUM <dbl></span></span></code></pre></div> |
| 1355 |
<p><code>TRA.grouped_df</code> was designed such that it matches the |
| 1356 |
columns of the statistics (aggregated columns) to those of the original |
| 1357 |
data, and only transforms matching columns while returning the whole |
| 1358 |
data frame. Thus it is easily possible to only apply a transformation to |
| 1359 |
the first two sectors:</p> |
| 1360 |
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" tabindex="-1"></a><span class="co"># This only demeans Agriculture (AGR) and Mining (MIN)</span></span> |
| 1361 |
<span id="cb35-2"><a href="#cb35-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1362 |
<span id="cb35-3"><a href="#cb35-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 1363 |
<span id="cb35-4"><a href="#cb35-4" tabindex="-1"></a> <span class="fu">TRA</span>(<span class="fu">fselect</span>(., AGR, MIN) <span class="sc">%>%</span> <span class="fu">fmean</span>(<span class="at">keep.group_vars =</span> <span class="cn">FALSE</span>), <span class="st">"-"</span>) <span class="sc">%>%</span> head</span> |
| 1364 |
<span id="cb35-5"><a href="#cb35-5" tabindex="-1"></a><span class="co"># # A tibble: 6 × 16</span></span> |
| 1365 |
<span id="cb35-6"><a href="#cb35-6" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 1366 |
<span id="cb35-7"><a href="#cb35-7" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1367 |
<span id="cb35-8"><a href="#cb35-8" tabindex="-1"></a><span class="co"># 1 BWA SSA Sub-s… VA 1960 NA NA NA NA NA NA NA NA NA </span></span> |
| 1368 |
<span id="cb35-9"><a href="#cb35-9" tabindex="-1"></a><span class="co"># 2 BWA SSA Sub-s… VA 1961 NA NA NA NA NA NA NA NA NA </span></span> |
| 1369 |
<span id="cb35-10"><a href="#cb35-10" tabindex="-1"></a><span class="co"># 3 BWA SSA Sub-s… VA 1962 NA NA NA NA NA NA NA NA NA </span></span> |
| 1370 |
<span id="cb35-11"><a href="#cb35-11" tabindex="-1"></a><span class="co"># 4 BWA SSA Sub-s… VA 1963 NA NA NA NA NA NA NA NA NA </span></span> |
| 1371 |
<span id="cb35-12"><a href="#cb35-12" tabindex="-1"></a><span class="co"># 5 BWA SSA Sub-s… VA 1964 -446. -4505. 0.737 0.104 0.660 6.24 1.66 1.12 4.82</span></span> |
| 1372 |
<span id="cb35-13"><a href="#cb35-13" tabindex="-1"></a><span class="co"># 6 BWA SSA Sub-s… VA 1965 -446. -4506. 1.02 0.135 1.35 7.06 1.94 1.25 5.70</span></span> |
| 1373 |
<span id="cb35-14"><a href="#cb35-14" tabindex="-1"></a><span class="co"># # ℹ 2 more variables: OTH <dbl>, SUM <dbl></span></span></code></pre></div> |
| 1374 |
<p>Since <code>TRA</code> is already built into all <em>Fast Statistical |
| 1375 |
Functions</em> as an argument, it is best used in computations where |
| 1376 |
grouped statistics are computed using some other function.</p> |
| 1377 |
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" tabindex="-1"></a><span class="co"># Same as above, with one line of code using fmean.data.frame and ftransform...</span></span> |
| 1378 |
<span id="cb36-2"><a href="#cb36-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="fu">ftransform</span>(<span class="fu">fmean</span>(<span class="fu">list</span>(<span class="at">AGR =</span> AGR, <span class="at">MIN =</span> MIN), <span class="fu">list</span>(Variable, Country), <span class="at">TRA =</span> <span class="st">"-"</span>)) <span class="sc">%>%</span> head</span> |
| 1379 |
<span id="cb36-3"><a href="#cb36-3" tabindex="-1"></a><span class="co"># # A tibble: 6 × 16</span></span> |
| 1380 |
<span id="cb36-4"><a href="#cb36-4" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU CON WRT TRA FIRE GOV</span></span> |
| 1381 |
<span id="cb36-5"><a href="#cb36-5" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1382 |
<span id="cb36-6"><a href="#cb36-6" tabindex="-1"></a><span class="co"># 1 BWA SSA Sub-s… VA 1960 NA NA NA NA NA NA NA NA NA </span></span> |
| 1383 |
<span id="cb36-7"><a href="#cb36-7" tabindex="-1"></a><span class="co"># 2 BWA SSA Sub-s… VA 1961 NA NA NA NA NA NA NA NA NA </span></span> |
| 1384 |
<span id="cb36-8"><a href="#cb36-8" tabindex="-1"></a><span class="co"># 3 BWA SSA Sub-s… VA 1962 NA NA NA NA NA NA NA NA NA </span></span> |
| 1385 |
<span id="cb36-9"><a href="#cb36-9" tabindex="-1"></a><span class="co"># 4 BWA SSA Sub-s… VA 1963 NA NA NA NA NA NA NA NA NA </span></span> |
| 1386 |
<span id="cb36-10"><a href="#cb36-10" tabindex="-1"></a><span class="co"># 5 BWA SSA Sub-s… VA 1964 -446. -4505. 0.737 0.104 0.660 6.24 1.66 1.12 4.82</span></span> |
| 1387 |
<span id="cb36-11"><a href="#cb36-11" tabindex="-1"></a><span class="co"># 6 BWA SSA Sub-s… VA 1965 -446. -4506. 1.02 0.135 1.35 7.06 1.94 1.25 5.70</span></span> |
| 1388 |
<span id="cb36-12"><a href="#cb36-12" tabindex="-1"></a><span class="co"># # ℹ 2 more variables: OTH <dbl>, SUM <dbl></span></span></code></pre></div> |
| 1389 |
<!-- # Get grouped tibble --> |
| 1390 |
<!-- gGGDC <- GGDC10S %>% group_by(Variable, Country) --> |
| 1391 |
<!-- library(microbenchmark) --> |
| 1392 |
<!-- microbenchmark(TRA = gGGDC %>% TRA(summarise_at(., c("AGR","SUM"), sum, na.rm = TRUE), "replace_fill"), --> |
| 1393 |
<!-- mutate = gGGDC %>% mutate_at(c("AGR","SUM"), sum, na.rm = TRUE)) --> |
| 1394 |
<p>Another potential use of <code>TRA</code> is to do computations in |
| 1395 |
two- or more steps, for example if both aggregated and transformed data |
| 1396 |
are needed, or if computations are more complex and involve other |
| 1397 |
manipulations in-between the aggregating and sweeping part:</p> |
| 1398 |
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" tabindex="-1"></a><span class="co"># Get grouped tibble</span></span> |
| 1399 |
<span id="cb37-2"><a href="#cb37-2" tabindex="-1"></a>gGGDC <span class="ot"><-</span> GGDC10S <span class="sc">%>%</span> <span class="fu">fgroup_by</span>(Variable, Country)</span> |
| 1400 |
<span id="cb37-3"><a href="#cb37-3" tabindex="-1"></a></span> |
| 1401 |
<span id="cb37-4"><a href="#cb37-4" tabindex="-1"></a><span class="co"># Get aggregated data</span></span> |
| 1402 |
<span id="cb37-5"><a href="#cb37-5" tabindex="-1"></a>gsumGGDC <span class="ot"><-</span> gGGDC <span class="sc">%>%</span> <span class="fu">fselect</span>(AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> fsum</span> |
| 1403 |
<span id="cb37-6"><a href="#cb37-6" tabindex="-1"></a><span class="fu">head</span>(gsumGGDC)</span> |
| 1404 |
<span id="cb37-7"><a href="#cb37-7" tabindex="-1"></a><span class="co"># # A tibble: 6 × 13</span></span> |
| 1405 |
<span id="cb37-8"><a href="#cb37-8" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1406 |
<span id="cb37-9"><a href="#cb37-9" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1407 |
<span id="cb37-10"><a href="#cb37-10" tabindex="-1"></a><span class="co"># 1 EMP ARG 88028. 3230. 1.20e5 6307. 4.60e4 1.23e5 4.02e4 3.89e4 1.27e5 6.15e4 6.54e5</span></span> |
| 1408 |
<span id="cb37-11"><a href="#cb37-11" tabindex="-1"></a><span class="co"># 2 EMP BOL 58817. 3418. 1.43e4 326. 7.49e3 1.72e4 7.04e3 2.72e3 NA 2.41e4 1.35e5</span></span> |
| 1409 |
<span id="cb37-12"><a href="#cb37-12" tabindex="-1"></a><span class="co"># 3 EMP BRA 1065864. 12773. 4.33e5 22604. 2.19e5 5.28e5 1.27e5 2.74e5 3.29e5 3.54e5 3.36e6</span></span> |
| 1410 |
<span id="cb37-13"><a href="#cb37-13" tabindex="-1"></a><span class="co"># 4 EMP BWA 8839. 493. 8.49e2 145. 1.19e3 1.71e3 3.93e2 7.21e2 2.87e3 1.30e3 1.85e4</span></span> |
| 1411 |
<span id="cb37-14"><a href="#cb37-14" tabindex="-1"></a><span class="co"># 5 EMP CHL 44220. 6389. 3.94e4 1850. 1.86e4 4.38e4 1.63e4 1.72e4 NA 6.32e4 2.51e5</span></span> |
| 1412 |
<span id="cb37-15"><a href="#cb37-15" tabindex="-1"></a><span class="co"># 6 EMP CHN 17264654. 422972. 4.03e6 96364. 1.25e6 1.73e6 8.36e5 2.96e5 1.36e6 1.86e6 2.91e7</span></span> |
| 1413 |
<span id="cb37-16"><a href="#cb37-16" tabindex="-1"></a></span> |
| 1414 |
<span id="cb37-17"><a href="#cb37-17" tabindex="-1"></a><span class="co"># Get transformed (scaled) data</span></span> |
| 1415 |
<span id="cb37-18"><a href="#cb37-18" tabindex="-1"></a><span class="fu">head</span>(<span class="fu">TRA</span>(gGGDC, gsumGGDC, <span class="st">"/"</span>))</span> |
| 1416 |
<span id="cb37-19"><a href="#cb37-19" tabindex="-1"></a><span class="co"># # A tibble: 6 × 16</span></span> |
| 1417 |
<span id="cb37-20"><a href="#cb37-20" tabindex="-1"></a><span class="co"># Country Regioncode Region Variable Year AGR MIN MAN PU CON WRT</span></span> |
| 1418 |
<span id="cb37-21"><a href="#cb37-21" tabindex="-1"></a><span class="co"># <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1419 |
<span id="cb37-22"><a href="#cb37-22" tabindex="-1"></a><span class="co"># 1 BWA SSA Sub-sahar… VA 1960 NA NA NA NA NA NA </span></span> |
| 1420 |
<span id="cb37-23"><a href="#cb37-23" tabindex="-1"></a><span class="co"># 2 BWA SSA Sub-sahar… VA 1961 NA NA NA NA NA NA </span></span> |
| 1421 |
<span id="cb37-24"><a href="#cb37-24" tabindex="-1"></a><span class="co"># 3 BWA SSA Sub-sahar… VA 1962 NA NA NA NA NA NA </span></span> |
| 1422 |
<span id="cb37-25"><a href="#cb37-25" tabindex="-1"></a><span class="co"># 4 BWA SSA Sub-sahar… VA 1963 NA NA NA NA NA NA </span></span> |
| 1423 |
<span id="cb37-26"><a href="#cb37-26" tabindex="-1"></a><span class="co"># 5 BWA SSA Sub-sahar… VA 1964 7.50e-4 1.65e-5 1.66e-5 1.03e-5 1.57e-5 6.82e-5</span></span> |
| 1424 |
<span id="cb37-27"><a href="#cb37-27" tabindex="-1"></a><span class="co"># 6 BWA SSA Sub-sahar… VA 1965 7.24e-4 1.18e-5 2.30e-5 1.33e-5 3.20e-5 7.72e-5</span></span> |
| 1425 |
<span id="cb37-28"><a href="#cb37-28" tabindex="-1"></a><span class="co"># # ℹ 5 more variables: TRA <dbl>, FIRE <dbl>, GOV <dbl>, OTH <dbl>, SUM <dbl></span></span></code></pre></div> |
| 1426 |
<p>As discussed, whether using the argument to fast statistical |
| 1427 |
functions or <code>TRA</code> directly, these data transformations are |
| 1428 |
essentially a two-step process: Statistics are first computed and then |
| 1429 |
used to transform the original data.</p> |
| 1430 |
<!-- This process is already very efficient since all functions are written in C++, and programmatically separating the computation of statistics and data transformation tasks allows for unlimited combinations and drastically simplifies the code base of this package. --> |
| 1431 |
<!-- Nonetheless there are of course more memory efficient and faster ways to program such data transformations, which principally involve doing them column-by-column with a single C++ function. --> |
| 1432 |
<p>Although both steps are efficiently done in C++, it would be even |
| 1433 |
more efficient to do them in a single step without materializing all the |
| 1434 |
statistics before transforming the data. Such slightly more efficient |
| 1435 |
functions are provided for the very commonly applied tasks of centering |
| 1436 |
and averaging data by groups (widely known as ‘between’-group and |
| 1437 |
‘within’-group transformations), and scaling and centering data by |
| 1438 |
groups (also known as ‘standardizing’ data).</p> |
| 1439 |
<!-- To ensure that this *collapse* lives up to the highest standards of performance for common uses, it also provides ... --> |
| 1440 |
</div> |
| 1441 |
<div id="faster-centering-averaging-and-standardizing" class="section level3"> |
| 1442 |
<h3>2.4 Faster Centering, Averaging and Standardizing</h3> |
| 1443 |
<!-- Between and Within Transformations and Standardization --> |
| 1444 |
<p>The functions <code>fbetween</code> and <code>fwithin</code> are |
| 1445 |
slightly more memory efficient implementations of <code>fmean</code> |
| 1446 |
invoked with different <code>TRA</code> options:</p> |
| 1447 |
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="co"># Same as ... %>% fmean(TRA = "replace")</span></span> |
| 1448 |
<span id="cb38-2"><a href="#cb38-2" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fbetween <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">2</span>)</span> |
| 1449 |
<span id="cb38-3"><a href="#cb38-3" tabindex="-1"></a><span class="co"># # A tibble: 2 × 11</span></span> |
| 1450 |
<span id="cb38-4"><a href="#cb38-4" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1451 |
<span id="cb38-5"><a href="#cb38-5" tabindex="-1"></a><span class="co"># <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1452 |
<span id="cb38-6"><a href="#cb38-6" tabindex="-1"></a><span class="co"># 1 4444. 34.9 1614. 131. 997. 1307. 799. 320. 2958. NA 12605.</span></span> |
| 1453 |
<span id="cb38-7"><a href="#cb38-7" tabindex="-1"></a><span class="co"># 2 4444. 34.9 1614. 131. 997. 1307. 799. 320. 2958. NA 12605.</span></span> |
| 1454 |
<span id="cb38-8"><a href="#cb38-8" tabindex="-1"></a></span> |
| 1455 |
<span id="cb38-9"><a href="#cb38-9" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="co"># Same as ... %>% fmean(TRA = "replace_fill")</span></span> |
| 1456 |
<span id="cb38-10"><a href="#cb38-10" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> <span class="fu">fbetween</span>(<span class="at">fill =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">2</span>)</span> |
| 1457 |
<span id="cb38-11"><a href="#cb38-11" tabindex="-1"></a><span class="co"># # A tibble: 2 × 11</span></span> |
| 1458 |
<span id="cb38-12"><a href="#cb38-12" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1459 |
<span id="cb38-13"><a href="#cb38-13" tabindex="-1"></a><span class="co"># <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1460 |
<span id="cb38-14"><a href="#cb38-14" tabindex="-1"></a><span class="co"># 1 4444. 34.9 1614. 131. 997. 1307. 799. 320. 2958. NA 12605.</span></span> |
| 1461 |
<span id="cb38-15"><a href="#cb38-15" tabindex="-1"></a><span class="co"># 2 4444. 34.9 1614. 131. 997. 1307. 799. 320. 2958. NA 12605.</span></span> |
| 1462 |
<span id="cb38-16"><a href="#cb38-16" tabindex="-1"></a></span> |
| 1463 |
<span id="cb38-17"><a href="#cb38-17" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> <span class="co"># Same as ... %>% fmean(TRA = "-")</span></span> |
| 1464 |
<span id="cb38-18"><a href="#cb38-18" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">get_vars</span>(<span class="dv">6</span><span class="sc">:</span><span class="dv">16</span>) <span class="sc">%>%</span> fwithin <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">2</span>)</span> |
| 1465 |
<span id="cb38-19"><a href="#cb38-19" tabindex="-1"></a><span class="co"># # A tibble: 2 × 11</span></span> |
| 1466 |
<span id="cb38-20"><a href="#cb38-20" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1467 |
<span id="cb38-21"><a href="#cb38-21" tabindex="-1"></a><span class="co"># <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1468 |
<span id="cb38-22"><a href="#cb38-22" tabindex="-1"></a><span class="co"># 1 742. -7.35 760. 187. 1798. 1713. 1249. 495. 2678. NA 9614.</span></span> |
| 1469 |
<span id="cb38-23"><a href="#cb38-23" tabindex="-1"></a><span class="co"># 2 717. -10.1 734. 194. 1934. 1803. 1266. 512. 2778. NA 9928.</span></span></code></pre></div> |
| 1470 |
<p>Apart from higher speed, <code>fwithin</code> has a <code>mean</code> |
| 1471 |
argument to assign an arbitrary mean to centered data, the default being |
| 1472 |
<code>mean = 0</code>. A very common choice for such an added mean is |
| 1473 |
just the overall mean of the data, which can be added in by invoking |
| 1474 |
<code>mean = "overall.mean"</code>:</p> |
| 1475 |
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> </span> |
| 1476 |
<span id="cb39-2"><a href="#cb39-2" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> </span> |
| 1477 |
<span id="cb39-3"><a href="#cb39-3" tabindex="-1"></a> <span class="fu">fselect</span>(Country, Variable, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> <span class="fu">fwithin</span>(<span class="at">mean =</span> <span class="st">"overall.mean"</span>) <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">3</span>)</span> |
| 1478 |
<span id="cb39-4"><a href="#cb39-4" tabindex="-1"></a><span class="co"># # A tibble: 3 × 13</span></span> |
| 1479 |
<span id="cb39-5"><a href="#cb39-5" tabindex="-1"></a><span class="co"># Country Variable AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1480 |
<span id="cb39-6"><a href="#cb39-6" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1481 |
<span id="cb39-7"><a href="#cb39-7" tabindex="-1"></a><span class="co"># 1 EGY EMP 2527458. 1867903. 5539313. 3.36e5 1.80e6 3.39e6 1.47e6 1.66e6 1.71e6 NA 2.16e7</span></span> |
| 1482 |
<span id="cb39-8"><a href="#cb39-8" tabindex="-1"></a><span class="co"># 2 EGY EMP 2527439. 1867902. 5539251. 3.36e5 1.80e6 3.39e6 1.47e6 1.66e6 1.71e6 NA 2.16e7</span></span> |
| 1483 |
<span id="cb39-9"><a href="#cb39-9" tabindex="-1"></a><span class="co"># 3 EGY EMP 2527413. 1867899. 5539226. 3.36e5 1.80e6 3.39e6 1.47e6 1.66e6 1.72e6 NA 2.16e7</span></span></code></pre></div> |
| 1484 |
<!-- in particular, which regards the joint use of weights and the `mean = "overall.mean"` option: `... %>% fmean(w = SUM, TRA = "-+")` will not properly group-center the data on the overall weighted mean. Instead, it will group-center data on a frequency weighted average of the weighted group-means, thus not taking into account different aggregated weights attached to those weighted group-means themselves. The reason for this shortcoming is simply that `TRA` was not designed to take a separate weight vector as input. `fwithin(w = SUM, mean = "overall.mean")` does a better job and properly centers data on the weighted overall mean after subtracting out weighted group means: --> |
| 1485 |
<!-- ```{r} --> |
| 1486 |
<!-- GGDC10S %>% # This does not center data on a properly computed weighted overall mean --> |
| 1487 |
<!-- group_by(Variable, Country) %>% select_at(6:16) %>% fmean(SUM, TRA = "-+") --> |
| 1488 |
<!-- GGDC10S %>% # This does a proper job by both subtracting weighted group-means and adding a weighted overall mean --> |
| 1489 |
<!-- group_by(Variable, Country) %>% select_at(6:16) %>% fwithin(SUM, mean = "overall.mean") --> |
| 1490 |
<!-- ``` --> |
| 1491 |
<p>This can also be done using weights. The code below uses the |
| 1492 |
<code>SUM</code> column as weights, and then for each variable and each |
| 1493 |
group subtracts out the weighted mean, and then adds the overall |
| 1494 |
weighted column mean back to the centered columns. The <code>SUM</code> |
| 1495 |
column is just kept as it is and added after the grouping columns.</p> |
| 1496 |
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span> </span> |
| 1497 |
<span id="cb40-2"><a href="#cb40-2" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> </span> |
| 1498 |
<span id="cb40-3"><a href="#cb40-3" tabindex="-1"></a> <span class="fu">fselect</span>(Country, Variable, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> <span class="fu">fwithin</span>(SUM, <span class="at">mean =</span> <span class="st">"overall.mean"</span>) <span class="sc">%>%</span> <span class="fu">tail</span>(<span class="dv">3</span>)</span> |
| 1499 |
<span id="cb40-4"><a href="#cb40-4" tabindex="-1"></a><span class="co"># # A tibble: 3 × 13</span></span> |
| 1500 |
<span id="cb40-5"><a href="#cb40-5" tabindex="-1"></a><span class="co"># Country Variable SUM AGR MIN MAN PU CON WRT TRA FIRE GOV OTH</span></span> |
| 1501 |
<span id="cb40-6"><a href="#cb40-6" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1502 |
<span id="cb40-7"><a href="#cb40-7" tabindex="-1"></a><span class="co"># 1 EGY EMP 22020. 429066006. 3.70e8 7.38e8 2.73e7 2.83e8 4.33e8 1.97e8 1.55e8 2.10e8 NA</span></span> |
| 1503 |
<span id="cb40-8"><a href="#cb40-8" tabindex="-1"></a><span class="co"># 2 EGY EMP 22219. 429065986. 3.70e8 7.38e8 2.73e7 2.83e8 4.33e8 1.97e8 1.55e8 2.10e8 NA</span></span> |
| 1504 |
<span id="cb40-9"><a href="#cb40-9" tabindex="-1"></a><span class="co"># 3 EGY EMP 22533. 429065961. 3.70e8 7.38e8 2.73e7 2.83e8 4.33e8 1.97e8 1.55e8 2.10e8 NA</span></span></code></pre></div> |
| 1505 |
<p>Another argument to <code>fwithin</code> is the <code>theta</code> |
| 1506 |
parameter, allowing partial- or quasi-demeaning operations, |
| 1507 |
e.g. <code>fwithin(gdata, theta = theta)</code> is equal to |
| 1508 |
<code>gdata - theta * fbetween(gdata)</code>. This is particularly |
| 1509 |
useful to prepare data for variance components (also known as |
| 1510 |
‘random-effects’) estimation.</p> |
| 1511 |
<p>Apart from <code>fbetween</code> and <code>fwithin</code>, the |
| 1512 |
function <code>fscale</code> exists to efficiently scale and center |
| 1513 |
data, to avoid sequential calls such as |
| 1514 |
<code>... %>% fsd(TRA = "/") %>% fmean(TRA = "-")</code>.</p> |
| 1515 |
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" tabindex="-1"></a><span class="co"># This efficiently scales and centers (i.e. standardizes) the data</span></span> |
| 1516 |
<span id="cb41-2"><a href="#cb41-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1517 |
<span id="cb41-3"><a href="#cb41-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 1518 |
<span id="cb41-4"><a href="#cb41-4" tabindex="-1"></a> <span class="fu">fselect</span>(Country, Variable, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> fscale</span> |
| 1519 |
<span id="cb41-5"><a href="#cb41-5" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 13</span></span> |
| 1520 |
<span id="cb41-6"><a href="#cb41-6" tabindex="-1"></a><span class="co"># Country Variable AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1521 |
<span id="cb41-7"><a href="#cb41-7" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1522 |
<span id="cb41-8"><a href="#cb41-8" tabindex="-1"></a><span class="co"># 1 BWA VA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1523 |
<span id="cb41-9"><a href="#cb41-9" tabindex="-1"></a><span class="co"># 2 BWA VA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1524 |
<span id="cb41-10"><a href="#cb41-10" tabindex="-1"></a><span class="co"># 3 BWA VA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1525 |
<span id="cb41-11"><a href="#cb41-11" tabindex="-1"></a><span class="co"># 4 BWA VA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1526 |
<span id="cb41-12"><a href="#cb41-12" tabindex="-1"></a><span class="co"># 5 BWA VA -0.738 -0.717 -0.668 -0.805 -0.692 -0.603 -0.589 -0.635 -0.656 -0.596 -0.676</span></span> |
| 1527 |
<span id="cb41-13"><a href="#cb41-13" tabindex="-1"></a><span class="co"># 6 BWA VA -0.739 -0.717 -0.668 -0.805 -0.692 -0.603 -0.589 -0.635 -0.656 -0.596 -0.676</span></span> |
| 1528 |
<span id="cb41-14"><a href="#cb41-14" tabindex="-1"></a><span class="co"># 7 BWA VA -0.736 -0.717 -0.668 -0.805 -0.692 -0.603 -0.589 -0.635 -0.656 -0.595 -0.676</span></span> |
| 1529 |
<span id="cb41-15"><a href="#cb41-15" tabindex="-1"></a><span class="co"># 8 BWA VA -0.734 -0.717 -0.668 -0.805 -0.692 -0.604 -0.589 -0.635 -0.655 -0.595 -0.676</span></span> |
| 1530 |
<span id="cb41-16"><a href="#cb41-16" tabindex="-1"></a><span class="co"># 9 BWA VA -0.730 -0.717 -0.668 -0.805 -0.692 -0.604 -0.588 -0.635 -0.656 -0.596 -0.676</span></span> |
| 1531 |
<span id="cb41-17"><a href="#cb41-17" tabindex="-1"></a><span class="co"># 10 BWA VA -0.729 -0.716 -0.667 -0.803 -0.690 -0.603 -0.588 -0.635 -0.656 -0.596 -0.675</span></span> |
| 1532 |
<span id="cb41-18"><a href="#cb41-18" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1533 |
<span id="cb41-19"><a href="#cb41-19" tabindex="-1"></a><span class="co"># </span></span> |
| 1534 |
<span id="cb41-20"><a href="#cb41-20" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1535 |
<p><code>fscale</code> also has additional <code>mean</code> and |
| 1536 |
<code>sd</code> arguments allowing the user to (group-) scale data to an |
| 1537 |
arbitrary mean and standard deviation. Setting <code>mean = FALSE</code> |
| 1538 |
just scales the data but preserves the means, and is thus different from |
| 1539 |
<code>fsd(..., TRA = "/")</code> which simply divides all values by the |
| 1540 |
standard deviation:</p> |
| 1541 |
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" tabindex="-1"></a><span class="co"># Saving grouped tibble</span></span> |
| 1542 |
<span id="cb42-2"><a href="#cb42-2" tabindex="-1"></a>gGGDC <span class="ot"><-</span> GGDC10S <span class="sc">%>%</span></span> |
| 1543 |
<span id="cb42-3"><a href="#cb42-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span></span> |
| 1544 |
<span id="cb42-4"><a href="#cb42-4" tabindex="-1"></a> <span class="fu">fselect</span>(Country, Variable, AGR<span class="sc">:</span>SUM)</span> |
| 1545 |
<span id="cb42-5"><a href="#cb42-5" tabindex="-1"></a></span> |
| 1546 |
<span id="cb42-6"><a href="#cb42-6" tabindex="-1"></a><span class="co"># Original means</span></span> |
| 1547 |
<span id="cb42-7"><a href="#cb42-7" tabindex="-1"></a><span class="fu">head</span>(<span class="fu">fmean</span>(gGGDC)) </span> |
| 1548 |
<span id="cb42-8"><a href="#cb42-8" tabindex="-1"></a><span class="co"># # A tibble: 6 × 13</span></span> |
| 1549 |
<span id="cb42-9"><a href="#cb42-9" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1550 |
<span id="cb42-10"><a href="#cb42-10" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1551 |
<span id="cb42-11"><a href="#cb42-11" tabindex="-1"></a><span class="co"># 1 EMP ARG 1420. 52.1 1932. 102. 742. 1.98e3 6.49e2 628. 2043. 9.92e2 1.05e4</span></span> |
| 1552 |
<span id="cb42-12"><a href="#cb42-12" tabindex="-1"></a><span class="co"># 2 EMP BOL 964. 56.0 235. 5.35 123. 2.82e2 1.15e2 44.6 NA 3.96e2 2.22e3</span></span> |
| 1553 |
<span id="cb42-13"><a href="#cb42-13" tabindex="-1"></a><span class="co"># 3 EMP BRA 17191. 206. 6991. 365. 3525. 8.51e3 2.05e3 4414. 5307. 5.71e3 5.43e4</span></span> |
| 1554 |
<span id="cb42-14"><a href="#cb42-14" tabindex="-1"></a><span class="co"># 4 EMP BWA 188. 10.5 18.1 3.09 25.3 3.63e1 8.36e0 15.3 61.1 2.76e1 3.94e2</span></span> |
| 1555 |
<span id="cb42-15"><a href="#cb42-15" tabindex="-1"></a><span class="co"># 5 EMP CHL 702. 101. 625. 29.4 296. 6.95e2 2.58e2 272. NA 1.00e3 3.98e3</span></span> |
| 1556 |
<span id="cb42-16"><a href="#cb42-16" tabindex="-1"></a><span class="co"># 6 EMP CHN 287744. 7050. 67144. 1606. 20852. 2.89e4 1.39e4 4929. 22669. 3.10e4 4.86e5</span></span> |
| 1557 |
<span id="cb42-17"><a href="#cb42-17" tabindex="-1"></a></span> |
| 1558 |
<span id="cb42-18"><a href="#cb42-18" tabindex="-1"></a><span class="co"># Mean Preserving Scaling</span></span> |
| 1559 |
<span id="cb42-19"><a href="#cb42-19" tabindex="-1"></a><span class="fu">head</span>(<span class="fu">fmean</span>(<span class="fu">fscale</span>(gGGDC, <span class="at">mean =</span> <span class="cn">FALSE</span>)))</span> |
| 1560 |
<span id="cb42-20"><a href="#cb42-20" tabindex="-1"></a><span class="co"># # A tibble: 6 × 13</span></span> |
| 1561 |
<span id="cb42-21"><a href="#cb42-21" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1562 |
<span id="cb42-22"><a href="#cb42-22" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1563 |
<span id="cb42-23"><a href="#cb42-23" tabindex="-1"></a><span class="co"># 1 EMP ARG 1420. 52.1 1932. 102. 742. 1.98e3 6.49e2 628. 2043. 9.92e2 1.05e4</span></span> |
| 1564 |
<span id="cb42-24"><a href="#cb42-24" tabindex="-1"></a><span class="co"># 2 EMP BOL 964. 56.0 235. 5.35 123. 2.82e2 1.15e2 44.6 NA 3.96e2 2.22e3</span></span> |
| 1565 |
<span id="cb42-25"><a href="#cb42-25" tabindex="-1"></a><span class="co"># 3 EMP BRA 17191. 206. 6991. 365. 3525. 8.51e3 2.05e3 4414. 5307. 5.71e3 5.43e4</span></span> |
| 1566 |
<span id="cb42-26"><a href="#cb42-26" tabindex="-1"></a><span class="co"># 4 EMP BWA 188. 10.5 18.1 3.09 25.3 3.63e1 8.36e0 15.3 61.1 2.76e1 3.94e2</span></span> |
| 1567 |
<span id="cb42-27"><a href="#cb42-27" tabindex="-1"></a><span class="co"># 5 EMP CHL 702. 101. 625. 29.4 296. 6.95e2 2.58e2 272. NA 1.00e3 3.98e3</span></span> |
| 1568 |
<span id="cb42-28"><a href="#cb42-28" tabindex="-1"></a><span class="co"># 6 EMP CHN 287744. 7050. 67144. 1606. 20852. 2.89e4 1.39e4 4929. 22669. 3.10e4 4.86e5</span></span> |
| 1569 |
<span id="cb42-29"><a href="#cb42-29" tabindex="-1"></a><span class="fu">head</span>(<span class="fu">fsd</span>(<span class="fu">fscale</span>(gGGDC, <span class="at">mean =</span> <span class="cn">FALSE</span>)))</span> |
| 1570 |
<span id="cb42-30"><a href="#cb42-30" tabindex="-1"></a><span class="co"># # A tibble: 6 × 13</span></span> |
| 1571 |
<span id="cb42-31"><a href="#cb42-31" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1572 |
<span id="cb42-32"><a href="#cb42-32" tabindex="-1"></a><span class="co"># <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1573 |
<span id="cb42-33"><a href="#cb42-33" tabindex="-1"></a><span class="co"># 1 EMP ARG 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00</span></span> |
| 1574 |
<span id="cb42-34"><a href="#cb42-34" tabindex="-1"></a><span class="co"># 2 EMP BOL 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 NA 1.00 1.00</span></span> |
| 1575 |
<span id="cb42-35"><a href="#cb42-35" tabindex="-1"></a><span class="co"># 3 EMP BRA 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00</span></span> |
| 1576 |
<span id="cb42-36"><a href="#cb42-36" tabindex="-1"></a><span class="co"># 4 EMP BWA 1.00 1.00 1.00 1 1.00 1.00 1.00 1 1.00 1.00 1.00</span></span> |
| 1577 |
<span id="cb42-37"><a href="#cb42-37" tabindex="-1"></a><span class="co"># 5 EMP CHL 1.00 1 1.00 1.00 1.00 1.00 1.00 1.00 NA 1.00 1.00</span></span> |
| 1578 |
<span id="cb42-38"><a href="#cb42-38" tabindex="-1"></a><span class="co"># 6 EMP CHN 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00</span></span></code></pre></div> |
| 1579 |
<p>One can also set <code>mean = "overall.mean"</code>, which |
| 1580 |
group-centers columns on the overall mean as illustrated with |
| 1581 |
<code>fwithin</code>. Another interesting option is setting |
| 1582 |
<code>sd = "within.sd"</code>. This group-scales data such that every |
| 1583 |
group has a standard deviation equal to the within-standard deviation of |
| 1584 |
the data:</p> |
| 1585 |
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" tabindex="-1"></a><span class="co"># Just using VA data for this example</span></span> |
| 1586 |
<span id="cb43-2"><a href="#cb43-2" tabindex="-1"></a>gGGDC <span class="ot"><-</span> GGDC10S <span class="sc">%>%</span></span> |
| 1587 |
<span id="cb43-3"><a href="#cb43-3" tabindex="-1"></a> <span class="fu">fsubset</span>(Variable <span class="sc">==</span> <span class="st">"VA"</span>, Country, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 1588 |
<span id="cb43-4"><a href="#cb43-4" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Country)</span> |
| 1589 |
<span id="cb43-5"><a href="#cb43-5" tabindex="-1"></a></span> |
| 1590 |
<span id="cb43-6"><a href="#cb43-6" tabindex="-1"></a><span class="co"># This calculates the within- standard deviation for all columns</span></span> |
| 1591 |
<span id="cb43-7"><a href="#cb43-7" tabindex="-1"></a><span class="fu">fsd</span>(<span class="fu">num_vars</span>(<span class="fu">ungroup</span>(<span class="fu">fwithin</span>(gGGDC))))</span> |
| 1592 |
<span id="cb43-8"><a href="#cb43-8" tabindex="-1"></a><span class="co"># AGR MIN MAN PU CON WRT TRA FIRE GOV OTH </span></span> |
| 1593 |
<span id="cb43-9"><a href="#cb43-9" tabindex="-1"></a><span class="co"># 45046972 40122220 75608708 3062688 30811572 44125207 20676901 16030868 20358973 18780869 </span></span> |
| 1594 |
<span id="cb43-10"><a href="#cb43-10" tabindex="-1"></a><span class="co"># SUM </span></span> |
| 1595 |
<span id="cb43-11"><a href="#cb43-11" tabindex="-1"></a><span class="co"># 306429102</span></span> |
| 1596 |
<span id="cb43-12"><a href="#cb43-12" tabindex="-1"></a></span> |
| 1597 |
<span id="cb43-13"><a href="#cb43-13" tabindex="-1"></a><span class="co"># This scales all groups to take on the within- standard deviation while preserving group means </span></span> |
| 1598 |
<span id="cb43-14"><a href="#cb43-14" tabindex="-1"></a><span class="fu">fsd</span>(<span class="fu">fscale</span>(gGGDC, <span class="at">mean =</span> <span class="cn">FALSE</span>, <span class="at">sd =</span> <span class="st">"within.sd"</span>))</span> |
| 1599 |
<span id="cb43-15"><a href="#cb43-15" tabindex="-1"></a><span class="co"># # A tibble: 43 × 12</span></span> |
| 1600 |
<span id="cb43-16"><a href="#cb43-16" tabindex="-1"></a><span class="co"># Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1601 |
<span id="cb43-17"><a href="#cb43-17" tabindex="-1"></a><span class="co"># <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1602 |
<span id="cb43-18"><a href="#cb43-18" tabindex="-1"></a><span class="co"># 1 ARG 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 2.04e7 1.88e7 3.06e8</span></span> |
| 1603 |
<span id="cb43-19"><a href="#cb43-19" tabindex="-1"></a><span class="co"># 2 BOL 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 NA 1.88e7 3.06e8</span></span> |
| 1604 |
<span id="cb43-20"><a href="#cb43-20" tabindex="-1"></a><span class="co"># 3 BRA 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 2.04e7 1.88e7 3.06e8</span></span> |
| 1605 |
<span id="cb43-21"><a href="#cb43-21" tabindex="-1"></a><span class="co"># 4 BWA 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 2.04e7 1.88e7 3.06e8</span></span> |
| 1606 |
<span id="cb43-22"><a href="#cb43-22" tabindex="-1"></a><span class="co"># 5 CHL 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 NA 1.88e7 3.06e8</span></span> |
| 1607 |
<span id="cb43-23"><a href="#cb43-23" tabindex="-1"></a><span class="co"># 6 CHN 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 2.04e7 1.88e7 3.06e8</span></span> |
| 1608 |
<span id="cb43-24"><a href="#cb43-24" tabindex="-1"></a><span class="co"># 7 COL 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 NA 1.88e7 3.06e8</span></span> |
| 1609 |
<span id="cb43-25"><a href="#cb43-25" tabindex="-1"></a><span class="co"># 8 CRI 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 2.04e7 1.88e7 3.06e8</span></span> |
| 1610 |
<span id="cb43-26"><a href="#cb43-26" tabindex="-1"></a><span class="co"># 9 DEW 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 2.04e7 1.88e7 3.06e8</span></span> |
| 1611 |
<span id="cb43-27"><a href="#cb43-27" tabindex="-1"></a><span class="co"># 10 DNK 45046972. 40122220. 75608708. 3062688. 3.08e7 4.41e7 2.07e7 1.60e7 2.04e7 1.88e7 3.06e8</span></span> |
| 1612 |
<span id="cb43-28"><a href="#cb43-28" tabindex="-1"></a><span class="co"># # ℹ 33 more rows</span></span></code></pre></div> |
| 1613 |
<p>A grouped scaling operation with both |
| 1614 |
<code>mean = "overall.mean"</code> and <code>sd = "within.sd"</code> |
| 1615 |
thus efficiently achieves a harmonization of all groups in the first two |
| 1616 |
moments without changing the fundamental properties (in terms of level |
| 1617 |
and scale) of the data.</p> |
| 1618 |
</div> |
| 1619 |
<div id="lags-leads-differences-and-growth-rates" class="section level3"> |
| 1620 |
<h3>2.5 Lags / Leads, Differences and Growth Rates</h3> |
| 1621 |
<!-- It was suggested some time ago that leaving the best wine for the end is not the best strategy when giving a feast. Considering the marriage of *collapse* and *dplyr* the 3 functions for time-computations introduced in this section combine great flexibility with precision and computing power, and feature amongst the highlights of *collapse*. --> |
| 1622 |
<p>This section introduces 3 further powerful <em>collapse</em> |
| 1623 |
functions: <code>flag</code>, <code>fdiff</code> and |
| 1624 |
<code>fgrowth</code>. The first function, <code>flag</code>, efficiently |
| 1625 |
computes sequences of fully identified lags and leads on time series and |
| 1626 |
panel data. The following code computes 1 fully-identified panel-lag and |
| 1627 |
1 fully identified panel-lead of each variable in the data:</p> |
| 1628 |
<!-- In addition: None of these functions require the data to be sorted, they can carry out fast computations on completely unordered data as long as a time-variable is supplied that uniquely identifies the data. --> |
| 1629 |
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1630 |
<span id="cb44-2"><a href="#cb44-2" tabindex="-1"></a> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode) <span class="sc">%>%</span> </span> |
| 1631 |
<span id="cb44-3"><a href="#cb44-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">flag</span>(<span class="sc">-</span><span class="dv">1</span><span class="sc">:</span><span class="dv">1</span>, Year)</span> |
| 1632 |
<span id="cb44-4"><a href="#cb44-4" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 36</span></span> |
| 1633 |
<span id="cb44-5"><a href="#cb44-5" tabindex="-1"></a><span class="co"># Country Variable Year F1.AGR AGR L1.AGR F1.MIN MIN L1.MIN F1.MAN MAN L1.MAN F1.PU PU</span></span> |
| 1634 |
<span id="cb44-6"><a href="#cb44-6" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1635 |
<span id="cb44-7"><a href="#cb44-7" tabindex="-1"></a><span class="co"># 1 BWA VA 1960 NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1636 |
<span id="cb44-8"><a href="#cb44-8" tabindex="-1"></a><span class="co"># 2 BWA VA 1961 NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1637 |
<span id="cb44-9"><a href="#cb44-9" tabindex="-1"></a><span class="co"># 3 BWA VA 1962 NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1638 |
<span id="cb44-10"><a href="#cb44-10" tabindex="-1"></a><span class="co"># 4 BWA VA 1963 16.3 NA NA 3.49 NA NA 0.737 NA NA 0.104 NA </span></span> |
| 1639 |
<span id="cb44-11"><a href="#cb44-11" tabindex="-1"></a><span class="co"># 5 BWA VA 1964 15.7 16.3 NA 2.50 3.49 NA 1.02 0.737 NA 0.135 0.104</span></span> |
| 1640 |
<span id="cb44-12"><a href="#cb44-12" tabindex="-1"></a><span class="co"># 6 BWA VA 1965 17.7 15.7 16.3 1.97 2.50 3.49 0.804 1.02 0.737 0.203 0.135</span></span> |
| 1641 |
<span id="cb44-13"><a href="#cb44-13" tabindex="-1"></a><span class="co"># 7 BWA VA 1966 19.1 17.7 15.7 2.30 1.97 2.50 0.938 0.804 1.02 0.203 0.203</span></span> |
| 1642 |
<span id="cb44-14"><a href="#cb44-14" tabindex="-1"></a><span class="co"># 8 BWA VA 1967 21.1 19.1 17.7 1.84 2.30 1.97 0.750 0.938 0.804 0.203 0.203</span></span> |
| 1643 |
<span id="cb44-15"><a href="#cb44-15" tabindex="-1"></a><span class="co"># 9 BWA VA 1968 21.9 21.1 19.1 5.24 1.84 2.30 2.14 0.750 0.938 0.578 0.203</span></span> |
| 1644 |
<span id="cb44-16"><a href="#cb44-16" tabindex="-1"></a><span class="co"># 10 BWA VA 1969 23.1 21.9 21.1 10.2 5.24 1.84 4.15 2.14 0.750 1.12 0.578</span></span> |
| 1645 |
<span id="cb44-17"><a href="#cb44-17" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1646 |
<span id="cb44-18"><a href="#cb44-18" tabindex="-1"></a><span class="co"># # ℹ 22 more variables: L1.PU <dbl>, F1.CON <dbl>, CON <dbl>, L1.CON <dbl>, F1.WRT <dbl>, WRT <dbl>,</span></span> |
| 1647 |
<span id="cb44-19"><a href="#cb44-19" tabindex="-1"></a><span class="co"># # L1.WRT <dbl>, F1.TRA <dbl>, TRA <dbl>, L1.TRA <dbl>, F1.FIRE <dbl>, FIRE <dbl>, L1.FIRE <dbl>,</span></span> |
| 1648 |
<span id="cb44-20"><a href="#cb44-20" tabindex="-1"></a><span class="co"># # F1.GOV <dbl>, GOV <dbl>, L1.GOV <dbl>, F1.OTH <dbl>, OTH <dbl>, L1.OTH <dbl>, F1.SUM <dbl>,</span></span> |
| 1649 |
<span id="cb44-21"><a href="#cb44-21" tabindex="-1"></a><span class="co"># # SUM <dbl>, L1.SUM <dbl></span></span> |
| 1650 |
<span id="cb44-22"><a href="#cb44-22" tabindex="-1"></a><span class="co"># </span></span> |
| 1651 |
<span id="cb44-23"><a href="#cb44-23" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1652 |
<p>If the time-variable passed does not exactly identify the data |
| 1653 |
(i.e. because of repeated values in each group), all 3 functions will |
| 1654 |
issue appropriate error messages. <code>flag</code>, <code>fdiff</code> |
| 1655 |
and <code>fgrowth</code> support irregular time series and unbalanced |
| 1656 |
panels. |
| 1657 |
<!-- with different start and end periods and duration of coverage for each individual, but not irregular panels. A workaround for such panels exists with the function `seqid` which generates a new panel-id identifying consecutive time-sequences at the sub-individual level, see `?seqid`. --></p> |
| 1658 |
<p>It is also possible to omit the time-variable if one is certain that |
| 1659 |
the data is sorted:</p> |
| 1660 |
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1661 |
<span id="cb45-2"><a href="#cb45-2" tabindex="-1"></a> <span class="fu">fselect</span>(Variable, Country,AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 1662 |
<span id="cb45-3"><a href="#cb45-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> flag</span> |
| 1663 |
<span id="cb45-4"><a href="#cb45-4" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 13</span></span> |
| 1664 |
<span id="cb45-5"><a href="#cb45-5" tabindex="-1"></a><span class="co"># Variable Country AGR MIN MAN PU CON WRT TRA FIRE GOV OTH SUM</span></span> |
| 1665 |
<span id="cb45-6"><a href="#cb45-6" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1666 |
<span id="cb45-7"><a href="#cb45-7" tabindex="-1"></a><span class="co"># 1 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1667 |
<span id="cb45-8"><a href="#cb45-8" tabindex="-1"></a><span class="co"># 2 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1668 |
<span id="cb45-9"><a href="#cb45-9" tabindex="-1"></a><span class="co"># 3 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1669 |
<span id="cb45-10"><a href="#cb45-10" tabindex="-1"></a><span class="co"># 4 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1670 |
<span id="cb45-11"><a href="#cb45-11" tabindex="-1"></a><span class="co"># 5 VA BWA NA NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1671 |
<span id="cb45-12"><a href="#cb45-12" tabindex="-1"></a><span class="co"># 6 VA BWA 16.3 3.49 0.737 0.104 0.660 6.24 1.66 1.12 4.82 2.34 37.5</span></span> |
| 1672 |
<span id="cb45-13"><a href="#cb45-13" tabindex="-1"></a><span class="co"># 7 VA BWA 15.7 2.50 1.02 0.135 1.35 7.06 1.94 1.25 5.70 2.68 39.3</span></span> |
| 1673 |
<span id="cb45-14"><a href="#cb45-14" tabindex="-1"></a><span class="co"># 8 VA BWA 17.7 1.97 0.804 0.203 1.35 8.27 2.15 1.36 6.37 2.99 43.1</span></span> |
| 1674 |
<span id="cb45-15"><a href="#cb45-15" tabindex="-1"></a><span class="co"># 9 VA BWA 19.1 2.30 0.938 0.203 0.897 4.31 1.72 1.54 7.04 3.31 41.4</span></span> |
| 1675 |
<span id="cb45-16"><a href="#cb45-16" tabindex="-1"></a><span class="co"># 10 VA BWA 21.1 1.84 0.750 0.203 1.22 5.17 2.44 1.03 5.03 2.36 41.1</span></span> |
| 1676 |
<span id="cb45-17"><a href="#cb45-17" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1677 |
<span id="cb45-18"><a href="#cb45-18" tabindex="-1"></a><span class="co"># </span></span> |
| 1678 |
<span id="cb45-19"><a href="#cb45-19" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1679 |
<p><code>fdiff</code> computes sequences of lagged-leaded and iterated |
| 1680 |
differences as well as quasi-differences and log-differences on time |
| 1681 |
series and panel data. The code below computes the 1 and 10 year first |
| 1682 |
and second differences of each variable in the data:</p> |
| 1683 |
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1684 |
<span id="cb46-2"><a href="#cb46-2" tabindex="-1"></a> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode) <span class="sc">%>%</span> </span> |
| 1685 |
<span id="cb46-3"><a href="#cb46-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fdiff</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>), <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, Year)</span> |
| 1686 |
<span id="cb46-4"><a href="#cb46-4" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 47</span></span> |
| 1687 |
<span id="cb46-5"><a href="#cb46-5" tabindex="-1"></a><span class="co"># Country Variable Year D1.AGR D2.AGR L10D1.AGR L10D2.AGR D1.MIN D2.MIN L10D1.MIN L10D2.MIN D1.MAN</span></span> |
| 1688 |
<span id="cb46-6"><a href="#cb46-6" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1689 |
<span id="cb46-7"><a href="#cb46-7" tabindex="-1"></a><span class="co"># 1 BWA VA 1960 NA NA NA NA NA NA NA NA NA </span></span> |
| 1690 |
<span id="cb46-8"><a href="#cb46-8" tabindex="-1"></a><span class="co"># 2 BWA VA 1961 NA NA NA NA NA NA NA NA NA </span></span> |
| 1691 |
<span id="cb46-9"><a href="#cb46-9" tabindex="-1"></a><span class="co"># 3 BWA VA 1962 NA NA NA NA NA NA NA NA NA </span></span> |
| 1692 |
<span id="cb46-10"><a href="#cb46-10" tabindex="-1"></a><span class="co"># 4 BWA VA 1963 NA NA NA NA NA NA NA NA NA </span></span> |
| 1693 |
<span id="cb46-11"><a href="#cb46-11" tabindex="-1"></a><span class="co"># 5 BWA VA 1964 NA NA NA NA NA NA NA NA NA </span></span> |
| 1694 |
<span id="cb46-12"><a href="#cb46-12" tabindex="-1"></a><span class="co"># 6 BWA VA 1965 -0.575 NA NA NA -0.998 NA NA NA 0.282</span></span> |
| 1695 |
<span id="cb46-13"><a href="#cb46-13" tabindex="-1"></a><span class="co"># 7 BWA VA 1966 1.95 2.53 NA NA -0.525 0.473 NA NA -0.214</span></span> |
| 1696 |
<span id="cb46-14"><a href="#cb46-14" tabindex="-1"></a><span class="co"># 8 BWA VA 1967 1.47 -0.488 NA NA 0.328 0.854 NA NA 0.134</span></span> |
| 1697 |
<span id="cb46-15"><a href="#cb46-15" tabindex="-1"></a><span class="co"># 9 BWA VA 1968 1.95 0.488 NA NA -0.460 -0.788 NA NA -0.188</span></span> |
| 1698 |
<span id="cb46-16"><a href="#cb46-16" tabindex="-1"></a><span class="co"># 10 BWA VA 1969 0.763 -1.19 NA NA 3.41 3.87 NA NA 1.39 </span></span> |
| 1699 |
<span id="cb46-17"><a href="#cb46-17" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1700 |
<span id="cb46-18"><a href="#cb46-18" tabindex="-1"></a><span class="co"># # ℹ 35 more variables: D2.MAN <dbl>, L10D1.MAN <dbl>, L10D2.MAN <dbl>, D1.PU <dbl>, D2.PU <dbl>,</span></span> |
| 1701 |
<span id="cb46-19"><a href="#cb46-19" tabindex="-1"></a><span class="co"># # L10D1.PU <dbl>, L10D2.PU <dbl>, D1.CON <dbl>, D2.CON <dbl>, L10D1.CON <dbl>, L10D2.CON <dbl>,</span></span> |
| 1702 |
<span id="cb46-20"><a href="#cb46-20" tabindex="-1"></a><span class="co"># # D1.WRT <dbl>, D2.WRT <dbl>, L10D1.WRT <dbl>, L10D2.WRT <dbl>, D1.TRA <dbl>, D2.TRA <dbl>,</span></span> |
| 1703 |
<span id="cb46-21"><a href="#cb46-21" tabindex="-1"></a><span class="co"># # L10D1.TRA <dbl>, L10D2.TRA <dbl>, D1.FIRE <dbl>, D2.FIRE <dbl>, L10D1.FIRE <dbl>,</span></span> |
| 1704 |
<span id="cb46-22"><a href="#cb46-22" tabindex="-1"></a><span class="co"># # L10D2.FIRE <dbl>, D1.GOV <dbl>, D2.GOV <dbl>, L10D1.GOV <dbl>, L10D2.GOV <dbl>, D1.OTH <dbl>,</span></span> |
| 1705 |
<span id="cb46-23"><a href="#cb46-23" tabindex="-1"></a><span class="co"># # D2.OTH <dbl>, L10D1.OTH <dbl>, L10D2.OTH <dbl>, D1.SUM <dbl>, D2.SUM <dbl>, L10D1.SUM <dbl>, …</span></span> |
| 1706 |
<span id="cb46-24"><a href="#cb46-24" tabindex="-1"></a><span class="co"># </span></span> |
| 1707 |
<span id="cb46-25"><a href="#cb46-25" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1708 |
<p>Log-differences of the form <span class="math inline">\(log(x_t) - |
| 1709 |
log(x_{t-s})\)</span> are also easily computed.</p> |
| 1710 |
<div class="sourceCode" id="cb47"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1711 |
<span id="cb47-2"><a href="#cb47-2" tabindex="-1"></a> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode) <span class="sc">%>%</span> </span> |
| 1712 |
<span id="cb47-3"><a href="#cb47-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fdiff</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>), <span class="dv">1</span>, Year, <span class="at">log =</span> <span class="cn">TRUE</span>)</span> |
| 1713 |
<span id="cb47-4"><a href="#cb47-4" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 25</span></span> |
| 1714 |
<span id="cb47-5"><a href="#cb47-5" tabindex="-1"></a><span class="co"># Country Variable Year Dlog1.AGR L10Dlog1.AGR Dlog1.MIN L10Dlog1.MIN Dlog1.MAN L10Dlog1.MAN</span></span> |
| 1715 |
<span id="cb47-6"><a href="#cb47-6" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1716 |
<span id="cb47-7"><a href="#cb47-7" tabindex="-1"></a><span class="co"># 1 BWA VA 1960 NA NA NA NA NA NA</span></span> |
| 1717 |
<span id="cb47-8"><a href="#cb47-8" tabindex="-1"></a><span class="co"># 2 BWA VA 1961 NA NA NA NA NA NA</span></span> |
| 1718 |
<span id="cb47-9"><a href="#cb47-9" tabindex="-1"></a><span class="co"># 3 BWA VA 1962 NA NA NA NA NA NA</span></span> |
| 1719 |
<span id="cb47-10"><a href="#cb47-10" tabindex="-1"></a><span class="co"># 4 BWA VA 1963 NA NA NA NA NA NA</span></span> |
| 1720 |
<span id="cb47-11"><a href="#cb47-11" tabindex="-1"></a><span class="co"># 5 BWA VA 1964 NA NA NA NA NA NA</span></span> |
| 1721 |
<span id="cb47-12"><a href="#cb47-12" tabindex="-1"></a><span class="co"># 6 BWA VA 1965 -0.0359 NA -0.336 NA 0.324 NA</span></span> |
| 1722 |
<span id="cb47-13"><a href="#cb47-13" tabindex="-1"></a><span class="co"># 7 BWA VA 1966 0.117 NA -0.236 NA -0.236 NA</span></span> |
| 1723 |
<span id="cb47-14"><a href="#cb47-14" tabindex="-1"></a><span class="co"># 8 BWA VA 1967 0.0796 NA 0.154 NA 0.154 NA</span></span> |
| 1724 |
<span id="cb47-15"><a href="#cb47-15" tabindex="-1"></a><span class="co"># 9 BWA VA 1968 0.0972 NA -0.223 NA -0.223 NA</span></span> |
| 1725 |
<span id="cb47-16"><a href="#cb47-16" tabindex="-1"></a><span class="co"># 10 BWA VA 1969 0.0355 NA 1.05 NA 1.05 NA</span></span> |
| 1726 |
<span id="cb47-17"><a href="#cb47-17" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1727 |
<span id="cb47-18"><a href="#cb47-18" tabindex="-1"></a><span class="co"># # ℹ 16 more variables: Dlog1.PU <dbl>, L10Dlog1.PU <dbl>, Dlog1.CON <dbl>, L10Dlog1.CON <dbl>,</span></span> |
| 1728 |
<span id="cb47-19"><a href="#cb47-19" tabindex="-1"></a><span class="co"># # Dlog1.WRT <dbl>, L10Dlog1.WRT <dbl>, Dlog1.TRA <dbl>, L10Dlog1.TRA <dbl>, Dlog1.FIRE <dbl>,</span></span> |
| 1729 |
<span id="cb47-20"><a href="#cb47-20" tabindex="-1"></a><span class="co"># # L10Dlog1.FIRE <dbl>, Dlog1.GOV <dbl>, L10Dlog1.GOV <dbl>, Dlog1.OTH <dbl>, L10Dlog1.OTH <dbl>,</span></span> |
| 1730 |
<span id="cb47-21"><a href="#cb47-21" tabindex="-1"></a><span class="co"># # Dlog1.SUM <dbl>, L10Dlog1.SUM <dbl></span></span> |
| 1731 |
<span id="cb47-22"><a href="#cb47-22" tabindex="-1"></a><span class="co"># </span></span> |
| 1732 |
<span id="cb47-23"><a href="#cb47-23" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1733 |
<p>Finally, it is also possible to compute quasi-differences and |
| 1734 |
quasi-log-differences of the form <span class="math inline">\(x_t - \rho |
| 1735 |
x_{t-s}\)</span> or <span class="math inline">\(log(x_t) - \rho |
| 1736 |
log(x_{t-s})\)</span>:</p> |
| 1737 |
<div class="sourceCode" id="cb48"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1738 |
<span id="cb48-2"><a href="#cb48-2" tabindex="-1"></a> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode) <span class="sc">%>%</span> </span> |
| 1739 |
<span id="cb48-3"><a href="#cb48-3" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fdiff</span>(<span class="at">t =</span> Year, <span class="at">rho =</span> <span class="fl">0.95</span>)</span> |
| 1740 |
<span id="cb48-4"><a href="#cb48-4" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 14</span></span> |
| 1741 |
<span id="cb48-5"><a href="#cb48-5" tabindex="-1"></a><span class="co"># Country Variable Year AGR MIN MAN PU CON WRT TRA FIRE GOV OTH</span></span> |
| 1742 |
<span id="cb48-6"><a href="#cb48-6" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1743 |
<span id="cb48-7"><a href="#cb48-7" tabindex="-1"></a><span class="co"># 1 BWA VA 1960 NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1744 |
<span id="cb48-8"><a href="#cb48-8" tabindex="-1"></a><span class="co"># 2 BWA VA 1961 NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1745 |
<span id="cb48-9"><a href="#cb48-9" tabindex="-1"></a><span class="co"># 3 BWA VA 1962 NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1746 |
<span id="cb48-10"><a href="#cb48-10" tabindex="-1"></a><span class="co"># 4 BWA VA 1963 NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1747 |
<span id="cb48-11"><a href="#cb48-11" tabindex="-1"></a><span class="co"># 5 BWA VA 1964 NA NA NA NA NA NA NA NA NA NA </span></span> |
| 1748 |
<span id="cb48-12"><a href="#cb48-12" tabindex="-1"></a><span class="co"># 6 BWA VA 1965 0.241 -0.824 0.318 0.0359 0.719 1.13 0.363 0.184 1.11 0.454</span></span> |
| 1749 |
<span id="cb48-13"><a href="#cb48-13" tabindex="-1"></a><span class="co"># 7 BWA VA 1966 2.74 -0.401 -0.163 0.0743 0.0673 1.56 0.312 0.174 0.955 0.449</span></span> |
| 1750 |
<span id="cb48-14"><a href="#cb48-14" tabindex="-1"></a><span class="co"># 8 BWA VA 1967 2.35 0.427 0.174 0.0101 -0.381 -3.55 -0.323 0.246 0.988 0.465</span></span> |
| 1751 |
<span id="cb48-15"><a href="#cb48-15" tabindex="-1"></a><span class="co"># 9 BWA VA 1968 2.91 -0.345 -0.141 0.0101 0.365 1.08 0.804 -0.427 -1.66 -0.780</span></span> |
| 1752 |
<span id="cb48-16"><a href="#cb48-16" tabindex="-1"></a><span class="co"># 10 BWA VA 1969 1.82 3.50 1.43 0.385 2.32 0.841 0.397 0.252 0.818 0.385</span></span> |
| 1753 |
<span id="cb48-17"><a href="#cb48-17" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1754 |
<span id="cb48-18"><a href="#cb48-18" tabindex="-1"></a><span class="co"># # ℹ 1 more variable: SUM <dbl></span></span> |
| 1755 |
<span id="cb48-19"><a href="#cb48-19" tabindex="-1"></a><span class="co"># </span></span> |
| 1756 |
<span id="cb48-20"><a href="#cb48-20" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1757 |
<p>The quasi-differencing feature was added to <code>fdiff</code> to |
| 1758 |
facilitate the preparation of time series and panel data for |
| 1759 |
least-squares estimations suffering from serial correlation following |
| 1760 |
Cochrane & Orcutt (1949).</p> |
| 1761 |
<!-- and `fgrowth` computes lagged-leaded and iterated growth-rates obtained via the exact computation method or through log-differencing. --> |
| 1762 |
<p>Finally, <code>fgrowth</code> computes growth rates in the same way. |
| 1763 |
By default exact growth rates are computed in percentage terms using |
| 1764 |
<span class="math inline">\((x_t-x_{t-s}) / x_{t-s} \times 100\)</span> |
| 1765 |
(the default argument is <code>scale = 100</code>). The user can also |
| 1766 |
request growth rates obtained by log-differencing using <span class="math inline">\(log(x_t/ x_{t-s}) \times 100\)</span>.</p> |
| 1767 |
<div class="sourceCode" id="cb49"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" tabindex="-1"></a><span class="co"># Exact growth rates, computed as: (x/lag(x) - 1) * 100</span></span> |
| 1768 |
<span id="cb49-2"><a href="#cb49-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1769 |
<span id="cb49-3"><a href="#cb49-3" tabindex="-1"></a> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode) <span class="sc">%>%</span> </span> |
| 1770 |
<span id="cb49-4"><a href="#cb49-4" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgrowth</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>), <span class="dv">1</span>, Year)</span> |
| 1771 |
<span id="cb49-5"><a href="#cb49-5" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 25</span></span> |
| 1772 |
<span id="cb49-6"><a href="#cb49-6" tabindex="-1"></a><span class="co"># Country Variable Year G1.AGR L10G1.AGR G1.MIN L10G1.MIN G1.MAN L10G1.MAN G1.PU L10G1.PU G1.CON</span></span> |
| 1773 |
<span id="cb49-7"><a href="#cb49-7" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1774 |
<span id="cb49-8"><a href="#cb49-8" tabindex="-1"></a><span class="co"># 1 BWA VA 1960 NA NA NA NA NA NA NA NA NA </span></span> |
| 1775 |
<span id="cb49-9"><a href="#cb49-9" tabindex="-1"></a><span class="co"># 2 BWA VA 1961 NA NA NA NA NA NA NA NA NA </span></span> |
| 1776 |
<span id="cb49-10"><a href="#cb49-10" tabindex="-1"></a><span class="co"># 3 BWA VA 1962 NA NA NA NA NA NA NA NA NA </span></span> |
| 1777 |
<span id="cb49-11"><a href="#cb49-11" tabindex="-1"></a><span class="co"># 4 BWA VA 1963 NA NA NA NA NA NA NA NA NA </span></span> |
| 1778 |
<span id="cb49-12"><a href="#cb49-12" tabindex="-1"></a><span class="co"># 5 BWA VA 1964 NA NA NA NA NA NA NA NA NA </span></span> |
| 1779 |
<span id="cb49-13"><a href="#cb49-13" tabindex="-1"></a><span class="co"># 6 BWA VA 1965 -3.52 NA -28.6 NA 38.2 NA 29.4 NA 104. </span></span> |
| 1780 |
<span id="cb49-14"><a href="#cb49-14" tabindex="-1"></a><span class="co"># 7 BWA VA 1966 12.4 NA -21.1 NA -21.1 NA 50 NA 0 </span></span> |
| 1781 |
<span id="cb49-15"><a href="#cb49-15" tabindex="-1"></a><span class="co"># 8 BWA VA 1967 8.29 NA 16.7 NA 16.7 NA 0 NA -33.3</span></span> |
| 1782 |
<span id="cb49-16"><a href="#cb49-16" tabindex="-1"></a><span class="co"># 9 BWA VA 1968 10.2 NA -20 NA -20 NA 0 NA 35.7</span></span> |
| 1783 |
<span id="cb49-17"><a href="#cb49-17" tabindex="-1"></a><span class="co"># 10 BWA VA 1969 3.61 NA 185. NA 185. NA 185. NA 185. </span></span> |
| 1784 |
<span id="cb49-18"><a href="#cb49-18" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1785 |
<span id="cb49-19"><a href="#cb49-19" tabindex="-1"></a><span class="co"># # ℹ 13 more variables: L10G1.CON <dbl>, G1.WRT <dbl>, L10G1.WRT <dbl>, G1.TRA <dbl>,</span></span> |
| 1786 |
<span id="cb49-20"><a href="#cb49-20" tabindex="-1"></a><span class="co"># # L10G1.TRA <dbl>, G1.FIRE <dbl>, L10G1.FIRE <dbl>, G1.GOV <dbl>, L10G1.GOV <dbl>, G1.OTH <dbl>,</span></span> |
| 1787 |
<span id="cb49-21"><a href="#cb49-21" tabindex="-1"></a><span class="co"># # L10G1.OTH <dbl>, G1.SUM <dbl>, L10G1.SUM <dbl></span></span> |
| 1788 |
<span id="cb49-22"><a href="#cb49-22" tabindex="-1"></a><span class="co"># </span></span> |
| 1789 |
<span id="cb49-23"><a href="#cb49-23" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span> |
| 1790 |
<span id="cb49-24"><a href="#cb49-24" tabindex="-1"></a></span> |
| 1791 |
<span id="cb49-25"><a href="#cb49-25" tabindex="-1"></a><span class="co"># Log-difference growth rates, computed as: log(x / lag(x)) * 100</span></span> |
| 1792 |
<span id="cb49-26"><a href="#cb49-26" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1793 |
<span id="cb49-27"><a href="#cb49-27" tabindex="-1"></a> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode) <span class="sc">%>%</span> </span> |
| 1794 |
<span id="cb49-28"><a href="#cb49-28" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgrowth</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>), <span class="dv">1</span>, Year, <span class="at">logdiff =</span> <span class="cn">TRUE</span>)</span> |
| 1795 |
<span id="cb49-29"><a href="#cb49-29" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 25</span></span> |
| 1796 |
<span id="cb49-30"><a href="#cb49-30" tabindex="-1"></a><span class="co"># Country Variable Year Dlog1.AGR L10Dlog1.AGR Dlog1.MIN L10Dlog1.MIN Dlog1.MAN L10Dlog1.MAN</span></span> |
| 1797 |
<span id="cb49-31"><a href="#cb49-31" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1798 |
<span id="cb49-32"><a href="#cb49-32" tabindex="-1"></a><span class="co"># 1 BWA VA 1960 NA NA NA NA NA NA</span></span> |
| 1799 |
<span id="cb49-33"><a href="#cb49-33" tabindex="-1"></a><span class="co"># 2 BWA VA 1961 NA NA NA NA NA NA</span></span> |
| 1800 |
<span id="cb49-34"><a href="#cb49-34" tabindex="-1"></a><span class="co"># 3 BWA VA 1962 NA NA NA NA NA NA</span></span> |
| 1801 |
<span id="cb49-35"><a href="#cb49-35" tabindex="-1"></a><span class="co"># 4 BWA VA 1963 NA NA NA NA NA NA</span></span> |
| 1802 |
<span id="cb49-36"><a href="#cb49-36" tabindex="-1"></a><span class="co"># 5 BWA VA 1964 NA NA NA NA NA NA</span></span> |
| 1803 |
<span id="cb49-37"><a href="#cb49-37" tabindex="-1"></a><span class="co"># 6 BWA VA 1965 -3.59 NA -33.6 NA 32.4 NA</span></span> |
| 1804 |
<span id="cb49-38"><a href="#cb49-38" tabindex="-1"></a><span class="co"># 7 BWA VA 1966 11.7 NA -23.6 NA -23.6 NA</span></span> |
| 1805 |
<span id="cb49-39"><a href="#cb49-39" tabindex="-1"></a><span class="co"># 8 BWA VA 1967 7.96 NA 15.4 NA 15.4 NA</span></span> |
| 1806 |
<span id="cb49-40"><a href="#cb49-40" tabindex="-1"></a><span class="co"># 9 BWA VA 1968 9.72 NA -22.3 NA -22.3 NA</span></span> |
| 1807 |
<span id="cb49-41"><a href="#cb49-41" tabindex="-1"></a><span class="co"># 10 BWA VA 1969 3.55 NA 105. NA 105. NA</span></span> |
| 1808 |
<span id="cb49-42"><a href="#cb49-42" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1809 |
<span id="cb49-43"><a href="#cb49-43" tabindex="-1"></a><span class="co"># # ℹ 16 more variables: Dlog1.PU <dbl>, L10Dlog1.PU <dbl>, Dlog1.CON <dbl>, L10Dlog1.CON <dbl>,</span></span> |
| 1810 |
<span id="cb49-44"><a href="#cb49-44" tabindex="-1"></a><span class="co"># # Dlog1.WRT <dbl>, L10Dlog1.WRT <dbl>, Dlog1.TRA <dbl>, L10Dlog1.TRA <dbl>, Dlog1.FIRE <dbl>,</span></span> |
| 1811 |
<span id="cb49-45"><a href="#cb49-45" tabindex="-1"></a><span class="co"># # L10Dlog1.FIRE <dbl>, Dlog1.GOV <dbl>, L10Dlog1.GOV <dbl>, Dlog1.OTH <dbl>, L10Dlog1.OTH <dbl>,</span></span> |
| 1812 |
<span id="cb49-46"><a href="#cb49-46" tabindex="-1"></a><span class="co"># # Dlog1.SUM <dbl>, L10Dlog1.SUM <dbl></span></span> |
| 1813 |
<span id="cb49-47"><a href="#cb49-47" tabindex="-1"></a><span class="co"># </span></span> |
| 1814 |
<span id="cb49-48"><a href="#cb49-48" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1815 |
<p><code>fdiff</code> and <code>fgrowth</code> can also perform leaded |
| 1816 |
(forward) differences and growth rates |
| 1817 |
(i.e. <code>... %>% fgrowth(-c(1, 10), 1:2, Year)</code> would |
| 1818 |
compute one and 10-year leaded first and second differences). Again it |
| 1819 |
is possible to perform sequential operations:</p> |
| 1820 |
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" tabindex="-1"></a><span class="co"># This computes the 1 and 10-year growth rates, for the current period and lagged by one period</span></span> |
| 1821 |
<span id="cb50-2"><a href="#cb50-2" tabindex="-1"></a>GGDC10S <span class="sc">%>%</span></span> |
| 1822 |
<span id="cb50-3"><a href="#cb50-3" tabindex="-1"></a> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode) <span class="sc">%>%</span> </span> |
| 1823 |
<span id="cb50-4"><a href="#cb50-4" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Variable, Country) <span class="sc">%>%</span> <span class="fu">fgrowth</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>), <span class="dv">1</span>, Year) <span class="sc">%>%</span> <span class="fu">flag</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">1</span>, Year)</span> |
| 1824 |
<span id="cb50-5"><a href="#cb50-5" tabindex="-1"></a><span class="co"># # A tibble: 5,027 × 47</span></span> |
| 1825 |
<span id="cb50-6"><a href="#cb50-6" tabindex="-1"></a><span class="co"># Country Variable Year G1.AGR L1.G1.AGR L10G1.AGR L1.L10G1.AGR G1.MIN L1.G1.MIN L10G1.MIN</span></span> |
| 1826 |
<span id="cb50-7"><a href="#cb50-7" tabindex="-1"></a><span class="co"># * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span> |
| 1827 |
<span id="cb50-8"><a href="#cb50-8" tabindex="-1"></a><span class="co"># 1 BWA VA 1960 NA NA NA NA NA NA NA</span></span> |
| 1828 |
<span id="cb50-9"><a href="#cb50-9" tabindex="-1"></a><span class="co"># 2 BWA VA 1961 NA NA NA NA NA NA NA</span></span> |
| 1829 |
<span id="cb50-10"><a href="#cb50-10" tabindex="-1"></a><span class="co"># 3 BWA VA 1962 NA NA NA NA NA NA NA</span></span> |
| 1830 |
<span id="cb50-11"><a href="#cb50-11" tabindex="-1"></a><span class="co"># 4 BWA VA 1963 NA NA NA NA NA NA NA</span></span> |
| 1831 |
<span id="cb50-12"><a href="#cb50-12" tabindex="-1"></a><span class="co"># 5 BWA VA 1964 NA NA NA NA NA NA NA</span></span> |
| 1832 |
<span id="cb50-13"><a href="#cb50-13" tabindex="-1"></a><span class="co"># 6 BWA VA 1965 -3.52 NA NA NA -28.6 NA NA</span></span> |
| 1833 |
<span id="cb50-14"><a href="#cb50-14" tabindex="-1"></a><span class="co"># 7 BWA VA 1966 12.4 -3.52 NA NA -21.1 -28.6 NA</span></span> |
| 1834 |
<span id="cb50-15"><a href="#cb50-15" tabindex="-1"></a><span class="co"># 8 BWA VA 1967 8.29 12.4 NA NA 16.7 -21.1 NA</span></span> |
| 1835 |
<span id="cb50-16"><a href="#cb50-16" tabindex="-1"></a><span class="co"># 9 BWA VA 1968 10.2 8.29 NA NA -20 16.7 NA</span></span> |
| 1836 |
<span id="cb50-17"><a href="#cb50-17" tabindex="-1"></a><span class="co"># 10 BWA VA 1969 3.61 10.2 NA NA 185. -20 NA</span></span> |
| 1837 |
<span id="cb50-18"><a href="#cb50-18" tabindex="-1"></a><span class="co"># # ℹ 5,017 more rows</span></span> |
| 1838 |
<span id="cb50-19"><a href="#cb50-19" tabindex="-1"></a><span class="co"># # ℹ 37 more variables: L1.L10G1.MIN <dbl>, G1.MAN <dbl>, L1.G1.MAN <dbl>, L10G1.MAN <dbl>,</span></span> |
| 1839 |
<span id="cb50-20"><a href="#cb50-20" tabindex="-1"></a><span class="co"># # L1.L10G1.MAN <dbl>, G1.PU <dbl>, L1.G1.PU <dbl>, L10G1.PU <dbl>, L1.L10G1.PU <dbl>,</span></span> |
| 1840 |
<span id="cb50-21"><a href="#cb50-21" tabindex="-1"></a><span class="co"># # G1.CON <dbl>, L1.G1.CON <dbl>, L10G1.CON <dbl>, L1.L10G1.CON <dbl>, G1.WRT <dbl>,</span></span> |
| 1841 |
<span id="cb50-22"><a href="#cb50-22" tabindex="-1"></a><span class="co"># # L1.G1.WRT <dbl>, L10G1.WRT <dbl>, L1.L10G1.WRT <dbl>, G1.TRA <dbl>, L1.G1.TRA <dbl>,</span></span> |
| 1842 |
<span id="cb50-23"><a href="#cb50-23" tabindex="-1"></a><span class="co"># # L10G1.TRA <dbl>, L1.L10G1.TRA <dbl>, G1.FIRE <dbl>, L1.G1.FIRE <dbl>, L10G1.FIRE <dbl>,</span></span> |
| 1843 |
<span id="cb50-24"><a href="#cb50-24" tabindex="-1"></a><span class="co"># # L1.L10G1.FIRE <dbl>, G1.GOV <dbl>, L1.G1.GOV <dbl>, L10G1.GOV <dbl>, L1.L10G1.GOV <dbl>, …</span></span> |
| 1844 |
<span id="cb50-25"><a href="#cb50-25" tabindex="-1"></a><span class="co"># </span></span> |
| 1845 |
<span id="cb50-26"><a href="#cb50-26" tabindex="-1"></a><span class="co"># Grouped by: Variable, Country [85 | 59 (7.7) 4-65]</span></span></code></pre></div> |
| 1846 |
</div> |
| 1847 |
</div> |
| 1848 |
<div id="benchmarks" class="section level2"> |
| 1849 |
<h2>3. Benchmarks</h2> |
| 1850 |
<p>This section seeks to demonstrate that the functionality introduced |
| 1851 |
in the preceding 2 sections indeed produces code that evaluates |
| 1852 |
substantially faster than native <em>dplyr</em>.</p> |
| 1853 |
<p>To do this properly, the different components of a typical piped call |
| 1854 |
(selecting / subsetting, ordering, grouping, and performing some |
| 1855 |
computation) are benchmarked separately on 2 different data sizes.</p> |
| 1856 |
<p>All benchmarks are run on a Windows 8.1 laptop with a 2x 2.2 GHZ |
| 1857 |
Intel i5 processor, 8GB DDR3 RAM and a Samsung 850 EVO SSD hard |
| 1858 |
drive.</p> |
| 1859 |
<div id="data" class="section level3"> |
| 1860 |
<h3>3.1 Data</h3> |
| 1861 |
<p>Benchmarks are run on the original <code>GGDC10S</code> data used |
| 1862 |
throughout this vignette and a larger dataset with approx. 1 million |
| 1863 |
observations, obtained by replicating and row-binding |
| 1864 |
<code>GGDC10S</code> 200 times while maintaining unique groups.</p> |
| 1865 |
<div class="sourceCode" id="cb51"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" tabindex="-1"></a><span class="co"># This shows the groups in GGDC10S</span></span> |
| 1866 |
<span id="cb51-2"><a href="#cb51-2" tabindex="-1"></a><span class="fu">GRP</span>(GGDC10S, <span class="sc">~</span> Variable <span class="sc">+</span> Country)</span> |
| 1867 |
<span id="cb51-3"><a href="#cb51-3" tabindex="-1"></a><span class="co"># collapse grouping object of length 5027 with 85 ordered groups</span></span> |
| 1868 |
<span id="cb51-4"><a href="#cb51-4" tabindex="-1"></a><span class="co"># </span></span> |
| 1869 |
<span id="cb51-5"><a href="#cb51-5" tabindex="-1"></a><span class="co"># Call: GRP.default(X = GGDC10S, by = ~Variable + Country), X is unsorted</span></span> |
| 1870 |
<span id="cb51-6"><a href="#cb51-6" tabindex="-1"></a><span class="co"># </span></span> |
| 1871 |
<span id="cb51-7"><a href="#cb51-7" tabindex="-1"></a><span class="co"># Distribution of group sizes: </span></span> |
| 1872 |
<span id="cb51-8"><a href="#cb51-8" tabindex="-1"></a><span class="co"># Min. 1st Qu. Median Mean 3rd Qu. Max. </span></span> |
| 1873 |
<span id="cb51-9"><a href="#cb51-9" tabindex="-1"></a><span class="co"># 4.00 53.00 62.00 59.14 63.00 65.00 </span></span> |
| 1874 |
<span id="cb51-10"><a href="#cb51-10" tabindex="-1"></a><span class="co"># </span></span> |
| 1875 |
<span id="cb51-11"><a href="#cb51-11" tabindex="-1"></a><span class="co"># Groups with sizes: </span></span> |
| 1876 |
<span id="cb51-12"><a href="#cb51-12" tabindex="-1"></a><span class="co"># EMP.ARG EMP.BOL EMP.BRA EMP.BWA EMP.CHL EMP.CHN </span></span> |
| 1877 |
<span id="cb51-13"><a href="#cb51-13" tabindex="-1"></a><span class="co"># 62 61 62 52 63 62 </span></span> |
| 1878 |
<span id="cb51-14"><a href="#cb51-14" tabindex="-1"></a><span class="co"># ---</span></span> |
| 1879 |
<span id="cb51-15"><a href="#cb51-15" tabindex="-1"></a><span class="co"># VA.TWN VA.TZA VA.USA VA.VEN VA.ZAF VA.ZMB </span></span> |
| 1880 |
<span id="cb51-16"><a href="#cb51-16" tabindex="-1"></a><span class="co"># 63 52 65 63 52 52</span></span> |
| 1881 |
<span id="cb51-17"><a href="#cb51-17" tabindex="-1"></a></span> |
| 1882 |
<span id="cb51-18"><a href="#cb51-18" tabindex="-1"></a><span class="co"># This replicates the data 200 times </span></span> |
| 1883 |
<span id="cb51-19"><a href="#cb51-19" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">replicate</span>(<span class="dv">200</span>, GGDC10S, <span class="at">simplify =</span> <span class="cn">FALSE</span>) </span> |
| 1884 |
<span id="cb51-20"><a href="#cb51-20" tabindex="-1"></a><span class="co"># This function adds a number i to the country and variable columns of each dataset</span></span> |
| 1885 |
<span id="cb51-21"><a href="#cb51-21" tabindex="-1"></a>uniquify <span class="ot"><-</span> <span class="cf">function</span>(x, i) <span class="fu">ftransform</span>(x, <span class="fu">lapply</span>(<span class="fu">unclass</span>(x)[<span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">4</span>)], paste0, i))</span> |
| 1886 |
<span id="cb51-22"><a href="#cb51-22" tabindex="-1"></a><span class="co"># Making datasets unique and row-binding them</span></span> |
| 1887 |
<span id="cb51-23"><a href="#cb51-23" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">unlist2d</span>(<span class="fu">Map</span>(uniquify, data, <span class="fu">as.list</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">200</span>)), <span class="at">idcols =</span> <span class="cn">FALSE</span>)</span> |
| 1888 |
<span id="cb51-24"><a href="#cb51-24" tabindex="-1"></a><span class="fu">fdim</span>(data)</span> |
| 1889 |
<span id="cb51-25"><a href="#cb51-25" tabindex="-1"></a><span class="co"># [1] 1005400 16</span></span> |
| 1890 |
<span id="cb51-26"><a href="#cb51-26" tabindex="-1"></a></span> |
| 1891 |
<span id="cb51-27"><a href="#cb51-27" tabindex="-1"></a><span class="co"># This shows the groups in the replicated data</span></span> |
| 1892 |
<span id="cb51-28"><a href="#cb51-28" tabindex="-1"></a><span class="fu">GRP</span>(data, <span class="sc">~</span> Variable <span class="sc">+</span> Country)</span> |
| 1893 |
<span id="cb51-29"><a href="#cb51-29" tabindex="-1"></a><span class="co"># collapse grouping object of length 1005400 with 17000 ordered groups</span></span> |
| 1894 |
<span id="cb51-30"><a href="#cb51-30" tabindex="-1"></a><span class="co"># </span></span> |
| 1895 |
<span id="cb51-31"><a href="#cb51-31" tabindex="-1"></a><span class="co"># Call: GRP.default(X = data, by = ~Variable + Country), X is unsorted</span></span> |
| 1896 |
<span id="cb51-32"><a href="#cb51-32" tabindex="-1"></a><span class="co"># </span></span> |
| 1897 |
<span id="cb51-33"><a href="#cb51-33" tabindex="-1"></a><span class="co"># Distribution of group sizes: </span></span> |
| 1898 |
<span id="cb51-34"><a href="#cb51-34" tabindex="-1"></a><span class="co"># Min. 1st Qu. Median Mean 3rd Qu. Max. </span></span> |
| 1899 |
<span id="cb51-35"><a href="#cb51-35" tabindex="-1"></a><span class="co"># 4.00 53.00 62.00 59.14 63.00 65.00 </span></span> |
| 1900 |
<span id="cb51-36"><a href="#cb51-36" tabindex="-1"></a><span class="co"># </span></span> |
| 1901 |
<span id="cb51-37"><a href="#cb51-37" tabindex="-1"></a><span class="co"># Groups with sizes: </span></span> |
| 1902 |
<span id="cb51-38"><a href="#cb51-38" tabindex="-1"></a><span class="co"># EMP1.ARG1 EMP1.BOL1 EMP1.BRA1 EMP1.BWA1 EMP1.CHL1 EMP1.CHN1 </span></span> |
| 1903 |
<span id="cb51-39"><a href="#cb51-39" tabindex="-1"></a><span class="co"># 62 61 62 52 63 62 </span></span> |
| 1904 |
<span id="cb51-40"><a href="#cb51-40" tabindex="-1"></a><span class="co"># ---</span></span> |
| 1905 |
<span id="cb51-41"><a href="#cb51-41" tabindex="-1"></a><span class="co"># VA99.TWN99 VA99.TZA99 VA99.USA99 VA99.VEN99 VA99.ZAF99 VA99.ZMB99 </span></span> |
| 1906 |
<span id="cb51-42"><a href="#cb51-42" tabindex="-1"></a><span class="co"># 63 52 65 63 52 52</span></span> |
| 1907 |
<span id="cb51-43"><a href="#cb51-43" tabindex="-1"></a></span> |
| 1908 |
<span id="cb51-44"><a href="#cb51-44" tabindex="-1"></a><span class="fu">gc</span>()</span> |
| 1909 |
<span id="cb51-45"><a href="#cb51-45" tabindex="-1"></a><span class="co"># used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)</span></span> |
| 1910 |
<span id="cb51-46"><a href="#cb51-46" tabindex="-1"></a><span class="co"># Ncells 3184710 170.1 8862174 473.3 NA 8862174 473.3</span></span> |
| 1911 |
<span id="cb51-47"><a href="#cb51-47" tabindex="-1"></a><span class="co"># Vcells 23965820 182.9 147787078 1127.6 16384 445825141 3401.4</span></span></code></pre></div> |
| 1912 |
</div> |
| 1913 |
<div id="selecting-subsetting-ordering-and-grouping" class="section level3"> |
| 1914 |
<h3>3.1 Selecting, Subsetting, Ordering and Grouping</h3> |
| 1915 |
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" tabindex="-1"></a><span class="do">## Selecting columns</span></span> |
| 1916 |
<span id="cb52-2"><a href="#cb52-2" tabindex="-1"></a><span class="co"># Small</span></span> |
| 1917 |
<span id="cb52-3"><a href="#cb52-3" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">select</span>(GGDC10S, Country, Variable, AGR<span class="sc">:</span>SUM),</span> |
| 1918 |
<span id="cb52-4"><a href="#cb52-4" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fselect</span>(GGDC10S, Country, Variable, AGR<span class="sc">:</span>SUM))</span> |
| 1919 |
<span id="cb52-5"><a href="#cb52-5" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 1920 |
<span id="cb52-6"><a href="#cb52-6" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1921 |
<span id="cb52-7"><a href="#cb52-7" tabindex="-1"></a><span class="co"># dplyr 400.775 410.7585 425.43117 416.396 424.637 820.041 100</span></span> |
| 1922 |
<span id="cb52-8"><a href="#cb52-8" tabindex="-1"></a><span class="co"># collapse 2.911 3.4645 4.59856 4.469 5.412 15.293 100</span></span> |
| 1923 |
<span id="cb52-9"><a href="#cb52-9" tabindex="-1"></a></span> |
| 1924 |
<span id="cb52-10"><a href="#cb52-10" tabindex="-1"></a><span class="co"># Large</span></span> |
| 1925 |
<span id="cb52-11"><a href="#cb52-11" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">select</span>(data, Country, Variable, AGR<span class="sc">:</span>SUM),</span> |
| 1926 |
<span id="cb52-12"><a href="#cb52-12" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fselect</span>(data, Country, Variable, AGR<span class="sc">:</span>SUM))</span> |
| 1927 |
<span id="cb52-13"><a href="#cb52-13" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 1928 |
<span id="cb52-14"><a href="#cb52-14" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1929 |
<span id="cb52-15"><a href="#cb52-15" tabindex="-1"></a><span class="co"># dplyr 388.926 396.429 412.67730 402.9890 411.0455 728.734 100</span></span> |
| 1930 |
<span id="cb52-16"><a href="#cb52-16" tabindex="-1"></a><span class="co"># collapse 2.870 3.280 4.44686 3.8335 5.3300 12.669 100</span></span> |
| 1931 |
<span id="cb52-17"><a href="#cb52-17" tabindex="-1"></a></span> |
| 1932 |
<span id="cb52-18"><a href="#cb52-18" tabindex="-1"></a><span class="do">## Subsetting columns </span></span> |
| 1933 |
<span id="cb52-19"><a href="#cb52-19" tabindex="-1"></a><span class="co"># Small</span></span> |
| 1934 |
<span id="cb52-20"><a href="#cb52-20" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">filter</span>(GGDC10S, Variable <span class="sc">==</span> <span class="st">"VA"</span>),</span> |
| 1935 |
<span id="cb52-21"><a href="#cb52-21" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsubset</span>(GGDC10S, Variable <span class="sc">==</span> <span class="st">"VA"</span>))</span> |
| 1936 |
<span id="cb52-22"><a href="#cb52-22" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 1937 |
<span id="cb52-23"><a href="#cb52-23" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1938 |
<span id="cb52-24"><a href="#cb52-24" tabindex="-1"></a><span class="co"># dplyr 374.084 394.4405 409.23986 401.0005 414.3050 716.475 100</span></span> |
| 1939 |
<span id="cb52-25"><a href="#cb52-25" tabindex="-1"></a><span class="co"># collapse 39.278 48.2775 55.85307 55.5550 60.4545 103.320 100</span></span> |
| 1940 |
<span id="cb52-26"><a href="#cb52-26" tabindex="-1"></a></span> |
| 1941 |
<span id="cb52-27"><a href="#cb52-27" tabindex="-1"></a><span class="co"># Large</span></span> |
| 1942 |
<span id="cb52-28"><a href="#cb52-28" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">filter</span>(data, Variable <span class="sc">==</span> <span class="st">"VA"</span>),</span> |
| 1943 |
<span id="cb52-29"><a href="#cb52-29" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsubset</span>(data, Variable <span class="sc">==</span> <span class="st">"VA"</span>))</span> |
| 1944 |
<span id="cb52-30"><a href="#cb52-30" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 1945 |
<span id="cb52-31"><a href="#cb52-31" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1946 |
<span id="cb52-32"><a href="#cb52-32" tabindex="-1"></a><span class="co"># dplyr 4.487409 5.242752 8.352270 5.653223 6.434048 159.13658 100</span></span> |
| 1947 |
<span id="cb52-33"><a href="#cb52-33" tabindex="-1"></a><span class="co"># collapse 2.840808 3.082359 3.469128 3.163478 3.302714 16.56047 100</span></span> |
| 1948 |
<span id="cb52-34"><a href="#cb52-34" tabindex="-1"></a></span> |
| 1949 |
<span id="cb52-35"><a href="#cb52-35" tabindex="-1"></a><span class="do">## Ordering rows</span></span> |
| 1950 |
<span id="cb52-36"><a href="#cb52-36" tabindex="-1"></a><span class="co"># Small</span></span> |
| 1951 |
<span id="cb52-37"><a href="#cb52-37" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">arrange</span>(GGDC10S, <span class="fu">desc</span>(Country), Variable, Year),</span> |
| 1952 |
<span id="cb52-38"><a href="#cb52-38" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">roworder</span>(GGDC10S, <span class="sc">-</span>Country, Variable, Year))</span> |
| 1953 |
<span id="cb52-39"><a href="#cb52-39" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 1954 |
<span id="cb52-40"><a href="#cb52-40" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1955 |
<span id="cb52-41"><a href="#cb52-41" tabindex="-1"></a><span class="co"># dplyr 1715.112 1867.4270 1983.4726 2015.109 2080.7500 2367.791 100</span></span> |
| 1956 |
<span id="cb52-42"><a href="#cb52-42" tabindex="-1"></a><span class="co"># collapse 192.495 232.4085 256.3878 247.968 258.7715 1055.381 100</span></span> |
| 1957 |
<span id="cb52-43"><a href="#cb52-43" tabindex="-1"></a></span> |
| 1958 |
<span id="cb52-44"><a href="#cb52-44" tabindex="-1"></a><span class="co"># Large</span></span> |
| 1959 |
<span id="cb52-45"><a href="#cb52-45" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">arrange</span>(data, <span class="fu">desc</span>(Country), Variable, Year),</span> |
| 1960 |
<span id="cb52-46"><a href="#cb52-46" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">roworder</span>(data, <span class="sc">-</span>Country, Variable, Year), <span class="at">times =</span> <span class="dv">2</span>)</span> |
| 1961 |
<span id="cb52-47"><a href="#cb52-47" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 1962 |
<span id="cb52-48"><a href="#cb52-48" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1963 |
<span id="cb52-49"><a href="#cb52-49" tabindex="-1"></a><span class="co"># dplyr 89.37512 89.37512 101.05180 101.05180 112.72848 112.72848 2</span></span> |
| 1964 |
<span id="cb52-50"><a href="#cb52-50" tabindex="-1"></a><span class="co"># collapse 66.46703 66.46703 67.45254 67.45254 68.43806 68.43806 2</span></span> |
| 1965 |
<span id="cb52-51"><a href="#cb52-51" tabindex="-1"></a></span> |
| 1966 |
<span id="cb52-52"><a href="#cb52-52" tabindex="-1"></a></span> |
| 1967 |
<span id="cb52-53"><a href="#cb52-53" tabindex="-1"></a><span class="do">## Grouping </span></span> |
| 1968 |
<span id="cb52-54"><a href="#cb52-54" tabindex="-1"></a><span class="co"># Small</span></span> |
| 1969 |
<span id="cb52-55"><a href="#cb52-55" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">group_by</span>(GGDC10S, Country, Variable),</span> |
| 1970 |
<span id="cb52-56"><a href="#cb52-56" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fgroup_by</span>(GGDC10S, Country, Variable))</span> |
| 1971 |
<span id="cb52-57"><a href="#cb52-57" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 1972 |
<span id="cb52-58"><a href="#cb52-58" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1973 |
<span id="cb52-59"><a href="#cb52-59" tabindex="-1"></a><span class="co"># dplyr 778.713 815.1825 911.3484 874.2225 960.3840 1529.874 100</span></span> |
| 1974 |
<span id="cb52-60"><a href="#cb52-60" tabindex="-1"></a><span class="co"># collapse 146.534 157.6245 198.5921 165.0660 177.3455 1484.241 100</span></span> |
| 1975 |
<span id="cb52-61"><a href="#cb52-61" tabindex="-1"></a></span> |
| 1976 |
<span id="cb52-62"><a href="#cb52-62" tabindex="-1"></a><span class="co"># Large</span></span> |
| 1977 |
<span id="cb52-63"><a href="#cb52-63" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">group_by</span>(data, Country, Variable),</span> |
| 1978 |
<span id="cb52-64"><a href="#cb52-64" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fgroup_by</span>(data, Country, Variable), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 1979 |
<span id="cb52-65"><a href="#cb52-65" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 1980 |
<span id="cb52-66"><a href="#cb52-66" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1981 |
<span id="cb52-67"><a href="#cb52-67" tabindex="-1"></a><span class="co"># dplyr 34.20294 34.62839 34.88041 34.88432 35.07821 35.48279 10</span></span> |
| 1982 |
<span id="cb52-68"><a href="#cb52-68" tabindex="-1"></a><span class="co"># collapse 27.89972 28.03211 28.55175 28.36954 29.32283 29.54206 10</span></span> |
| 1983 |
<span id="cb52-69"><a href="#cb52-69" tabindex="-1"></a></span> |
| 1984 |
<span id="cb52-70"><a href="#cb52-70" tabindex="-1"></a><span class="do">## Computing a new column </span></span> |
| 1985 |
<span id="cb52-71"><a href="#cb52-71" tabindex="-1"></a><span class="co"># Small</span></span> |
| 1986 |
<span id="cb52-72"><a href="#cb52-72" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate</span>(GGDC10S, <span class="at">NEW =</span> AGR<span class="sc">+</span><span class="dv">1</span>),</span> |
| 1987 |
<span id="cb52-73"><a href="#cb52-73" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">ftransform</span>(GGDC10S, <span class="at">NEW =</span> AGR<span class="sc">+</span><span class="dv">1</span>))</span> |
| 1988 |
<span id="cb52-74"><a href="#cb52-74" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 1989 |
<span id="cb52-75"><a href="#cb52-75" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1990 |
<span id="cb52-76"><a href="#cb52-76" tabindex="-1"></a><span class="co"># dplyr 317.463 321.7270 333.38822 324.9660 333.7810 631.564 100</span></span> |
| 1991 |
<span id="cb52-77"><a href="#cb52-77" tabindex="-1"></a><span class="co"># collapse 8.897 11.0495 12.95354 12.4435 14.2065 38.991 100</span></span> |
| 1992 |
<span id="cb52-78"><a href="#cb52-78" tabindex="-1"></a></span> |
| 1993 |
<span id="cb52-79"><a href="#cb52-79" tabindex="-1"></a><span class="co"># Large</span></span> |
| 1994 |
<span id="cb52-80"><a href="#cb52-80" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate</span>(data, <span class="at">NEW =</span> AGR<span class="sc">+</span><span class="dv">1</span>),</span> |
| 1995 |
<span id="cb52-81"><a href="#cb52-81" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">ftransform</span>(data, <span class="at">NEW =</span> AGR<span class="sc">+</span><span class="dv">1</span>))</span> |
| 1996 |
<span id="cb52-82"><a href="#cb52-82" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 1997 |
<span id="cb52-83"><a href="#cb52-83" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 1998 |
<span id="cb52-84"><a href="#cb52-84" tabindex="-1"></a><span class="co"># dplyr 637.878 1084.225 1330.006 1164.6665 1291.2335 15869.05 100</span></span> |
| 1999 |
<span id="cb52-85"><a href="#cb52-85" tabindex="-1"></a><span class="co"># collapse 210.740 657.025 1021.434 698.3735 781.7675 16725.09 100</span></span> |
| 2000 |
<span id="cb52-86"><a href="#cb52-86" tabindex="-1"></a></span> |
| 2001 |
<span id="cb52-87"><a href="#cb52-87" tabindex="-1"></a><span class="do">## All combined with pipes </span></span> |
| 2002 |
<span id="cb52-88"><a href="#cb52-88" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2003 |
<span id="cb52-89"><a href="#cb52-89" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">filter</span>(GGDC10S, Variable <span class="sc">==</span> <span class="st">"VA"</span>) <span class="sc">%>%</span> </span> |
| 2004 |
<span id="cb52-90"><a href="#cb52-90" tabindex="-1"></a> <span class="fu">select</span>(Country, Year, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 2005 |
<span id="cb52-91"><a href="#cb52-91" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(Country), Year) <span class="sc">%>%</span></span> |
| 2006 |
<span id="cb52-92"><a href="#cb52-92" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">NEW =</span> AGR<span class="sc">+</span><span class="dv">1</span>) <span class="sc">%>%</span></span> |
| 2007 |
<span id="cb52-93"><a href="#cb52-93" tabindex="-1"></a> <span class="fu">group_by</span>(Country),</span> |
| 2008 |
<span id="cb52-94"><a href="#cb52-94" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsubset</span>(GGDC10S, Variable <span class="sc">==</span> <span class="st">"VA"</span>, Country, Year, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 2009 |
<span id="cb52-95"><a href="#cb52-95" tabindex="-1"></a> <span class="fu">roworder</span>(<span class="sc">-</span>Country, Year) <span class="sc">%>%</span></span> |
| 2010 |
<span id="cb52-96"><a href="#cb52-96" tabindex="-1"></a> <span class="fu">ftransform</span>(<span class="at">NEW =</span> AGR<span class="sc">+</span><span class="dv">1</span>) <span class="sc">%>%</span></span> |
| 2011 |
<span id="cb52-97"><a href="#cb52-97" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Country))</span> |
| 2012 |
<span id="cb52-98"><a href="#cb52-98" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2013 |
<span id="cb52-99"><a href="#cb52-99" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2014 |
<span id="cb52-100"><a href="#cb52-100" tabindex="-1"></a><span class="co"># dplyr 2982.340 3416.325 3525.7983 3538.464 3668.516 5034.021 100</span></span> |
| 2015 |
<span id="cb52-101"><a href="#cb52-101" tabindex="-1"></a><span class="co"># collapse 136.858 186.632 214.4681 211.683 243.130 314.470 100</span></span> |
| 2016 |
<span id="cb52-102"><a href="#cb52-102" tabindex="-1"></a></span> |
| 2017 |
<span id="cb52-103"><a href="#cb52-103" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2018 |
<span id="cb52-104"><a href="#cb52-104" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">filter</span>(data, Variable <span class="sc">==</span> <span class="st">"VA"</span>) <span class="sc">%>%</span> </span> |
| 2019 |
<span id="cb52-105"><a href="#cb52-105" tabindex="-1"></a> <span class="fu">select</span>(Country, Year, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 2020 |
<span id="cb52-106"><a href="#cb52-106" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(Country), Year) <span class="sc">%>%</span></span> |
| 2021 |
<span id="cb52-107"><a href="#cb52-107" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">NEW =</span> AGR<span class="sc">+</span><span class="dv">1</span>) <span class="sc">%>%</span></span> |
| 2022 |
<span id="cb52-108"><a href="#cb52-108" tabindex="-1"></a> <span class="fu">group_by</span>(Country),</span> |
| 2023 |
<span id="cb52-109"><a href="#cb52-109" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsubset</span>(data, Variable <span class="sc">==</span> <span class="st">"VA"</span>, Country, Year, AGR<span class="sc">:</span>SUM) <span class="sc">%>%</span> </span> |
| 2024 |
<span id="cb52-110"><a href="#cb52-110" tabindex="-1"></a> <span class="fu">roworder</span>(<span class="sc">-</span>Country, Year) <span class="sc">%>%</span></span> |
| 2025 |
<span id="cb52-111"><a href="#cb52-111" tabindex="-1"></a> <span class="fu">ftransform</span>(<span class="at">NEW =</span> AGR<span class="sc">+</span><span class="dv">1</span>) <span class="sc">%>%</span></span> |
| 2026 |
<span id="cb52-112"><a href="#cb52-112" tabindex="-1"></a> <span class="fu">fgroup_by</span>(Country), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2027 |
<span id="cb52-113"><a href="#cb52-113" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2028 |
<span id="cb52-114"><a href="#cb52-114" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2029 |
<span id="cb52-115"><a href="#cb52-115" tabindex="-1"></a><span class="co"># dplyr 7.917182 7.997378 8.142653 8.109943 8.292291 8.423163 10</span></span> |
| 2030 |
<span id="cb52-116"><a href="#cb52-116" tabindex="-1"></a><span class="co"># collapse 3.080289 3.104028 3.150153 3.140969 3.188365 3.251259 10</span></span> |
| 2031 |
<span id="cb52-117"><a href="#cb52-117" tabindex="-1"></a></span> |
| 2032 |
<span id="cb52-118"><a href="#cb52-118" tabindex="-1"></a><span class="fu">gc</span>()</span> |
| 2033 |
<span id="cb52-119"><a href="#cb52-119" tabindex="-1"></a><span class="co"># used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)</span></span> |
| 2034 |
<span id="cb52-120"><a href="#cb52-120" tabindex="-1"></a><span class="co"># Ncells 3184728 170.1 8862174 473.3 NA 8862174 473.3</span></span> |
| 2035 |
<span id="cb52-121"><a href="#cb52-121" tabindex="-1"></a><span class="co"># Vcells 23970594 182.9 75772825 578.2 16384 445825141 3401.4</span></span></code></pre></div> |
| 2036 |
</div> |
| 2037 |
<div id="aggregation" class="section level3"> |
| 2038 |
<h3>3.1 Aggregation</h3> |
| 2039 |
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" tabindex="-1"></a><span class="do">## Grouping the data</span></span> |
| 2040 |
<span id="cb53-2"><a href="#cb53-2" tabindex="-1"></a>cgGGDC10S <span class="ot"><-</span> <span class="fu">fgroup_by</span>(GGDC10S, Variable, Country) <span class="sc">%>%</span> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode)</span> |
| 2041 |
<span id="cb53-3"><a href="#cb53-3" tabindex="-1"></a>gGGDC10S <span class="ot"><-</span> <span class="fu">group_by</span>(GGDC10S, Variable, Country) <span class="sc">%>%</span> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode)</span> |
| 2042 |
<span id="cb53-4"><a href="#cb53-4" tabindex="-1"></a>cgdata <span class="ot"><-</span> <span class="fu">fgroup_by</span>(data, Variable, Country) <span class="sc">%>%</span> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode)</span> |
| 2043 |
<span id="cb53-5"><a href="#cb53-5" tabindex="-1"></a>gdata <span class="ot"><-</span> <span class="fu">group_by</span>(data, Variable, Country) <span class="sc">%>%</span> <span class="fu">fselect</span>(<span class="sc">-</span>Region, <span class="sc">-</span>Regioncode)</span> |
| 2044 |
<span id="cb53-6"><a href="#cb53-6" tabindex="-1"></a><span class="fu">rm</span>(data, GGDC10S) </span> |
| 2045 |
<span id="cb53-7"><a href="#cb53-7" tabindex="-1"></a><span class="fu">gc</span>()</span> |
| 2046 |
<span id="cb53-8"><a href="#cb53-8" tabindex="-1"></a><span class="co"># used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)</span></span> |
| 2047 |
<span id="cb53-9"><a href="#cb53-9" tabindex="-1"></a><span class="co"># Ncells 3201723 171 8862174 473.3 NA 8862174 473.3</span></span> |
| 2048 |
<span id="cb53-10"><a href="#cb53-10" tabindex="-1"></a><span class="co"># Vcells 23589381 180 75772825 578.2 16384 445825141 3401.4</span></span> |
| 2049 |
<span id="cb53-11"><a href="#cb53-11" tabindex="-1"></a></span> |
| 2050 |
<span id="cb53-12"><a href="#cb53-12" tabindex="-1"></a><span class="do">## Conversion of Grouping object: This time would be required extra in all hybrid calls </span></span> |
| 2051 |
<span id="cb53-13"><a href="#cb53-13" tabindex="-1"></a><span class="do">## i.e. when calling collapse functions on data grouped with dplyr::group_by</span></span> |
| 2052 |
<span id="cb53-14"><a href="#cb53-14" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2053 |
<span id="cb53-15"><a href="#cb53-15" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">GRP</span>(gGGDC10S))</span> |
| 2054 |
<span id="cb53-16"><a href="#cb53-16" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2055 |
<span id="cb53-17"><a href="#cb53-17" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2056 |
<span id="cb53-18"><a href="#cb53-18" tabindex="-1"></a><span class="co"># GRP(gGGDC10S) 8.692 9.2455 10.16021 9.4915 10.086 39.196 100</span></span> |
| 2057 |
<span id="cb53-19"><a href="#cb53-19" tabindex="-1"></a></span> |
| 2058 |
<span id="cb53-20"><a href="#cb53-20" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2059 |
<span id="cb53-21"><a href="#cb53-21" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">GRP</span>(gdata))</span> |
| 2060 |
<span id="cb53-22"><a href="#cb53-22" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2061 |
<span id="cb53-23"><a href="#cb53-23" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2062 |
<span id="cb53-24"><a href="#cb53-24" tabindex="-1"></a><span class="co"># GRP(gdata) 885.641 1160.915 1248.258 1237.236 1323.234 1651.398 100</span></span> |
| 2063 |
<span id="cb53-25"><a href="#cb53-25" tabindex="-1"></a></span> |
| 2064 |
<span id="cb53-26"><a href="#cb53-26" tabindex="-1"></a></span> |
| 2065 |
<span id="cb53-27"><a href="#cb53-27" tabindex="-1"></a><span class="do">## Sum </span></span> |
| 2066 |
<span id="cb53-28"><a href="#cb53-28" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2067 |
<span id="cb53-29"><a href="#cb53-29" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gGGDC10S, sum, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2068 |
<span id="cb53-30"><a href="#cb53-30" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsum</span>(cgGGDC10S))</span> |
| 2069 |
<span id="cb53-31"><a href="#cb53-31" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2070 |
<span id="cb53-32"><a href="#cb53-32" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2071 |
<span id="cb53-33"><a href="#cb53-33" tabindex="-1"></a><span class="co"># dplyr 3017.723 3354.1895 3733.4739 3620.9560 3738.441 22135.736 100</span></span> |
| 2072 |
<span id="cb53-34"><a href="#cb53-34" tabindex="-1"></a><span class="co"># collapse 218.120 227.3655 236.7693 235.1965 244.852 270.805 100</span></span> |
| 2073 |
<span id="cb53-35"><a href="#cb53-35" tabindex="-1"></a></span> |
| 2074 |
<span id="cb53-36"><a href="#cb53-36" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2075 |
<span id="cb53-37"><a href="#cb53-37" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gdata, sum, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2076 |
<span id="cb53-38"><a href="#cb53-38" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsum</span>(cgdata), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2077 |
<span id="cb53-39"><a href="#cb53-39" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2078 |
<span id="cb53-40"><a href="#cb53-40" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2079 |
<span id="cb53-41"><a href="#cb53-41" tabindex="-1"></a><span class="co"># dplyr 272.9737 279.91024 305.02067 283.59737 303.57122 448.07629 10</span></span> |
| 2080 |
<span id="cb53-42"><a href="#cb53-42" tabindex="-1"></a><span class="co"># collapse 41.5330 41.63214 41.88717 41.77062 41.96059 42.78662 10</span></span> |
| 2081 |
<span id="cb53-43"><a href="#cb53-43" tabindex="-1"></a></span> |
| 2082 |
<span id="cb53-44"><a href="#cb53-44" tabindex="-1"></a><span class="do">## Mean</span></span> |
| 2083 |
<span id="cb53-45"><a href="#cb53-45" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2084 |
<span id="cb53-46"><a href="#cb53-46" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gGGDC10S, mean.default, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2085 |
<span id="cb53-47"><a href="#cb53-47" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fmean</span>(cgGGDC10S))</span> |
| 2086 |
<span id="cb53-48"><a href="#cb53-48" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2087 |
<span id="cb53-49"><a href="#cb53-49" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2088 |
<span id="cb53-50"><a href="#cb53-50" tabindex="-1"></a><span class="co"># dplyr 4360.104 4596.6740 5125.4194 4754.791 5005.710 37144.852 100</span></span> |
| 2089 |
<span id="cb53-51"><a href="#cb53-51" tabindex="-1"></a><span class="co"># collapse 169.084 174.3935 185.4594 183.434 194.832 221.933 100</span></span> |
| 2090 |
<span id="cb53-52"><a href="#cb53-52" tabindex="-1"></a></span> |
| 2091 |
<span id="cb53-53"><a href="#cb53-53" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2092 |
<span id="cb53-54"><a href="#cb53-54" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gdata, mean.default, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2093 |
<span id="cb53-55"><a href="#cb53-55" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fmean</span>(cgdata), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2094 |
<span id="cb53-56"><a href="#cb53-56" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2095 |
<span id="cb53-57"><a href="#cb53-57" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2096 |
<span id="cb53-58"><a href="#cb53-58" tabindex="-1"></a><span class="co"># dplyr 623.5123 642.83748 704.39836 681.32260 786.82731 829.74435 10</span></span> |
| 2097 |
<span id="cb53-59"><a href="#cb53-59" tabindex="-1"></a><span class="co"># collapse 31.7636 31.88037 32.00222 31.99445 32.08209 32.43875 10</span></span> |
| 2098 |
<span id="cb53-60"><a href="#cb53-60" tabindex="-1"></a></span> |
| 2099 |
<span id="cb53-61"><a href="#cb53-61" tabindex="-1"></a><span class="do">## Median</span></span> |
| 2100 |
<span id="cb53-62"><a href="#cb53-62" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2101 |
<span id="cb53-63"><a href="#cb53-63" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gGGDC10S, median, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2102 |
<span id="cb53-64"><a href="#cb53-64" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fmedian</span>(cgGGDC10S))</span> |
| 2103 |
<span id="cb53-65"><a href="#cb53-65" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2104 |
<span id="cb53-66"><a href="#cb53-66" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2105 |
<span id="cb53-67"><a href="#cb53-67" tabindex="-1"></a><span class="co"># dplyr 14399.118 14849.933 16170.3500 14982.5685 15145.892 33613.235 100</span></span> |
| 2106 |
<span id="cb53-68"><a href="#cb53-68" tabindex="-1"></a><span class="co"># collapse 137.596 164.902 189.2056 178.1245 214.676 248.624 100</span></span> |
| 2107 |
<span id="cb53-69"><a href="#cb53-69" tabindex="-1"></a></span> |
| 2108 |
<span id="cb53-70"><a href="#cb53-70" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2109 |
<span id="cb53-71"><a href="#cb53-71" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gdata, median, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2110 |
<span id="cb53-72"><a href="#cb53-72" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fmedian</span>(cgdata), <span class="at">times =</span> <span class="dv">2</span>)</span> |
| 2111 |
<span id="cb53-73"><a href="#cb53-73" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2112 |
<span id="cb53-74"><a href="#cb53-74" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2113 |
<span id="cb53-75"><a href="#cb53-75" tabindex="-1"></a><span class="co"># dplyr 2826.83036 2826.83036 2828.12912 2828.12912 2829.42788 2829.42788 2</span></span> |
| 2114 |
<span id="cb53-76"><a href="#cb53-76" tabindex="-1"></a><span class="co"># collapse 19.95564 19.95564 19.98524 19.98524 20.01485 20.01485 2</span></span> |
| 2115 |
<span id="cb53-77"><a href="#cb53-77" tabindex="-1"></a></span> |
| 2116 |
<span id="cb53-78"><a href="#cb53-78" tabindex="-1"></a><span class="do">## Standard Deviation</span></span> |
| 2117 |
<span id="cb53-79"><a href="#cb53-79" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2118 |
<span id="cb53-80"><a href="#cb53-80" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gGGDC10S, sd, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2119 |
<span id="cb53-81"><a href="#cb53-81" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsd</span>(cgGGDC10S))</span> |
| 2120 |
<span id="cb53-82"><a href="#cb53-82" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2121 |
<span id="cb53-83"><a href="#cb53-83" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2122 |
<span id="cb53-84"><a href="#cb53-84" tabindex="-1"></a><span class="co"># dplyr 8332.635 8612.5215 9365.1216 8712.766 8989.086 25087.982 100</span></span> |
| 2123 |
<span id="cb53-85"><a href="#cb53-85" tabindex="-1"></a><span class="co"># collapse 242.228 251.0225 269.7849 273.552 282.326 321.891 100</span></span> |
| 2124 |
<span id="cb53-86"><a href="#cb53-86" tabindex="-1"></a></span> |
| 2125 |
<span id="cb53-87"><a href="#cb53-87" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2126 |
<span id="cb53-88"><a href="#cb53-88" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gdata, sd, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2127 |
<span id="cb53-89"><a href="#cb53-89" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsd</span>(cgdata), <span class="at">times =</span> <span class="dv">2</span>)</span> |
| 2128 |
<span id="cb53-90"><a href="#cb53-90" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2129 |
<span id="cb53-91"><a href="#cb53-91" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2130 |
<span id="cb53-92"><a href="#cb53-92" tabindex="-1"></a><span class="co"># dplyr 1375.80363 1375.80363 1409.60358 1409.60358 1443.40352 1443.40352 2</span></span> |
| 2131 |
<span id="cb53-93"><a href="#cb53-93" tabindex="-1"></a><span class="co"># collapse 46.21713 46.21713 56.88205 56.88205 67.54697 67.54697 2</span></span> |
| 2132 |
<span id="cb53-94"><a href="#cb53-94" tabindex="-1"></a></span> |
| 2133 |
<span id="cb53-95"><a href="#cb53-95" tabindex="-1"></a><span class="do">## Maximum</span></span> |
| 2134 |
<span id="cb53-96"><a href="#cb53-96" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2135 |
<span id="cb53-97"><a href="#cb53-97" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gGGDC10S, max, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2136 |
<span id="cb53-98"><a href="#cb53-98" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fmax</span>(cgGGDC10S))</span> |
| 2137 |
<span id="cb53-99"><a href="#cb53-99" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2138 |
<span id="cb53-100"><a href="#cb53-100" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2139 |
<span id="cb53-101"><a href="#cb53-101" tabindex="-1"></a><span class="co"># dplyr 39964.504 41008.8560 43577.92707 41448.273 44195.1095 58816.550 100</span></span> |
| 2140 |
<span id="cb53-102"><a href="#cb53-102" tabindex="-1"></a><span class="co"># collapse 68.798 74.7225 87.83389 77.572 100.9215 129.519 100</span></span> |
| 2141 |
<span id="cb53-103"><a href="#cb53-103" tabindex="-1"></a></span> |
| 2142 |
<span id="cb53-104"><a href="#cb53-104" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2143 |
<span id="cb53-105"><a href="#cb53-105" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gdata, max, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2144 |
<span id="cb53-106"><a href="#cb53-106" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fmax</span>(cgdata), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2145 |
<span id="cb53-107"><a href="#cb53-107" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2146 |
<span id="cb53-108"><a href="#cb53-108" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2147 |
<span id="cb53-109"><a href="#cb53-109" tabindex="-1"></a><span class="co"># dplyr 480.83804 490.9982 540.7374 517.86136 533.85723 687.14713 10</span></span> |
| 2148 |
<span id="cb53-110"><a href="#cb53-110" tabindex="-1"></a><span class="co"># collapse 11.40116 11.7745 11.9366 11.85156 11.94908 13.18318 10</span></span> |
| 2149 |
<span id="cb53-111"><a href="#cb53-111" tabindex="-1"></a></span> |
| 2150 |
<span id="cb53-112"><a href="#cb53-112" tabindex="-1"></a><span class="do">## First Value</span></span> |
| 2151 |
<span id="cb53-113"><a href="#cb53-113" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2152 |
<span id="cb53-114"><a href="#cb53-114" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gGGDC10S, first),</span> |
| 2153 |
<span id="cb53-115"><a href="#cb53-115" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">ffirst</span>(cgGGDC10S, <span class="at">na.rm =</span> <span class="cn">FALSE</span>))</span> |
| 2154 |
<span id="cb53-116"><a href="#cb53-116" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2155 |
<span id="cb53-117"><a href="#cb53-117" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2156 |
<span id="cb53-118"><a href="#cb53-118" tabindex="-1"></a><span class="co"># dplyr 4147.888 4242.249 4801.88966 4383.248 4701.532 19254.215 100</span></span> |
| 2157 |
<span id="cb53-119"><a href="#cb53-119" tabindex="-1"></a><span class="co"># collapse 11.685 14.227 26.25476 24.764 35.301 137.514 100</span></span> |
| 2158 |
<span id="cb53-120"><a href="#cb53-120" tabindex="-1"></a></span> |
| 2159 |
<span id="cb53-121"><a href="#cb53-121" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2160 |
<span id="cb53-122"><a href="#cb53-122" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gdata, first),</span> |
| 2161 |
<span id="cb53-123"><a href="#cb53-123" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">ffirst</span>(cgdata, <span class="at">na.rm =</span> <span class="cn">FALSE</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2162 |
<span id="cb53-124"><a href="#cb53-124" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2163 |
<span id="cb53-125"><a href="#cb53-125" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2164 |
<span id="cb53-126"><a href="#cb53-126" tabindex="-1"></a><span class="co"># dplyr 530327.66 558767.393 637499.226 596503.08 672801.103 969373.660 10</span></span> |
| 2165 |
<span id="cb53-127"><a href="#cb53-127" tabindex="-1"></a><span class="co"># collapse 872.89 999.088 1087.845 1068.87 1204.416 1289.327 10</span></span> |
| 2166 |
<span id="cb53-128"><a href="#cb53-128" tabindex="-1"></a></span> |
| 2167 |
<span id="cb53-129"><a href="#cb53-129" tabindex="-1"></a><span class="do">## Number of Distinct Values</span></span> |
| 2168 |
<span id="cb53-130"><a href="#cb53-130" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2169 |
<span id="cb53-131"><a href="#cb53-131" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gGGDC10S, n_distinct, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2170 |
<span id="cb53-132"><a href="#cb53-132" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fndistinct</span>(cgGGDC10S))</span> |
| 2171 |
<span id="cb53-133"><a href="#cb53-133" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2172 |
<span id="cb53-134"><a href="#cb53-134" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2173 |
<span id="cb53-135"><a href="#cb53-135" tabindex="-1"></a><span class="co"># dplyr 11316.574 11600.847 12573.1010 11759.435 11939.487 31659.667 100</span></span> |
| 2174 |
<span id="cb53-136"><a href="#cb53-136" tabindex="-1"></a><span class="co"># collapse 189.051 205.164 226.0933 235.422 239.604 443.661 100</span></span> |
| 2175 |
<span id="cb53-137"><a href="#cb53-137" tabindex="-1"></a></span> |
| 2176 |
<span id="cb53-138"><a href="#cb53-138" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2177 |
<span id="cb53-139"><a href="#cb53-139" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">summarise_all</span>(gdata, n_distinct, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2178 |
<span id="cb53-140"><a href="#cb53-140" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fndistinct</span>(cgdata), <span class="at">times =</span> <span class="dv">5</span>)</span> |
| 2179 |
<span id="cb53-141"><a href="#cb53-141" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2180 |
<span id="cb53-142"><a href="#cb53-142" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2181 |
<span id="cb53-143"><a href="#cb53-143" tabindex="-1"></a><span class="co"># dplyr 2044.13376 2110.16926 2133.91960 2138.07456 2154.39797 2222.82246 5</span></span> |
| 2182 |
<span id="cb53-144"><a href="#cb53-144" tabindex="-1"></a><span class="co"># collapse 30.65443 30.94582 31.51081 31.17123 31.17972 33.60286 5</span></span> |
| 2183 |
<span id="cb53-145"><a href="#cb53-145" tabindex="-1"></a></span> |
| 2184 |
<span id="cb53-146"><a href="#cb53-146" tabindex="-1"></a><span class="fu">gc</span>()</span> |
| 2185 |
<span id="cb53-147"><a href="#cb53-147" tabindex="-1"></a><span class="co"># used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)</span></span> |
| 2186 |
<span id="cb53-148"><a href="#cb53-148" tabindex="-1"></a><span class="co"># Ncells 3972309 212.2 8862174 473.3 NA 8862174 473.3</span></span> |
| 2187 |
<span id="cb53-149"><a href="#cb53-149" tabindex="-1"></a><span class="co"># Vcells 24857303 189.7 75772825 578.2 16384 445825141 3401.4</span></span></code></pre></div> |
| 2188 |
<!-- The benchmarks show that at this data size efficient primitives like `base::sum` or `base::max` can still deliver very decent performance with `summarize`. Less optimized base functions like `mean`, `median` and `sd` however take multiple seconds to compute, and here `collapse` fast functions really prove to be very useful complements to the *dplyr* system. --> |
| 2189 |
<!-- Weighted statistics are also performed extremely fast by *collapse* functions. I would not know how to compute weighted statistics by groups in *dplyr*, as it would require the weighting variable to be split as well, which seems impossible in native *dplyr*. --> |
| 2190 |
<!-- A further highlight of *collapse* is the extremely fast statistical mode function, which can also compute a weighted mode. Fast categorical aggregation has been an issue in R, and defining a mode function from base R and applying it to 17000 groups will probably let it run at least a minute. `fmode` reduces this time to half a second. --> |
| 2191 |
<!-- Thus in terms of data aggregation *collapse* fast functions are able to speed up *dplyr* to a level that makes it attractive again to R users working on medium-sized or larger data, and everyone programming with *dplyr*. I however strongly recommend *collapse* itself for easy and speedy programming as it does not rely on non-standard evaluation and has less R-overhead than *dplyr*. --> |
| 2192 |
<!-- In all of this the grouping system of *dplyr* remains the central bottleneck. For example grouping 10 million observations in 1 million groups takes around 10 second with `group_by`, whereas `GRP` takes around 1.5 seconds, and this difference grows exponentially as data get larger. Rewriting `group_by` using `GRP` / `radixorderv` and then writing a simple C++ conversion program for the grouping object could be a quick remedy for this issue, but that is at the discretion of Hadley Wickham and coauthors. --> |
| 2193 |
<!-- (If you need that speed program with *collapse* or use *data.table* with GeForce optimized functions). --> |
| 2194 |
<p>Below are some additional benchmarks for weighted aggregations and |
| 2195 |
aggregations using the statistical mode, which cannot easily or |
| 2196 |
efficiently be performed with <em>dplyr</em>.</p> |
| 2197 |
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" tabindex="-1"></a><span class="do">## Weighted Mean</span></span> |
| 2198 |
<span id="cb54-2"><a href="#cb54-2" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2199 |
<span id="cb54-3"><a href="#cb54-3" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fmean</span>(cgGGDC10S, SUM)) </span> |
| 2200 |
<span id="cb54-4"><a href="#cb54-4" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2201 |
<span id="cb54-5"><a href="#cb54-5" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2202 |
<span id="cb54-6"><a href="#cb54-6" tabindex="-1"></a><span class="co"># fmean(cgGGDC10S, SUM) 195.488 200.4285 218.2836 211.1295 218.8375 444.276 100</span></span> |
| 2203 |
<span id="cb54-7"><a href="#cb54-7" tabindex="-1"></a></span> |
| 2204 |
<span id="cb54-8"><a href="#cb54-8" tabindex="-1"></a><span class="co"># Large </span></span> |
| 2205 |
<span id="cb54-9"><a href="#cb54-9" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fmean</span>(cgdata, SUM), <span class="at">times =</span> <span class="dv">10</span>) </span> |
| 2206 |
<span id="cb54-10"><a href="#cb54-10" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2207 |
<span id="cb54-11"><a href="#cb54-11" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2208 |
<span id="cb54-12"><a href="#cb54-12" tabindex="-1"></a><span class="co"># fmean(cgdata, SUM) 34.73516 35.28276 35.66689 35.32257 36.44802 36.80722 10</span></span> |
| 2209 |
<span id="cb54-13"><a href="#cb54-13" tabindex="-1"></a></span> |
| 2210 |
<span id="cb54-14"><a href="#cb54-14" tabindex="-1"></a><span class="do">## Weighted Standard-Deviation</span></span> |
| 2211 |
<span id="cb54-15"><a href="#cb54-15" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2212 |
<span id="cb54-16"><a href="#cb54-16" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fsd</span>(cgGGDC10S, SUM)) </span> |
| 2213 |
<span id="cb54-17"><a href="#cb54-17" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2214 |
<span id="cb54-18"><a href="#cb54-18" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2215 |
<span id="cb54-19"><a href="#cb54-19" tabindex="-1"></a><span class="co"># fsd(cgGGDC10S, SUM) 243.048 244.606 249.2181 246.9635 249.444 323.9 100</span></span> |
| 2216 |
<span id="cb54-20"><a href="#cb54-20" tabindex="-1"></a></span> |
| 2217 |
<span id="cb54-21"><a href="#cb54-21" tabindex="-1"></a><span class="co"># Large </span></span> |
| 2218 |
<span id="cb54-22"><a href="#cb54-22" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fsd</span>(cgdata, SUM), <span class="at">times =</span> <span class="dv">10</span>) </span> |
| 2219 |
<span id="cb54-23"><a href="#cb54-23" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2220 |
<span id="cb54-24"><a href="#cb54-24" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2221 |
<span id="cb54-25"><a href="#cb54-25" tabindex="-1"></a><span class="co"># fsd(cgdata, SUM) 44.905 44.93116 45.15391 45.01095 45.22677 46.14689 10</span></span> |
| 2222 |
<span id="cb54-26"><a href="#cb54-26" tabindex="-1"></a></span> |
| 2223 |
<span id="cb54-27"><a href="#cb54-27" tabindex="-1"></a><span class="do">## Statistical Mode</span></span> |
| 2224 |
<span id="cb54-28"><a href="#cb54-28" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2225 |
<span id="cb54-29"><a href="#cb54-29" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fmode</span>(cgGGDC10S)) </span> |
| 2226 |
<span id="cb54-30"><a href="#cb54-30" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2227 |
<span id="cb54-31"><a href="#cb54-31" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2228 |
<span id="cb54-32"><a href="#cb54-32" tabindex="-1"></a><span class="co"># fmode(cgGGDC10S) 245.098 248.3575 253.4809 250.6945 253.9335 420.619 100</span></span> |
| 2229 |
<span id="cb54-33"><a href="#cb54-33" tabindex="-1"></a></span> |
| 2230 |
<span id="cb54-34"><a href="#cb54-34" tabindex="-1"></a><span class="co"># Large </span></span> |
| 2231 |
<span id="cb54-35"><a href="#cb54-35" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fmode</span>(cgdata), <span class="at">times =</span> <span class="dv">10</span>) </span> |
| 2232 |
<span id="cb54-36"><a href="#cb54-36" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2233 |
<span id="cb54-37"><a href="#cb54-37" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2234 |
<span id="cb54-38"><a href="#cb54-38" tabindex="-1"></a><span class="co"># fmode(cgdata) 40.26151 41.82082 41.63019 41.88382 42.0232 42.0587 10</span></span> |
| 2235 |
<span id="cb54-39"><a href="#cb54-39" tabindex="-1"></a></span> |
| 2236 |
<span id="cb54-40"><a href="#cb54-40" tabindex="-1"></a><span class="do">## Weighted Statistical Mode</span></span> |
| 2237 |
<span id="cb54-41"><a href="#cb54-41" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2238 |
<span id="cb54-42"><a href="#cb54-42" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fmode</span>(cgGGDC10S, SUM)) </span> |
| 2239 |
<span id="cb54-43"><a href="#cb54-43" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2240 |
<span id="cb54-44"><a href="#cb54-44" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2241 |
<span id="cb54-45"><a href="#cb54-45" tabindex="-1"></a><span class="co"># fmode(cgGGDC10S, SUM) 330.993 333.535 337.7744 334.5395 337.3685 447.187 100</span></span> |
| 2242 |
<span id="cb54-46"><a href="#cb54-46" tabindex="-1"></a></span> |
| 2243 |
<span id="cb54-47"><a href="#cb54-47" tabindex="-1"></a><span class="co"># Large </span></span> |
| 2244 |
<span id="cb54-48"><a href="#cb54-48" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fmode</span>(cgdata, SUM), <span class="at">times =</span> <span class="dv">10</span>) </span> |
| 2245 |
<span id="cb54-49"><a href="#cb54-49" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2246 |
<span id="cb54-50"><a href="#cb54-50" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2247 |
<span id="cb54-51"><a href="#cb54-51" tabindex="-1"></a><span class="co"># fmode(cgdata, SUM) 57.69815 57.78466 57.98187 57.84567 58.09942 58.81835 10</span></span> |
| 2248 |
<span id="cb54-52"><a href="#cb54-52" tabindex="-1"></a></span> |
| 2249 |
<span id="cb54-53"><a href="#cb54-53" tabindex="-1"></a><span class="fu">gc</span>()</span> |
| 2250 |
<span id="cb54-54"><a href="#cb54-54" tabindex="-1"></a><span class="co"># used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)</span></span> |
| 2251 |
<span id="cb54-55"><a href="#cb54-55" tabindex="-1"></a><span class="co"># Ncells 3971768 212.2 8862174 473.3 NA 8862174 473.3</span></span> |
| 2252 |
<span id="cb54-56"><a href="#cb54-56" tabindex="-1"></a><span class="co"># Vcells 24853915 189.7 75772825 578.2 16384 445825141 3401.4</span></span></code></pre></div> |
| 2253 |
</div> |
| 2254 |
<div id="transformation" class="section level3"> |
| 2255 |
<h3>3.2 Transformation</h3> |
| 2256 |
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" tabindex="-1"></a></span> |
| 2257 |
<span id="cb55-2"><a href="#cb55-2" tabindex="-1"></a><span class="do">## Replacing with group sum</span></span> |
| 2258 |
<span id="cb55-3"><a href="#cb55-3" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2259 |
<span id="cb55-4"><a href="#cb55-4" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate_all</span>(gGGDC10S, sum, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2260 |
<span id="cb55-5"><a href="#cb55-5" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsum</span>(cgGGDC10S, <span class="at">TRA =</span> <span class="st">"replace_fill"</span>))</span> |
| 2261 |
<span id="cb55-6"><a href="#cb55-6" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2262 |
<span id="cb55-7"><a href="#cb55-7" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2263 |
<span id="cb55-8"><a href="#cb55-8" tabindex="-1"></a><span class="co"># dplyr 13088.102 13223.340 14388.9000 13359.7680 14380.05 29060.554 100</span></span> |
| 2264 |
<span id="cb55-9"><a href="#cb55-9" tabindex="-1"></a><span class="co"># collapse 238.456 273.757 292.1693 293.9905 312.01 388.106 100</span></span> |
| 2265 |
<span id="cb55-10"><a href="#cb55-10" tabindex="-1"></a></span> |
| 2266 |
<span id="cb55-11"><a href="#cb55-11" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2267 |
<span id="cb55-12"><a href="#cb55-12" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate_all</span>(gdata, sum, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 2268 |
<span id="cb55-13"><a href="#cb55-13" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsum</span>(cgdata, <span class="at">TRA =</span> <span class="st">"replace_fill"</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2269 |
<span id="cb55-14"><a href="#cb55-14" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2270 |
<span id="cb55-15"><a href="#cb55-15" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2271 |
<span id="cb55-16"><a href="#cb55-16" tabindex="-1"></a><span class="co"># dplyr 391.63618 679.62609 662.91807 716.40975 729.7527 749.4973 10</span></span> |
| 2272 |
<span id="cb55-17"><a href="#cb55-17" tabindex="-1"></a><span class="co"># collapse 49.63788 50.24189 61.77658 55.18416 63.4596 111.6039 10</span></span> |
| 2273 |
<span id="cb55-18"><a href="#cb55-18" tabindex="-1"></a></span> |
| 2274 |
<span id="cb55-19"><a href="#cb55-19" tabindex="-1"></a><span class="do">## Dividing by group sum</span></span> |
| 2275 |
<span id="cb55-20"><a href="#cb55-20" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2276 |
<span id="cb55-21"><a href="#cb55-21" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate_all</span>(gGGDC10S, <span class="cf">function</span>(x) x<span class="sc">/</span><span class="fu">sum</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)),</span> |
| 2277 |
<span id="cb55-22"><a href="#cb55-22" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsum</span>(cgGGDC10S, <span class="at">TRA =</span> <span class="st">"/"</span>))</span> |
| 2278 |
<span id="cb55-23"><a href="#cb55-23" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2279 |
<span id="cb55-24"><a href="#cb55-24" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2280 |
<span id="cb55-25"><a href="#cb55-25" tabindex="-1"></a><span class="co"># dplyr 13058.992 13203.8450 14294.3733 13321.41 13880.796 42300.028 100</span></span> |
| 2281 |
<span id="cb55-26"><a href="#cb55-26" tabindex="-1"></a><span class="co"># collapse 242.884 268.5295 278.8541 274.29 294.585 330.255 100</span></span> |
| 2282 |
<span id="cb55-27"><a href="#cb55-27" tabindex="-1"></a></span> |
| 2283 |
<span id="cb55-28"><a href="#cb55-28" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2284 |
<span id="cb55-29"><a href="#cb55-29" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate_all</span>(gdata, <span class="cf">function</span>(x) x<span class="sc">/</span><span class="fu">sum</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)),</span> |
| 2285 |
<span id="cb55-30"><a href="#cb55-30" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fsum</span>(cgdata, <span class="at">TRA =</span> <span class="st">"/"</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2286 |
<span id="cb55-31"><a href="#cb55-31" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2287 |
<span id="cb55-32"><a href="#cb55-32" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2288 |
<span id="cb55-33"><a href="#cb55-33" tabindex="-1"></a><span class="co"># dplyr 474.9046 654.6199 796.14248 907.32863 942.32567 999.2501 10</span></span> |
| 2289 |
<span id="cb55-34"><a href="#cb55-34" tabindex="-1"></a><span class="co"># collapse 49.3542 50.9056 84.66647 52.05635 74.51705 325.4319 10</span></span> |
| 2290 |
<span id="cb55-35"><a href="#cb55-35" tabindex="-1"></a></span> |
| 2291 |
<span id="cb55-36"><a href="#cb55-36" tabindex="-1"></a><span class="do">## Centering</span></span> |
| 2292 |
<span id="cb55-37"><a href="#cb55-37" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2293 |
<span id="cb55-38"><a href="#cb55-38" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate_all</span>(gGGDC10S, <span class="cf">function</span>(x) x<span class="sc">-</span><span class="fu">mean.default</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)),</span> |
| 2294 |
<span id="cb55-39"><a href="#cb55-39" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fwithin</span>(cgGGDC10S))</span> |
| 2295 |
<span id="cb55-40"><a href="#cb55-40" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2296 |
<span id="cb55-41"><a href="#cb55-41" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2297 |
<span id="cb55-42"><a href="#cb55-42" tabindex="-1"></a><span class="co"># dplyr 14460.04 14769.4095 15977.4942 14859.815 15013.421 37113.077 100</span></span> |
| 2298 |
<span id="cb55-43"><a href="#cb55-43" tabindex="-1"></a><span class="co"># collapse 203.77 229.7845 246.5043 242.638 266.664 293.191 100</span></span> |
| 2299 |
<span id="cb55-44"><a href="#cb55-44" tabindex="-1"></a></span> |
| 2300 |
<span id="cb55-45"><a href="#cb55-45" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2301 |
<span id="cb55-46"><a href="#cb55-46" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate_all</span>(gdata, <span class="cf">function</span>(x) x<span class="sc">-</span><span class="fu">mean.default</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)),</span> |
| 2302 |
<span id="cb55-47"><a href="#cb55-47" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fwithin</span>(cgdata), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2303 |
<span id="cb55-48"><a href="#cb55-48" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2304 |
<span id="cb55-49"><a href="#cb55-49" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2305 |
<span id="cb55-50"><a href="#cb55-50" tabindex="-1"></a><span class="co"># dplyr 893.06503 925.50231 1217.2225 1259.34620 1445.254 1545.5490 10</span></span> |
| 2306 |
<span id="cb55-51"><a href="#cb55-51" tabindex="-1"></a><span class="co"># collapse 43.90731 56.97093 143.4797 73.39498 152.872 429.3341 10</span></span> |
| 2307 |
<span id="cb55-52"><a href="#cb55-52" tabindex="-1"></a></span> |
| 2308 |
<span id="cb55-53"><a href="#cb55-53" tabindex="-1"></a><span class="do">## Centering and Scaling (Standardizing)</span></span> |
| 2309 |
<span id="cb55-54"><a href="#cb55-54" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2310 |
<span id="cb55-55"><a href="#cb55-55" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate_all</span>(gGGDC10S, <span class="cf">function</span>(x) (x<span class="sc">-</span><span class="fu">mean.default</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>))<span class="sc">/</span><span class="fu">sd</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)),</span> |
| 2311 |
<span id="cb55-56"><a href="#cb55-56" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fscale</span>(cgGGDC10S))</span> |
| 2312 |
<span id="cb55-57"><a href="#cb55-57" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2313 |
<span id="cb55-58"><a href="#cb55-58" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2314 |
<span id="cb55-59"><a href="#cb55-59" tabindex="-1"></a><span class="co"># dplyr 20275.033 21145.524 24976.1242 22214.190 25194.0285 79869.435 100</span></span> |
| 2315 |
<span id="cb55-60"><a href="#cb55-60" tabindex="-1"></a><span class="co"># collapse 277.775 304.958 323.3613 314.388 338.2705 437.388 100</span></span> |
| 2316 |
<span id="cb55-61"><a href="#cb55-61" tabindex="-1"></a></span> |
| 2317 |
<span id="cb55-62"><a href="#cb55-62" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2318 |
<span id="cb55-63"><a href="#cb55-63" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr =</span> <span class="fu">mutate_all</span>(gdata, <span class="cf">function</span>(x) (x<span class="sc">-</span><span class="fu">mean.default</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>))<span class="sc">/</span><span class="fu">sd</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)),</span> |
| 2319 |
<span id="cb55-64"><a href="#cb55-64" tabindex="-1"></a> <span class="at">collapse =</span> <span class="fu">fscale</span>(cgdata), <span class="at">times =</span> <span class="dv">2</span>)</span> |
| 2320 |
<span id="cb55-65"><a href="#cb55-65" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2321 |
<span id="cb55-66"><a href="#cb55-66" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2322 |
<span id="cb55-67"><a href="#cb55-67" tabindex="-1"></a><span class="co"># dplyr 2118.97696 2118.97696 2315.9282 2315.9282 2512.87938 2512.87938 2</span></span> |
| 2323 |
<span id="cb55-68"><a href="#cb55-68" tabindex="-1"></a><span class="co"># collapse 60.17144 60.17144 60.6284 60.6284 61.08537 61.08537 2</span></span> |
| 2324 |
<span id="cb55-69"><a href="#cb55-69" tabindex="-1"></a></span> |
| 2325 |
<span id="cb55-70"><a href="#cb55-70" tabindex="-1"></a><span class="do">## Lag</span></span> |
| 2326 |
<span id="cb55-71"><a href="#cb55-71" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2327 |
<span id="cb55-72"><a href="#cb55-72" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr_unordered =</span> <span class="fu">mutate</span>(gGGDC10S, <span class="fu">across</span>(<span class="fu">everything</span>(), dplyr<span class="sc">::</span>lag)),</span> |
| 2328 |
<span id="cb55-73"><a href="#cb55-73" tabindex="-1"></a> <span class="at">collapse_unordered =</span> <span class="fu">flag</span>(cgGGDC10S),</span> |
| 2329 |
<span id="cb55-74"><a href="#cb55-74" tabindex="-1"></a> <span class="at">dplyr_ordered =</span> <span class="fu">mutate</span>(gGGDC10S, <span class="fu">across</span>(<span class="fu">everything</span>(), \(x) dplyr<span class="sc">::</span><span class="fu">lag</span>(x, <span class="at">order_by =</span> Year))),</span> |
| 2330 |
<span id="cb55-75"><a href="#cb55-75" tabindex="-1"></a> <span class="at">collapse_ordered =</span> <span class="fu">flag</span>(cgGGDC10S, <span class="at">t =</span> Year))</span> |
| 2331 |
<span id="cb55-76"><a href="#cb55-76" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2332 |
<span id="cb55-77"><a href="#cb55-77" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2333 |
<span id="cb55-78"><a href="#cb55-78" tabindex="-1"></a><span class="co"># dplyr_unordered 14495.386 14796.101 17579.85413 15265.3250 15889.7550 49137.721 100</span></span> |
| 2334 |
<span id="cb55-79"><a href="#cb55-79" tabindex="-1"></a><span class="co"># collapse_unordered 48.544 75.071 90.29225 86.6330 109.6545 225.377 100</span></span> |
| 2335 |
<span id="cb55-80"><a href="#cb55-80" tabindex="-1"></a><span class="co"># dplyr_ordered 24893.437 25327.607 27521.59809 25904.9275 27136.2190 51312.074 100</span></span> |
| 2336 |
<span id="cb55-81"><a href="#cb55-81" tabindex="-1"></a><span class="co"># collapse_ordered 80.196 107.953 120.85160 117.5675 131.6715 189.051 100</span></span> |
| 2337 |
<span id="cb55-82"><a href="#cb55-82" tabindex="-1"></a></span> |
| 2338 |
<span id="cb55-83"><a href="#cb55-83" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2339 |
<span id="cb55-84"><a href="#cb55-84" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr_unordered =</span> <span class="fu">mutate</span>(gdata, <span class="fu">across</span>(<span class="fu">everything</span>(), dplyr<span class="sc">::</span>lag)),</span> |
| 2340 |
<span id="cb55-85"><a href="#cb55-85" tabindex="-1"></a> <span class="at">collapse_unordered =</span> <span class="fu">flag</span>(cgdata),</span> |
| 2341 |
<span id="cb55-86"><a href="#cb55-86" tabindex="-1"></a> <span class="at">dplyr_ordered =</span> <span class="fu">mutate</span>(gdata, <span class="fu">across</span>(<span class="fu">everything</span>(), \(x) dplyr<span class="sc">::</span><span class="fu">lag</span>(x, <span class="at">order_by =</span> Year))),</span> |
| 2342 |
<span id="cb55-87"><a href="#cb55-87" tabindex="-1"></a> <span class="at">collapse_ordered =</span> <span class="fu">flag</span>(cgdata, <span class="at">t =</span> Year), <span class="at">times =</span> <span class="dv">2</span>)</span> |
| 2343 |
<span id="cb55-88"><a href="#cb55-88" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2344 |
<span id="cb55-89"><a href="#cb55-89" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2345 |
<span id="cb55-90"><a href="#cb55-90" tabindex="-1"></a><span class="co"># dplyr_unordered 3461.11500 3461.11500 3471.95821 3471.95821 3482.80142 3482.80142 2</span></span> |
| 2346 |
<span id="cb55-91"><a href="#cb55-91" tabindex="-1"></a><span class="co"># collapse_unordered 13.71897 13.71897 211.59809 211.59809 409.47721 409.47721 2</span></span> |
| 2347 |
<span id="cb55-92"><a href="#cb55-92" tabindex="-1"></a><span class="co"># dplyr_ordered 5786.57522 5786.57522 6291.90389 6291.90389 6797.23256 6797.23256 2</span></span> |
| 2348 |
<span id="cb55-93"><a href="#cb55-93" tabindex="-1"></a><span class="co"># collapse_ordered 25.14399 25.14399 35.36102 35.36102 45.57806 45.57806 2</span></span> |
| 2349 |
<span id="cb55-94"><a href="#cb55-94" tabindex="-1"></a></span> |
| 2350 |
<span id="cb55-95"><a href="#cb55-95" tabindex="-1"></a><span class="do">## First-Difference (unordered)</span></span> |
| 2351 |
<span id="cb55-96"><a href="#cb55-96" tabindex="-1"></a><span class="co"># Small</span></span> |
| 2352 |
<span id="cb55-97"><a href="#cb55-97" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr_unordered =</span> <span class="fu">mutate_all</span>(gGGDC10S, <span class="cf">function</span>(x) x <span class="sc">-</span> dplyr<span class="sc">::</span><span class="fu">lag</span>(x)),</span> |
| 2353 |
<span id="cb55-98"><a href="#cb55-98" tabindex="-1"></a> <span class="at">collapse_unordered =</span> <span class="fu">fdiff</span>(cgGGDC10S))</span> |
| 2354 |
<span id="cb55-99"><a href="#cb55-99" tabindex="-1"></a><span class="co"># Unit: microseconds</span></span> |
| 2355 |
<span id="cb55-100"><a href="#cb55-100" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2356 |
<span id="cb55-101"><a href="#cb55-101" tabindex="-1"></a><span class="co"># dplyr_unordered 25613.274 25878.0725 27951.41954 26257.3225 27226.808 43048.893 100</span></span> |
| 2357 |
<span id="cb55-102"><a href="#cb55-102" tabindex="-1"></a><span class="co"># collapse_unordered 56.539 72.3035 95.72147 91.6965 102.664 254.077 100</span></span> |
| 2358 |
<span id="cb55-103"><a href="#cb55-103" tabindex="-1"></a></span> |
| 2359 |
<span id="cb55-104"><a href="#cb55-104" tabindex="-1"></a><span class="co"># Large</span></span> |
| 2360 |
<span id="cb55-105"><a href="#cb55-105" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="at">dplyr_unordered =</span> <span class="fu">mutate_all</span>(gdata, <span class="cf">function</span>(x) x <span class="sc">-</span> dplyr<span class="sc">::</span><span class="fu">lag</span>(x)),</span> |
| 2361 |
<span id="cb55-106"><a href="#cb55-106" tabindex="-1"></a> <span class="at">collapse_unordered =</span> <span class="fu">fdiff</span>(cgdata), <span class="at">times =</span> <span class="dv">2</span>)</span> |
| 2362 |
<span id="cb55-107"><a href="#cb55-107" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2363 |
<span id="cb55-108"><a href="#cb55-108" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2364 |
<span id="cb55-109"><a href="#cb55-109" tabindex="-1"></a><span class="co"># dplyr_unordered 3287.88487 3287.88487 3425.69703 3425.69703 3563.509 3563.509 2</span></span> |
| 2365 |
<span id="cb55-110"><a href="#cb55-110" tabindex="-1"></a><span class="co"># collapse_unordered 16.58971 16.58971 23.36885 23.36885 30.148 30.148 2</span></span> |
| 2366 |
<span id="cb55-111"><a href="#cb55-111" tabindex="-1"></a></span> |
| 2367 |
<span id="cb55-112"><a href="#cb55-112" tabindex="-1"></a><span class="fu">gc</span>()</span> |
| 2368 |
<span id="cb55-113"><a href="#cb55-113" tabindex="-1"></a><span class="co"># used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)</span></span> |
| 2369 |
<span id="cb55-114"><a href="#cb55-114" tabindex="-1"></a><span class="co"># Ncells 3978800 212.5 8862175 473.3 NA 8862175 473.3</span></span> |
| 2370 |
<span id="cb55-115"><a href="#cb55-115" tabindex="-1"></a><span class="co"># Vcells 24870572 189.8 72805912 555.5 16384 445825141 3401.4</span></span></code></pre></div> |
| 2371 |
<p>Below again some benchmarks for transformations not easily of |
| 2372 |
efficiently performed with <em>dplyr</em>, such as centering on the |
| 2373 |
overall mean, mean-preserving scaling, weighted scaling and centering, |
| 2374 |
sequences of lags / leads, (iterated) panel-differences and growth |
| 2375 |
rates.</p> |
| 2376 |
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" tabindex="-1"></a><span class="co"># Centering on overall mean</span></span> |
| 2377 |
<span id="cb56-2"><a href="#cb56-2" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fwithin</span>(cgdata, <span class="at">mean =</span> <span class="st">"overall.mean"</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2378 |
<span id="cb56-3"><a href="#cb56-3" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2379 |
<span id="cb56-4"><a href="#cb56-4" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2380 |
<span id="cb56-5"><a href="#cb56-5" tabindex="-1"></a><span class="co"># fwithin(cgdata, mean = "overall.mean") 44.66782 48.03445 52.04073 50.07953 53.67134 71.13221 10</span></span> |
| 2381 |
<span id="cb56-6"><a href="#cb56-6" tabindex="-1"></a></span> |
| 2382 |
<span id="cb56-7"><a href="#cb56-7" tabindex="-1"></a><span class="co"># Weighted Centering</span></span> |
| 2383 |
<span id="cb56-8"><a href="#cb56-8" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fwithin</span>(cgdata, SUM), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2384 |
<span id="cb56-9"><a href="#cb56-9" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2385 |
<span id="cb56-10"><a href="#cb56-10" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2386 |
<span id="cb56-11"><a href="#cb56-11" tabindex="-1"></a><span class="co"># fwithin(cgdata, SUM) 40.45204 42.86833 46.55326 46.18277 47.28202 57.82673 10</span></span> |
| 2387 |
<span id="cb56-12"><a href="#cb56-12" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fwithin</span>(cgdata, SUM, <span class="at">mean =</span> <span class="st">"overall.mean"</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2388 |
<span id="cb56-13"><a href="#cb56-13" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2389 |
<span id="cb56-14"><a href="#cb56-14" tabindex="-1"></a><span class="co"># expr min lq mean median uq max</span></span> |
| 2390 |
<span id="cb56-15"><a href="#cb56-15" tabindex="-1"></a><span class="co"># fwithin(cgdata, SUM, mean = "overall.mean") 39.99279 40.32256 43.0638 40.60269 41.34366 54.45542</span></span> |
| 2391 |
<span id="cb56-16"><a href="#cb56-16" tabindex="-1"></a><span class="co"># neval</span></span> |
| 2392 |
<span id="cb56-17"><a href="#cb56-17" tabindex="-1"></a><span class="co"># 10</span></span> |
| 2393 |
<span id="cb56-18"><a href="#cb56-18" tabindex="-1"></a></span> |
| 2394 |
<span id="cb56-19"><a href="#cb56-19" tabindex="-1"></a><span class="co"># Weighted Scaling and Standardizing</span></span> |
| 2395 |
<span id="cb56-20"><a href="#cb56-20" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fsd</span>(cgdata, SUM, <span class="at">TRA =</span> <span class="st">"/"</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2396 |
<span id="cb56-21"><a href="#cb56-21" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2397 |
<span id="cb56-22"><a href="#cb56-22" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2398 |
<span id="cb56-23"><a href="#cb56-23" tabindex="-1"></a><span class="co"># fsd(cgdata, SUM, TRA = "/") 50.19536 50.9145 55.12553 53.23862 56.27094 67.46816 10</span></span> |
| 2399 |
<span id="cb56-24"><a href="#cb56-24" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fscale</span>(cgdata, SUM), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2400 |
<span id="cb56-25"><a href="#cb56-25" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2401 |
<span id="cb56-26"><a href="#cb56-26" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2402 |
<span id="cb56-27"><a href="#cb56-27" tabindex="-1"></a><span class="co"># fscale(cgdata, SUM) 54.14792 57.64584 60.83251 59.88025 61.16425 72.31928 10</span></span> |
| 2403 |
<span id="cb56-28"><a href="#cb56-28" tabindex="-1"></a></span> |
| 2404 |
<span id="cb56-29"><a href="#cb56-29" tabindex="-1"></a><span class="co"># Sequence of lags and leads</span></span> |
| 2405 |
<span id="cb56-30"><a href="#cb56-30" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">flag</span>(cgdata, <span class="sc">-</span><span class="dv">1</span><span class="sc">:</span><span class="dv">1</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2406 |
<span id="cb56-31"><a href="#cb56-31" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2407 |
<span id="cb56-32"><a href="#cb56-32" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2408 |
<span id="cb56-33"><a href="#cb56-33" tabindex="-1"></a><span class="co"># flag(cgdata, -1:1) 26.03902 48.02695 194.8518 257.0652 264.5479 276.5348 10</span></span> |
| 2409 |
<span id="cb56-34"><a href="#cb56-34" tabindex="-1"></a></span> |
| 2410 |
<span id="cb56-35"><a href="#cb56-35" tabindex="-1"></a><span class="co"># Iterated difference</span></span> |
| 2411 |
<span id="cb56-36"><a href="#cb56-36" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fdiff</span>(cgdata, <span class="dv">1</span>, <span class="dv">2</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2412 |
<span id="cb56-37"><a href="#cb56-37" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2413 |
<span id="cb56-38"><a href="#cb56-38" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2414 |
<span id="cb56-39"><a href="#cb56-39" tabindex="-1"></a><span class="co"># fdiff(cgdata, 1, 2) 38.76001 39.83896 44.93731 41.08887 48.98348 63.42528 10</span></span> |
| 2415 |
<span id="cb56-40"><a href="#cb56-40" tabindex="-1"></a></span> |
| 2416 |
<span id="cb56-41"><a href="#cb56-41" tabindex="-1"></a><span class="co"># Growth Rate</span></span> |
| 2417 |
<span id="cb56-42"><a href="#cb56-42" tabindex="-1"></a><span class="fu">microbenchmark</span>(<span class="fu">fgrowth</span>(cgdata,<span class="dv">1</span>), <span class="at">times =</span> <span class="dv">10</span>)</span> |
| 2418 |
<span id="cb56-43"><a href="#cb56-43" tabindex="-1"></a><span class="co"># Unit: milliseconds</span></span> |
| 2419 |
<span id="cb56-44"><a href="#cb56-44" tabindex="-1"></a><span class="co"># expr min lq mean median uq max neval</span></span> |
| 2420 |
<span id="cb56-45"><a href="#cb56-45" tabindex="-1"></a><span class="co"># fgrowth(cgdata, 1) 11.58627 13.81528 18.05776 14.03489 22.34279 31.15811 10</span></span></code></pre></div> |
| 2421 |
<!-- Again the benchmarks show stunning performance gains using *collapse* functions. --> |
| 2422 |
</div> |
| 2423 |
</div> |
| 2424 |
<div id="references" class="section level2"> |
| 2425 |
<h2>References</h2> |
| 2426 |
<p>Timmer, M. P., de Vries, G. J., & de Vries, K. (2015). “Patterns |
| 2427 |
of Structural Change in Developing Countries.” . In J. Weiss, & M. |
| 2428 |
Tribe (Eds.), <em>Routledge Handbook of Industry and Development.</em> |
| 2429 |
(pp. 65-83). Routledge.</p> |
| 2430 |
<p>Cochrane, D. & Orcutt, G. H. (1949). “Application of Least |
| 2431 |
Squares Regression to Relationships Containing Auto-Correlated Error |
| 2432 |
Terms”. <em>Journal of the American Statistical Association.</em> 44 |
| 2433 |
(245): 32–61.</p> |
| 2434 |
<p>Prais, S. J. & Winsten, C. B. (1954). “Trend Estimators and |
| 2435 |
Serial Correlation”. <em>Cowles Commission Discussion Paper |
| 2436 |
No. 383.</em> Chicago.</p> |
| 2437 |
</div> |
| 2438 |
<div class="footnotes footnotes-end-of-document"> |
| 2439 |
<hr /> |
| 2440 |
<ol> |
| 2441 |
<li id="fn1"><p>Row-wise operations are not supported by TRA.<a href="#fnref1" class="footnote-back">↩︎</a></p></li> |
| 2442 |
</ol> |
| 2443 |
</div> |
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
<!-- code folding --> |
| 2448 |
|
| 2449 |
|
| 2450 |
<!-- dynamically load mathjax for compatibility with self-contained --> |
| 2451 |
<script> |
| 2452 |
(function () { |
| 2453 |
var script = document.createElement("script"); |
| 2454 |
script.type = "text/javascript"; |
| 2455 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 2456 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 2457 |
})(); |
| 2458 |
</script> |
| 2459 |
|
| 2460 |
</body> |
| 2461 |
</html> |