1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Row-wise operations</title>
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
}
});
</script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; }
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.at { color: #7d9029; }
code span.bn { color: #40a070; }
code span.bu { color: #008000; }
code span.cf { color: #007020; font-weight: bold; }
code span.ch { color: #4070a0; }
code span.cn { color: #880000; }
code span.co { color: #60a0b0; font-style: italic; }
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.do { color: #ba2121; font-style: italic; }
code span.dt { color: #902000; }
code span.dv { color: #40a070; }
code span.er { color: #ff0000; font-weight: bold; }
code span.ex { }
code span.fl { color: #40a070; }
code span.fu { color: #06287e; }
code span.im { color: #008000; font-weight: bold; }
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.kw { color: #007020; font-weight: bold; }
code span.op { color: #666666; }
code span.ot { color: #007020; }
code span.pp { color: #bc7a00; }
code span.sc { color: #4070a0; }
code span.ss { color: #bb6688; }
code span.st { color: #4070a0; }
code span.va { color: #19177c; }
code span.vs { color: #4070a0; }
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
var j = 0;
while (j < rules.length) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
j++;
continue;
}
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') {
j++;
continue;
}
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap;
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }
code > span.kw { color: #555; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #d14; }
code > span.fl { color: #d14; }
code > span.ch { color: #d14; }
code > span.st { color: #d14; }
code > span.co { color: #888888; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #900; font-weight: bold; }
code > span.er { color: #a61717; background-color: #e3d2d2; }
</style>
</head>
<body>
<h1 class="title toc-ignore">Row-wise operations</h1>
<p>dplyr, and R in general, are particularly well suited to performing
operations over columns, and performing operations over rows is much
harder. In this vignette, you’ll learn dplyr’s approach centred around
the row-wise data frame created by <code>rowwise()</code>.</p>
<p>There are three common use cases that we discuss in this
vignette:</p>
<ul>
<li>Row-wise aggregates (e.g. compute the mean of x, y, z).</li>
<li>Calling a function multiple times with varying arguments.</li>
<li>Working with list-columns.</li>
</ul>
<p>These types of problems are often easily solved with a for loop, but
it’s nice to have a solution that fits naturally into a pipeline.</p>
<blockquote>
<p>Of course, someone has to write loops. It doesn’t have to be you. —
Jenny Bryan</p>
</blockquote>
<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">library</span>(dplyr, <span class="at">warn.conflicts =</span> <span class="cn">FALSE</span>)</span></code></pre></div>
<div id="creating" class="section level2">
<h2>Creating</h2>
<p>Row-wise operations require a special type of grouping where each
group consists of a single row. You create this with
<code>rowwise()</code>:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="at">y =</span> <span class="dv">3</span><span class="sc">:</span><span class="dv">4</span>, <span class="at">z =</span> <span class="dv">5</span><span class="sc">:</span><span class="dv">6</span>)</span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">rowwise</span>()</span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 3</span></span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co">#> # Rowwise: </span></span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="co">#> x y z</span></span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co">#> <int> <int> <int></span></span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co">#> 1 1 3 5</span></span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co">#> 2 2 4 6</span></span></code></pre></div>
<p>Like <code>group_by()</code>, <code>rowwise()</code> doesn’t really
do anything itself; it just changes how the other verbs work. For
example, compare the results of <code>mutate()</code> in the following
code:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">m =</span> <span class="fu">mean</span>(<span class="fu">c</span>(x, y, z)))</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 4</span></span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#> x y z m</span></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#> <int> <int> <int> <dbl></span></span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#> 1 1 3 5 3.5</span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#> 2 2 4 6 3.5</span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">rowwise</span>() <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">m =</span> <span class="fu">mean</span>(<span class="fu">c</span>(x, y, z)))</span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 4</span></span>
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co">#> # Rowwise: </span></span>
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co">#> x y z m</span></span>
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="co">#> <int> <int> <int> <dbl></span></span>
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a><span class="co">#> 1 1 3 5 3</span></span>
<span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a><span class="co">#> 2 2 4 6 4</span></span></code></pre></div>
<p>If you use <code>mutate()</code> with a regular data frame, it
computes the mean of <code>x</code>, <code>y</code>, and <code>z</code>
across all rows. If you apply it to a row-wise data frame, it computes
the mean for each row.</p>
<p>You can optionally supply “identifier” variables in your call to
<code>rowwise()</code>. These variables are preserved when you call
<code>summarise()</code>, so they behave somewhat similarly to the
grouping variables passed to <code>group_by()</code>:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="at">name =</span> <span class="fu">c</span>(<span class="st">"Mara"</span>, <span class="st">"Hadley"</span>), <span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="at">y =</span> <span class="dv">3</span><span class="sc">:</span><span class="dv">4</span>, <span class="at">z =</span> <span class="dv">5</span><span class="sc">:</span><span class="dv">6</span>)</span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a></span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a> <span class="fu">rowwise</span>() <span class="sc">%>%</span> </span>
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">m =</span> <span class="fu">mean</span>(<span class="fu">c</span>(x, y, z)))</span>
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 1</span></span>
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#> m</span></span>
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#> <dbl></span></span>
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#> 1 3</span></span>
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#> 2 4</span></span>
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a></span>
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a> <span class="fu">rowwise</span>(name) <span class="sc">%>%</span> </span>
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">m =</span> <span class="fu">mean</span>(<span class="fu">c</span>(x, y, z)))</span>
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="co">#> `summarise()` has grouped output by 'name'. You can override using the</span></span>
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a><span class="co">#> `.groups` argument.</span></span>
<span id="cb4-17"><a href="#cb4-17" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 2</span></span>
<span id="cb4-18"><a href="#cb4-18" tabindex="-1"></a><span class="co">#> # Groups: name [2]</span></span>
<span id="cb4-19"><a href="#cb4-19" tabindex="-1"></a><span class="co">#> name m</span></span>
<span id="cb4-20"><a href="#cb4-20" tabindex="-1"></a><span class="co">#> <chr> <dbl></span></span>
<span id="cb4-21"><a href="#cb4-21" tabindex="-1"></a><span class="co">#> 1 Mara 3</span></span>
<span id="cb4-22"><a href="#cb4-22" tabindex="-1"></a><span class="co">#> 2 Hadley 4</span></span></code></pre></div>
<p><code>rowwise()</code> is just a special form of grouping, so if you
want to remove it from a data frame, just call
<code>ungroup()</code>.</p>
</div>
<div id="per-row-summary-statistics" class="section level2">
<h2>Per row summary statistics</h2>
<p><code>dplyr::summarise()</code> makes it really easy to summarise
values across rows within one column. When combined with
<code>rowwise()</code> it also makes it easy to summarise values across
columns within one row. To see how, we’ll start by making a little
dataset:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="at">id =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>, <span class="at">w =</span> <span class="dv">10</span><span class="sc">:</span><span class="dv">15</span>, <span class="at">x =</span> <span class="dv">20</span><span class="sc">:</span><span class="dv">25</span>, <span class="at">y =</span> <span class="dv">30</span><span class="sc">:</span><span class="dv">35</span>, <span class="at">z =</span> <span class="dv">40</span><span class="sc">:</span><span class="dv">45</span>)</span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a>df</span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 5</span></span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="co">#> id w x y z</span></span>
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="co">#> <int> <int> <int> <int> <int></span></span>
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="co">#> 1 1 10 20 30 40</span></span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#> 2 2 11 21 31 41</span></span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#> 3 3 12 22 32 42</span></span>
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#> 4 4 13 23 33 43</span></span>
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span></code></pre></div>
<p>Let’s say we want compute the sum of <code>w</code>, <code>x</code>,
<code>y</code>, and <code>z</code> for each row. We start by making a
row-wise data frame:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>rf <span class="ot"><-</span> df <span class="sc">%>%</span> <span class="fu">rowwise</span>(id)</span></code></pre></div>
<p>We can then use <code>mutate()</code> to add a new column to each
row, or <code>summarise()</code> to return just that one summary:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>rf <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">total =</span> <span class="fu">sum</span>(<span class="fu">c</span>(w, x, y, z)))</span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 6</span></span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co">#> # Rowwise: id</span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co">#> id w x y z total</span></span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#> <int> <int> <int> <int> <int> <int></span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#> 1 1 10 20 30 40 100</span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#> 2 2 11 21 31 41 104</span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co">#> 3 3 12 22 32 42 108</span></span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co">#> 4 4 13 23 33 43 112</span></span>
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span>
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a>rf <span class="sc">%>%</span> <span class="fu">summarise</span>(<span class="at">total =</span> <span class="fu">sum</span>(<span class="fu">c</span>(w, x, y, z)))</span>
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a><span class="co">#> `summarise()` has grouped output by 'id'. You can override using the `.groups`</span></span>
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a><span class="co">#> argument.</span></span>
<span id="cb7-14"><a href="#cb7-14" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 2</span></span>
<span id="cb7-15"><a href="#cb7-15" tabindex="-1"></a><span class="co">#> # Groups: id [6]</span></span>
<span id="cb7-16"><a href="#cb7-16" tabindex="-1"></a><span class="co">#> id total</span></span>
<span id="cb7-17"><a href="#cb7-17" tabindex="-1"></a><span class="co">#> <int> <int></span></span>
<span id="cb7-18"><a href="#cb7-18" tabindex="-1"></a><span class="co">#> 1 1 100</span></span>
<span id="cb7-19"><a href="#cb7-19" tabindex="-1"></a><span class="co">#> 2 2 104</span></span>
<span id="cb7-20"><a href="#cb7-20" tabindex="-1"></a><span class="co">#> 3 3 108</span></span>
<span id="cb7-21"><a href="#cb7-21" tabindex="-1"></a><span class="co">#> 4 4 112</span></span>
<span id="cb7-22"><a href="#cb7-22" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span></code></pre></div>
<p>Of course, if you have a lot of variables, it’s going to be tedious
to type in every variable name. Instead, you can use
<code>c_across()</code> which uses tidy selection syntax so you can to
succinctly select many variables:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>rf <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">total =</span> <span class="fu">sum</span>(<span class="fu">c_across</span>(w<span class="sc">:</span>z)))</span>
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 6</span></span>
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="co">#> # Rowwise: id</span></span>
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="co">#> id w x y z total</span></span>
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co">#> <int> <int> <int> <int> <int> <int></span></span>
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="co">#> 1 1 10 20 30 40 100</span></span>
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co">#> 2 2 11 21 31 41 104</span></span>
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="co">#> 3 3 12 22 32 42 108</span></span>
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a><span class="co">#> 4 4 13 23 33 43 112</span></span>
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span>
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a>rf <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">total =</span> <span class="fu">sum</span>(<span class="fu">c_across</span>(<span class="fu">where</span>(is.numeric))))</span>
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 6</span></span>
<span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a><span class="co">#> # Rowwise: id</span></span>
<span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a><span class="co">#> id w x y z total</span></span>
<span id="cb8-15"><a href="#cb8-15" tabindex="-1"></a><span class="co">#> <int> <int> <int> <int> <int> <int></span></span>
<span id="cb8-16"><a href="#cb8-16" tabindex="-1"></a><span class="co">#> 1 1 10 20 30 40 100</span></span>
<span id="cb8-17"><a href="#cb8-17" tabindex="-1"></a><span class="co">#> 2 2 11 21 31 41 104</span></span>
<span id="cb8-18"><a href="#cb8-18" tabindex="-1"></a><span class="co">#> 3 3 12 22 32 42 108</span></span>
<span id="cb8-19"><a href="#cb8-19" tabindex="-1"></a><span class="co">#> 4 4 13 23 33 43 112</span></span>
<span id="cb8-20"><a href="#cb8-20" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span></code></pre></div>
<p>You could combine this with column-wise operations (see
<code>vignette("colwise")</code> for more details) to compute the
proportion of the total for each column:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>rf <span class="sc">%>%</span> </span>
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">total =</span> <span class="fu">sum</span>(<span class="fu">c_across</span>(w<span class="sc">:</span>z))) <span class="sc">%>%</span> </span>
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> <span class="fu">ungroup</span>() <span class="sc">%>%</span> </span>
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(w<span class="sc">:</span>z, <span class="sc">~</span> . <span class="sc">/</span> total))</span>
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 6</span></span>
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co">#> id w x y z total</span></span>
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="co">#> <int> <dbl> <dbl> <dbl> <dbl> <int></span></span>
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#> 1 1 0.1 0.2 0.3 0.4 100</span></span>
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co">#> 2 2 0.106 0.202 0.298 0.394 104</span></span>
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a><span class="co">#> 3 3 0.111 0.204 0.296 0.389 108</span></span>
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a><span class="co">#> 4 4 0.116 0.205 0.295 0.384 112</span></span>
<span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span></code></pre></div>
<div id="row-wise-summary-functions" class="section level3">
<h3>Row-wise summary functions</h3>
<p>The <code>rowwise()</code> approach will work for any summary
function. But if you need greater speed, it’s worth looking for a
built-in row-wise variant of your summary function. These are more
efficient because they operate on the data frame as whole; they don’t
split it into rows, compute the summary, and then join the results back
together again.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">total =</span> <span class="fu">rowSums</span>(<span class="fu">pick</span>(<span class="fu">where</span>(is.numeric), <span class="sc">-</span>id)))</span>
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 6</span></span>
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="co">#> id w x y z total</span></span>
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="co">#> <int> <int> <int> <int> <int> <dbl></span></span>
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#> 1 1 10 20 30 40 100</span></span>
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co">#> 2 2 11 21 31 41 104</span></span>
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#> 3 3 12 22 32 42 108</span></span>
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co">#> 4 4 13 23 33 43 112</span></span>
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span>
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">mean =</span> <span class="fu">rowMeans</span>(<span class="fu">pick</span>(<span class="fu">where</span>(is.numeric), <span class="sc">-</span>id)))</span>
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 6</span></span>
<span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a><span class="co">#> id w x y z mean</span></span>
<span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a><span class="co">#> <int> <int> <int> <int> <int> <dbl></span></span>
<span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a><span class="co">#> 1 1 10 20 30 40 25</span></span>
<span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a><span class="co">#> 2 2 11 21 31 41 26</span></span>
<span id="cb10-16"><a href="#cb10-16" tabindex="-1"></a><span class="co">#> 3 3 12 22 32 42 27</span></span>
<span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a><span class="co">#> 4 4 13 23 33 43 28</span></span>
<span id="cb10-18"><a href="#cb10-18" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span></code></pre></div>
<p><strong>NB</strong>: I use <code>df</code> (not <code>rf</code>) and
<code>pick()</code> (not <code>c_across()</code>) here because
<code>rowMeans()</code> and <code>rowSums()</code> take a multi-row data
frame as input. Also note that <code>-id</code> is needed to avoid
selecting <code>id</code> in <code>pick()</code>. This wasn’t required
with the rowwise data frame because we had specified <code>id</code> as
an identifier in our original call to <code>rowwise()</code>, preventing
it from being selected as a grouping column.</p>
</div>
</div>
<div id="list-columns" class="section level2">
<h2>List-columns</h2>
<p><code>rowwise()</code> operations are a natural pairing when you have
list-columns. They allow you to avoid explicit loops and/or functions
from the <code>apply()</code> or <code>purrr::map()</code> families.</p>
<div id="motivation" class="section level3">
<h3>Motivation</h3>
<p>Imagine you have this data frame, and you want to count the lengths
of each element:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">tibble</span>(</span>
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a> <span class="at">x =</span> <span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>, <span class="dv">4</span><span class="sc">:</span><span class="dv">6</span>)</span>
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a>)</span></code></pre></div>
<p>You might try calling <code>length()</code>:</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">l =</span> <span class="fu">length</span>(x))</span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co">#> x l</span></span>
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="co">#> <list> <int></span></span>
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="co">#> 1 <dbl [1]> 3</span></span>
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#> 2 <int [2]> 3</span></span>
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#> 3 <int [3]> 3</span></span></code></pre></div>
<p>But that returns the length of the column, not the length of the
individual values. If you’re an R documentation aficionado, you might
know there’s already a base R function just for this purpose:</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">l =</span> <span class="fu">lengths</span>(x))</span>
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="co">#> x l</span></span>
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="co">#> <list> <int></span></span>
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#> 1 <dbl [1]> 1</span></span>
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co">#> 2 <int [2]> 2</span></span>
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a><span class="co">#> 3 <int [3]> 3</span></span></code></pre></div>
<p>Or if you’re an experienced R programmer, you might know how to apply
a function to each element of a list using <code>sapply()</code>,
<code>vapply()</code>, or one of the purrr <code>map()</code>
functions:</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">l =</span> <span class="fu">sapply</span>(x, length))</span>
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="co">#> x l</span></span>
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#> <list> <int></span></span>
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#> 1 <dbl [1]> 1</span></span>
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="co">#> 2 <int [2]> 2</span></span>
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#> 3 <int [3]> 3</span></span>
<span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a>df <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">l =</span> purrr<span class="sc">::</span><span class="fu">map_int</span>(x, length))</span>
<span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="co">#> x l</span></span>
<span id="cb14-11"><a href="#cb14-11" tabindex="-1"></a><span class="co">#> <list> <int></span></span>
<span id="cb14-12"><a href="#cb14-12" tabindex="-1"></a><span class="co">#> 1 <dbl [1]> 1</span></span>
<span id="cb14-13"><a href="#cb14-13" tabindex="-1"></a><span class="co">#> 2 <int [2]> 2</span></span>
<span id="cb14-14"><a href="#cb14-14" tabindex="-1"></a><span class="co">#> 3 <int [3]> 3</span></span></code></pre></div>
<p>But wouldn’t it be nice if you could just write
<code>length(x)</code> and dplyr would figure out that you wanted to
compute the length of the element inside of <code>x</code>? Since you’re
here, you might already be guessing at the answer: this is just another
application of the row-wise pattern.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> <span class="fu">rowwise</span>() <span class="sc">%>%</span> </span>
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">l =</span> <span class="fu">length</span>(x))</span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a><span class="co">#> # Rowwise: </span></span>
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="co">#> x l</span></span>
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a><span class="co">#> <list> <int></span></span>
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a><span class="co">#> 1 <dbl [1]> 1</span></span>
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a><span class="co">#> 2 <int [2]> 2</span></span>
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a><span class="co">#> 3 <int [3]> 3</span></span></code></pre></div>
</div>
<div id="subsetting" class="section level3">
<h3>Subsetting</h3>
<p>Before we continue on, I wanted to briefly mention the magic that
makes this work. This isn’t something you’ll generally need to think
about (it’ll just work), but it’s useful to know about when something
goes wrong.</p>
<p>There’s an important difference between a grouped data frame where
each group happens to have one row, and a row-wise data frame where
every group always has one row. Take these two data frames:</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="at">g =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="at">y =</span> <span class="fu">list</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"a"</span>))</span>
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a>gf <span class="ot"><-</span> df <span class="sc">%>%</span> <span class="fu">group_by</span>(g)</span>
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a>rf <span class="ot"><-</span> df <span class="sc">%>%</span> <span class="fu">rowwise</span>(g)</span></code></pre></div>
<p>If we compute some properties of <code>y</code>, you’ll notice the
results look different:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a>gf <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">type =</span> <span class="fu">typeof</span>(y), <span class="at">length =</span> <span class="fu">length</span>(y))</span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 4</span></span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a><span class="co">#> # Groups: g [2]</span></span>
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a><span class="co">#> g y type length</span></span>
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="co">#> <int> <list> <chr> <int></span></span>
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co">#> 1 1 <int [3]> list 1</span></span>
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a><span class="co">#> 2 2 <chr [1]> list 1</span></span>
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a>rf <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">type =</span> <span class="fu">typeof</span>(y), <span class="at">length =</span> <span class="fu">length</span>(y))</span>
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 4</span></span>
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a><span class="co">#> # Rowwise: g</span></span>
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="co">#> g y type length</span></span>
<span id="cb17-12"><a href="#cb17-12" tabindex="-1"></a><span class="co">#> <int> <list> <chr> <int></span></span>
<span id="cb17-13"><a href="#cb17-13" tabindex="-1"></a><span class="co">#> 1 1 <int [3]> integer 3</span></span>
<span id="cb17-14"><a href="#cb17-14" tabindex="-1"></a><span class="co">#> 2 2 <chr [1]> character 1</span></span></code></pre></div>
<p>They key difference is that when <code>mutate()</code> slices up the
columns to pass to <code>length(y)</code> the grouped mutate uses
<code>[</code> and the row-wise mutate uses <code>[[</code>. The
following code gives a flavour of the differences if you used a for
loop:</p>
<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"># grouped</span></span>
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a>out1 <span class="ot"><-</span> <span class="fu">integer</span>(<span class="dv">2</span>)</span>
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>) {</span>
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a> out1[[i]] <span class="ot"><-</span> <span class="fu">length</span>(df<span class="sc">$</span>y[i])</span>
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a>}</span>
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a>out1</span>
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="co">#> [1] 1 1</span></span>
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a></span>
<span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a><span class="co"># rowwise</span></span>
<span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a>out2 <span class="ot"><-</span> <span class="fu">integer</span>(<span class="dv">2</span>)</span>
<span id="cb18-11"><a href="#cb18-11" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>) {</span>
<span id="cb18-12"><a href="#cb18-12" tabindex="-1"></a> out2[[i]] <span class="ot"><-</span> <span class="fu">length</span>(df<span class="sc">$</span>y[[i]])</span>
<span id="cb18-13"><a href="#cb18-13" tabindex="-1"></a>}</span>
<span id="cb18-14"><a href="#cb18-14" tabindex="-1"></a>out2</span>
<span id="cb18-15"><a href="#cb18-15" tabindex="-1"></a><span class="co">#> [1] 3 1</span></span></code></pre></div>
<p>Note that this magic only applies when you’re referring to existing
columns, not when you’re creating new rows. This is potentially
confusing, but we’re fairly confident it’s the least worst solution,
particularly given the hint in the error message.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>gf <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">y2 =</span> y)</span>
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 3</span></span>
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a><span class="co">#> # Groups: g [2]</span></span>
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="co">#> g y y2 </span></span>
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="co">#> <int> <list> <list> </span></span>
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a><span class="co">#> 1 1 <int [3]> <int [3]></span></span>
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a><span class="co">#> 2 2 <chr [1]> <chr [1]></span></span>
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a>rf <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">y2 =</span> y)</span>
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a><span class="co">#> Error in `mutate()`:</span></span>
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a><span class="co">#> ℹ In argument: `y2 = y`.</span></span>
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a><span class="co">#> ℹ In row 1.</span></span>
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a><span class="co">#> Caused by error:</span></span>
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a><span class="co">#> ! `y2` must be size 1, not 3.</span></span>
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a><span class="co">#> ℹ Did you mean: `y2 = list(y)` ?</span></span>
<span id="cb19-15"><a href="#cb19-15" tabindex="-1"></a>rf <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">y2 =</span> <span class="fu">list</span>(y))</span>
<span id="cb19-16"><a href="#cb19-16" tabindex="-1"></a><span class="co">#> # A tibble: 2 × 3</span></span>
<span id="cb19-17"><a href="#cb19-17" tabindex="-1"></a><span class="co">#> # Rowwise: g</span></span>
<span id="cb19-18"><a href="#cb19-18" tabindex="-1"></a><span class="co">#> g y y2 </span></span>
<span id="cb19-19"><a href="#cb19-19" tabindex="-1"></a><span class="co">#> <int> <list> <list> </span></span>
<span id="cb19-20"><a href="#cb19-20" tabindex="-1"></a><span class="co">#> 1 1 <int [3]> <int [3]></span></span>
<span id="cb19-21"><a href="#cb19-21" tabindex="-1"></a><span class="co">#> 2 2 <chr [1]> <chr [1]></span></span></code></pre></div>
</div>
<div id="modelling" class="section level3">
<h3>Modelling</h3>
<p><code>rowwise()</code> data frames allow you to solve a variety of
modelling problems in what I think is a particularly elegant way. We’ll
start by creating a nested data frame:</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>by_cyl <span class="ot"><-</span> mtcars <span class="sc">%>%</span> <span class="fu">nest_by</span>(cyl)</span>
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a>by_cyl</span>
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a><span class="co">#> # Rowwise: cyl</span></span>
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a><span class="co">#> cyl data </span></span>
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a><span class="co">#> <dbl> <list> </span></span>
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a><span class="co">#> 1 4 <tibble [11 × 12]></span></span>
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a><span class="co">#> 2 6 <tibble [7 × 12]> </span></span>
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a><span class="co">#> 3 8 <tibble [14 × 12]></span></span></code></pre></div>
<p>This is a little different to the usual <code>group_by()</code>
output: we have visibly changed the structure of the data. Now we have
three rows (one for each group), and we have a list-col,
<code>data</code>, that stores the data for that group. Also note that
the output is <code>rowwise()</code>; this is important because it’s
going to make working with that list of data frames much easier.</p>
<p>Once we have one data frame per row, it’s straightforward to make one
model per row:</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>mods <span class="ot"><-</span> by_cyl <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">mod =</span> <span class="fu">list</span>(<span class="fu">lm</span>(mpg <span class="sc">~</span> wt, <span class="at">data =</span> data)))</span>
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a>mods</span>
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 3</span></span>
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a><span class="co">#> # Rowwise: cyl</span></span>
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a><span class="co">#> cyl data mod </span></span>
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a><span class="co">#> <dbl> <list> <list></span></span>
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="co">#> 1 4 <tibble [11 × 12]> <lm> </span></span>
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="co">#> 2 6 <tibble [7 × 12]> <lm> </span></span>
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="co">#> 3 8 <tibble [14 × 12]> <lm></span></span></code></pre></div>
<p>And supplement that with one set of predictions per row:</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a>mods <span class="ot"><-</span> mods <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">pred =</span> <span class="fu">list</span>(<span class="fu">predict</span>(mod, data)))</span>
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a>mods</span>
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 4</span></span>
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a><span class="co">#> # Rowwise: cyl</span></span>
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="co">#> cyl data mod pred </span></span>
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a><span class="co">#> <dbl> <list> <list> <list> </span></span>
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a><span class="co">#> 1 4 <tibble [11 × 12]> <lm> <dbl [11]></span></span>
<span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a><span class="co">#> 2 6 <tibble [7 × 12]> <lm> <dbl [7]> </span></span>
<span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a><span class="co">#> 3 8 <tibble [14 × 12]> <lm> <dbl [14]></span></span></code></pre></div>
<p>You could then summarise the model in a variety of ways:</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a>mods <span class="sc">%>%</span> <span class="fu">summarise</span>(<span class="at">rmse =</span> <span class="fu">sqrt</span>(<span class="fu">mean</span>((pred <span class="sc">-</span> data<span class="sc">$</span>mpg) <span class="sc">^</span> <span class="dv">2</span>)))</span>
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="co">#> `summarise()` has grouped output by 'cyl'. You can override using the `.groups`</span></span>
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a><span class="co">#> argument.</span></span>
<span id="cb23-4"><a href="#cb23-4" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a><span class="co">#> # Groups: cyl [3]</span></span>
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="co">#> cyl rmse</span></span>
<span id="cb23-7"><a href="#cb23-7" tabindex="-1"></a><span class="co">#> <dbl> <dbl></span></span>
<span id="cb23-8"><a href="#cb23-8" tabindex="-1"></a><span class="co">#> 1 4 3.01 </span></span>
<span id="cb23-9"><a href="#cb23-9" tabindex="-1"></a><span class="co">#> 2 6 0.985</span></span>
<span id="cb23-10"><a href="#cb23-10" tabindex="-1"></a><span class="co">#> 3 8 1.87</span></span>
<span id="cb23-11"><a href="#cb23-11" tabindex="-1"></a>mods <span class="sc">%>%</span> <span class="fu">summarise</span>(<span class="at">rsq =</span> <span class="fu">summary</span>(mod)<span class="sc">$</span>r.squared)</span>
<span id="cb23-12"><a href="#cb23-12" tabindex="-1"></a><span class="co">#> `summarise()` has grouped output by 'cyl'. You can override using the `.groups`</span></span>
<span id="cb23-13"><a href="#cb23-13" tabindex="-1"></a><span class="co">#> argument.</span></span>
<span id="cb23-14"><a href="#cb23-14" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb23-15"><a href="#cb23-15" tabindex="-1"></a><span class="co">#> # Groups: cyl [3]</span></span>
<span id="cb23-16"><a href="#cb23-16" tabindex="-1"></a><span class="co">#> cyl rsq</span></span>
<span id="cb23-17"><a href="#cb23-17" tabindex="-1"></a><span class="co">#> <dbl> <dbl></span></span>
<span id="cb23-18"><a href="#cb23-18" tabindex="-1"></a><span class="co">#> 1 4 0.509</span></span>
<span id="cb23-19"><a href="#cb23-19" tabindex="-1"></a><span class="co">#> 2 6 0.465</span></span>
<span id="cb23-20"><a href="#cb23-20" tabindex="-1"></a><span class="co">#> 3 8 0.423</span></span>
<span id="cb23-21"><a href="#cb23-21" tabindex="-1"></a>mods <span class="sc">%>%</span> <span class="fu">summarise</span>(broom<span class="sc">::</span><span class="fu">glance</span>(mod))</span>
<span id="cb23-22"><a href="#cb23-22" tabindex="-1"></a><span class="co">#> `summarise()` has grouped output by 'cyl'. You can override using the `.groups`</span></span>
<span id="cb23-23"><a href="#cb23-23" tabindex="-1"></a><span class="co">#> argument.</span></span>
<span id="cb23-24"><a href="#cb23-24" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 13</span></span>
<span id="cb23-25"><a href="#cb23-25" tabindex="-1"></a><span class="co">#> # Groups: cyl [3]</span></span>
<span id="cb23-26"><a href="#cb23-26" tabindex="-1"></a><span class="co">#> cyl r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC</span></span>
<span id="cb23-27"><a href="#cb23-27" tabindex="-1"></a><span class="co">#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span>
<span id="cb23-28"><a href="#cb23-28" tabindex="-1"></a><span class="co">#> 1 4 0.509 0.454 3.33 9.32 0.0137 1 -27.7 61.5 62.7</span></span>
<span id="cb23-29"><a href="#cb23-29" tabindex="-1"></a><span class="co">#> 2 6 0.465 0.357 1.17 4.34 0.0918 1 -9.83 25.7 25.5</span></span>
<span id="cb23-30"><a href="#cb23-30" tabindex="-1"></a><span class="co">#> 3 8 0.423 0.375 2.02 8.80 0.0118 1 -28.7 63.3 65.2</span></span>
<span id="cb23-31"><a href="#cb23-31" tabindex="-1"></a><span class="co">#> # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int></span></span></code></pre></div>
<p>Or easily access the parameters of each model:</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a>mods <span class="sc">%>%</span> <span class="fu">reframe</span>(broom<span class="sc">::</span><span class="fu">tidy</span>(mod))</span>
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 6</span></span>
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a><span class="co">#> cyl term estimate std.error statistic p.value</span></span>
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a><span class="co">#> <dbl> <chr> <dbl> <dbl> <dbl> <dbl></span></span>
<span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a><span class="co">#> 1 4 (Intercept) 39.6 4.35 9.10 0.00000777</span></span>
<span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a><span class="co">#> 2 4 wt -5.65 1.85 -3.05 0.0137 </span></span>
<span id="cb24-7"><a href="#cb24-7" tabindex="-1"></a><span class="co">#> 3 6 (Intercept) 28.4 4.18 6.79 0.00105 </span></span>
<span id="cb24-8"><a href="#cb24-8" tabindex="-1"></a><span class="co">#> 4 6 wt -2.78 1.33 -2.08 0.0918 </span></span>
<span id="cb24-9"><a href="#cb24-9" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span></code></pre></div>
</div>
</div>
<div id="repeated-function-calls" class="section level2">
<h2>Repeated function calls</h2>
<p><code>rowwise()</code> doesn’t just work with functions that return a
length-1 vector (aka summary functions); it can work with any function
if the result is a list. This means that <code>rowwise()</code> and
<code>mutate()</code> provide an elegant way to call a function many
times with varying arguments, storing the outputs alongside the
inputs.</p>
<div id="simulations" class="section level3">
<h3>Simulations</h3>
<p>I think this is a particularly elegant way to perform simulations,
because it lets you store simulated values along with the parameters
that generated them. For example, imagine you have the following data
frame that describes the properties of 3 samples from the uniform
distribution:</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">tribble</span>(</span>
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a> <span class="sc">~</span> n, <span class="sc">~</span> min, <span class="sc">~</span> max,</span>
<span id="cb25-3"><a href="#cb25-3" tabindex="-1"></a> <span class="dv">1</span>, <span class="dv">0</span>, <span class="dv">1</span>,</span>
<span id="cb25-4"><a href="#cb25-4" tabindex="-1"></a> <span class="dv">2</span>, <span class="dv">10</span>, <span class="dv">100</span>,</span>
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a> <span class="dv">3</span>, <span class="dv">100</span>, <span class="dv">1000</span>,</span>
<span id="cb25-6"><a href="#cb25-6" tabindex="-1"></a>)</span></code></pre></div>
<p>You can supply these parameters to <code>runif()</code> by using
<code>rowwise()</code> and <code>mutate()</code>:</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a> <span class="fu">rowwise</span>() <span class="sc">%>%</span> </span>
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">data =</span> <span class="fu">list</span>(<span class="fu">runif</span>(n, min, max)))</span>
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 4</span></span>
<span id="cb26-5"><a href="#cb26-5" tabindex="-1"></a><span class="co">#> # Rowwise: </span></span>
<span id="cb26-6"><a href="#cb26-6" tabindex="-1"></a><span class="co">#> n min max data </span></span>
<span id="cb26-7"><a href="#cb26-7" tabindex="-1"></a><span class="co">#> <dbl> <dbl> <dbl> <list> </span></span>
<span id="cb26-8"><a href="#cb26-8" tabindex="-1"></a><span class="co">#> 1 1 0 1 <dbl [1]></span></span>
<span id="cb26-9"><a href="#cb26-9" tabindex="-1"></a><span class="co">#> 2 2 10 100 <dbl [2]></span></span>
<span id="cb26-10"><a href="#cb26-10" tabindex="-1"></a><span class="co">#> 3 3 100 1000 <dbl [3]></span></span></code></pre></div>
<p>Note the use of <code>list()</code> here - <code>runif()</code>
returns multiple values and a <code>mutate()</code> expression has to
return something of length 1. <code>list()</code> means that we’ll get a
list column where each row is a list containing multiple values. If you
forget to use <code>list()</code>, dplyr will give you a hint:</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
<span id="cb27-2"><a href="#cb27-2" tabindex="-1"></a> <span class="fu">rowwise</span>() <span class="sc">%>%</span> </span>
<span id="cb27-3"><a href="#cb27-3" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">data =</span> <span class="fu">runif</span>(n, min, max))</span>
<span id="cb27-4"><a href="#cb27-4" tabindex="-1"></a><span class="co">#> Error in `mutate()`:</span></span>
<span id="cb27-5"><a href="#cb27-5" tabindex="-1"></a><span class="co">#> ℹ In argument: `data = runif(n, min, max)`.</span></span>
<span id="cb27-6"><a href="#cb27-6" tabindex="-1"></a><span class="co">#> ℹ In row 2.</span></span>
<span id="cb27-7"><a href="#cb27-7" tabindex="-1"></a><span class="co">#> Caused by error:</span></span>
<span id="cb27-8"><a href="#cb27-8" tabindex="-1"></a><span class="co">#> ! `data` must be size 1, not 2.</span></span>
<span id="cb27-9"><a href="#cb27-9" tabindex="-1"></a><span class="co">#> ℹ Did you mean: `data = list(runif(n, min, max))` ?</span></span></code></pre></div>
</div>
<div id="multiple-combinations" class="section level3">
<h3>Multiple combinations</h3>
<p>What if you want to call a function for every combination of inputs?
You can use <code>expand.grid()</code> (or
<code>tidyr::expand_grid()</code>) to generate the data frame and then
repeat the same pattern as above:</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">expand.grid</span>(<span class="at">mean =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>, <span class="dv">0</span>, <span class="dv">1</span>), <span class="at">sd =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">100</span>))</span>
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a></span>
<span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
<span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a> <span class="fu">rowwise</span>() <span class="sc">%>%</span> </span>
<span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">data =</span> <span class="fu">list</span>(<span class="fu">rnorm</span>(<span class="dv">10</span>, mean, sd)))</span>
<span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a><span class="co">#> # A tibble: 9 × 3</span></span>
<span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a><span class="co">#> # Rowwise: </span></span>
<span id="cb28-8"><a href="#cb28-8" tabindex="-1"></a><span class="co">#> mean sd data </span></span>
<span id="cb28-9"><a href="#cb28-9" tabindex="-1"></a><span class="co">#> <dbl> <dbl> <list> </span></span>
<span id="cb28-10"><a href="#cb28-10" tabindex="-1"></a><span class="co">#> 1 -1 1 <dbl [10]></span></span>
<span id="cb28-11"><a href="#cb28-11" tabindex="-1"></a><span class="co">#> 2 0 1 <dbl [10]></span></span>
<span id="cb28-12"><a href="#cb28-12" tabindex="-1"></a><span class="co">#> 3 1 1 <dbl [10]></span></span>
<span id="cb28-13"><a href="#cb28-13" tabindex="-1"></a><span class="co">#> 4 -1 10 <dbl [10]></span></span>
<span id="cb28-14"><a href="#cb28-14" tabindex="-1"></a><span class="co">#> # ℹ 5 more rows</span></span></code></pre></div>
</div>
<div id="varying-functions" class="section level3">
<h3>Varying functions</h3>
<p>In more complicated problems, you might also want to vary the
function being called. This tends to be a bit more of an awkward fit
with this approach because the columns in the input tibble will be less
regular. But it’s still possible, and it’s a natural place to use
<code>do.call()</code>:</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">tribble</span>(</span>
<span id="cb29-2"><a href="#cb29-2" tabindex="-1"></a> <span class="sc">~</span>rng, <span class="sc">~</span>params,</span>
<span id="cb29-3"><a href="#cb29-3" tabindex="-1"></a> <span class="st">"runif"</span>, <span class="fu">list</span>(<span class="at">n =</span> <span class="dv">10</span>), </span>
<span id="cb29-4"><a href="#cb29-4" tabindex="-1"></a> <span class="st">"rnorm"</span>, <span class="fu">list</span>(<span class="at">n =</span> <span class="dv">20</span>),</span>
<span id="cb29-5"><a href="#cb29-5" tabindex="-1"></a> <span class="st">"rpois"</span>, <span class="fu">list</span>(<span class="at">n =</span> <span class="dv">10</span>, <span class="at">lambda =</span> <span class="dv">5</span>),</span>
<span id="cb29-6"><a href="#cb29-6" tabindex="-1"></a>) <span class="sc">%>%</span></span>
<span id="cb29-7"><a href="#cb29-7" tabindex="-1"></a> <span class="fu">rowwise</span>()</span>
<span id="cb29-8"><a href="#cb29-8" tabindex="-1"></a></span>
<span id="cb29-9"><a href="#cb29-9" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
<span id="cb29-10"><a href="#cb29-10" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">data =</span> <span class="fu">list</span>(<span class="fu">do.call</span>(rng, params)))</span>
<span id="cb29-11"><a href="#cb29-11" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 3</span></span>
<span id="cb29-12"><a href="#cb29-12" tabindex="-1"></a><span class="co">#> # Rowwise: </span></span>
<span id="cb29-13"><a href="#cb29-13" tabindex="-1"></a><span class="co">#> rng params data </span></span>
<span id="cb29-14"><a href="#cb29-14" tabindex="-1"></a><span class="co">#> <chr> <list> <list> </span></span>
<span id="cb29-15"><a href="#cb29-15" tabindex="-1"></a><span class="co">#> 1 runif <named list [1]> <dbl [10]></span></span>
<span id="cb29-16"><a href="#cb29-16" tabindex="-1"></a><span class="co">#> 2 rnorm <named list [1]> <dbl [20]></span></span>
<span id="cb29-17"><a href="#cb29-17" tabindex="-1"></a><span class="co">#> 3 rpois <named list [2]> <int [10]></span></span></code></pre></div>
</div>
</div>
<div id="previously" class="section level2">
<h2>Previously</h2>
<div id="rowwise" class="section level3">
<h3><code>rowwise()</code></h3>
<p><code>rowwise()</code> was also questioning for quite some time,
partly because I didn’t appreciate how many people needed the native
ability to compute summaries across multiple variables for each row. As
an alternative, we recommended performing row-wise operations with the
purrr <code>map()</code> functions. However, this was challenging
because you needed to pick a map function based on the number of
arguments that were varying and the type of result, which required quite
some knowledge of purrr functions.</p>
<p>I was also resistant to <code>rowwise()</code> because I felt like
automatically switching between <code>[</code> to <code>[[</code> was
too magical in the same way that automatically <code>list()</code>-ing
results made <code>do()</code> too magical. I’ve now persuaded myself
that the row-wise magic is good magic partly because most people find
the distinction between <code>[</code> and <code>[[</code> mystifying
and <code>rowwise()</code> means that you don’t need to think about
it.</p>
<p>Since <code>rowwise()</code> clearly is useful it is not longer
questioning, and we expect it to be around for the long term.</p>
</div>
<div id="do" class="section level3">
<h3><code>do()</code></h3>
<p>We’ve questioned the need for <code>do()</code> for quite some time,
because it never felt very similar to the other dplyr verbs. It had two
main modes of operation:</p>
<ul>
<li><p>Without argument names: you could call functions that input and
output data frames using <code>.</code> to refer to the “current” group.
For example, the following code gets the first row of each group:</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a>mtcars <span class="sc">%>%</span> </span>
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a> <span class="fu">group_by</span>(cyl) <span class="sc">%>%</span> </span>
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a> <span class="fu">do</span>(<span class="fu">head</span>(., <span class="dv">1</span>))</span>
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 13</span></span>
<span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a><span class="co">#> # Groups: cyl [3]</span></span>
<span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a><span class="co">#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2 cyl4</span></span>
<span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a><span class="co">#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span>
<span id="cb30-8"><a href="#cb30-8" tabindex="-1"></a><span class="co">#> 1 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 8 16</span></span>
<span id="cb30-9"><a href="#cb30-9" tabindex="-1"></a><span class="co">#> 2 21 6 160 110 3.9 2.62 16.5 0 1 4 4 12 24</span></span>
<span id="cb30-10"><a href="#cb30-10" tabindex="-1"></a><span class="co">#> 3 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 16 32</span></span></code></pre></div>
<p>This has been superseded by <code>pick()</code> plus
<code>reframe()</code>, a variant of <code>summarise()</code> that can
create multiple rows and columns per group.</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" tabindex="-1"></a>mtcars <span class="sc">%>%</span> </span>
<span id="cb31-2"><a href="#cb31-2" tabindex="-1"></a> <span class="fu">group_by</span>(cyl) <span class="sc">%>%</span> </span>
<span id="cb31-3"><a href="#cb31-3" tabindex="-1"></a> <span class="fu">reframe</span>(<span class="fu">head</span>(<span class="fu">pick</span>(<span class="fu">everything</span>()), <span class="dv">1</span>))</span>
<span id="cb31-4"><a href="#cb31-4" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 13</span></span>
<span id="cb31-5"><a href="#cb31-5" tabindex="-1"></a><span class="co">#> cyl mpg disp hp drat wt qsec vs am gear carb cyl2 cyl4</span></span>
<span id="cb31-6"><a href="#cb31-6" tabindex="-1"></a><span class="co">#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl></span></span>
<span id="cb31-7"><a href="#cb31-7" tabindex="-1"></a><span class="co">#> 1 4 22.8 108 93 3.85 2.32 18.6 1 1 4 1 8 16</span></span>
<span id="cb31-8"><a href="#cb31-8" tabindex="-1"></a><span class="co">#> 2 6 21 160 110 3.9 2.62 16.5 0 1 4 4 12 24</span></span>
<span id="cb31-9"><a href="#cb31-9" tabindex="-1"></a><span class="co">#> 3 8 18.7 360 175 3.15 3.44 17.0 0 0 3 2 16 32</span></span></code></pre></div></li>
<li><p>With arguments: it worked like <code>mutate()</code> but
automatically wrapped every element in a list:</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" tabindex="-1"></a>mtcars <span class="sc">%>%</span> </span>
<span id="cb32-2"><a href="#cb32-2" tabindex="-1"></a> <span class="fu">group_by</span>(cyl) <span class="sc">%>%</span> </span>
<span id="cb32-3"><a href="#cb32-3" tabindex="-1"></a> <span class="fu">do</span>(<span class="at">nrows =</span> <span class="fu">nrow</span>(.))</span>
<span id="cb32-4"><a href="#cb32-4" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb32-5"><a href="#cb32-5" tabindex="-1"></a><span class="co">#> # Rowwise: </span></span>
<span id="cb32-6"><a href="#cb32-6" tabindex="-1"></a><span class="co">#> cyl nrows </span></span>
<span id="cb32-7"><a href="#cb32-7" tabindex="-1"></a><span class="co">#> <dbl> <list> </span></span>
<span id="cb32-8"><a href="#cb32-8" tabindex="-1"></a><span class="co">#> 1 4 <int [1]></span></span>
<span id="cb32-9"><a href="#cb32-9" tabindex="-1"></a><span class="co">#> 2 6 <int [1]></span></span>
<span id="cb32-10"><a href="#cb32-10" tabindex="-1"></a><span class="co">#> 3 8 <int [1]></span></span></code></pre></div>
<p>I now believe that behaviour is both too magical and not very useful,
and it can be replaced by <code>summarise()</code> and
<code>pick()</code>.</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" tabindex="-1"></a>mtcars <span class="sc">%>%</span> </span>
<span id="cb33-2"><a href="#cb33-2" tabindex="-1"></a> <span class="fu">group_by</span>(cyl) <span class="sc">%>%</span> </span>
<span id="cb33-3"><a href="#cb33-3" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">nrows =</span> <span class="fu">nrow</span>(<span class="fu">pick</span>(<span class="fu">everything</span>())))</span>
<span id="cb33-4"><a href="#cb33-4" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 2</span></span>
<span id="cb33-5"><a href="#cb33-5" tabindex="-1"></a><span class="co">#> cyl nrows</span></span>
<span id="cb33-6"><a href="#cb33-6" tabindex="-1"></a><span class="co">#> <dbl> <int></span></span>
<span id="cb33-7"><a href="#cb33-7" tabindex="-1"></a><span class="co">#> 1 4 11</span></span>
<span id="cb33-8"><a href="#cb33-8" tabindex="-1"></a><span class="co">#> 2 6 7</span></span>
<span id="cb33-9"><a href="#cb33-9" tabindex="-1"></a><span class="co">#> 3 8 14</span></span></code></pre></div>
<p>If needed (unlike here), you can wrap the results in a list
yourself.</p></li>
</ul>
<p>The addition of <code>pick()</code>/<code>across()</code> and the
increased scope of <code>summarise()</code>/<code>reframe()</code> means
that <code>do()</code> is no longer needed, so it is now superseded.</p>
</div>
</div>
<!-- code folding -->
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>
|