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 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
|
<!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>Introduction to dplyr</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">Introduction to dplyr</h1>
<p>When working with data you must:</p>
<ul>
<li><p>Figure out what you want to do.</p></li>
<li><p>Describe those tasks in the form of a computer program.</p></li>
<li><p>Execute the program.</p></li>
</ul>
<p>The dplyr package makes these steps fast and easy:</p>
<ul>
<li><p>By constraining your options, it helps you think about your data
manipulation challenges.</p></li>
<li><p>It provides simple “verbs”, functions that correspond to the most
common data manipulation tasks, to help you translate your thoughts into
code.</p></li>
<li><p>It uses efficient backends, so you spend less time waiting for
the computer.</p></li>
</ul>
<p>This document introduces you to dplyr’s basic set of tools, and shows
you how to apply them to data frames. dplyr also supports databases via
the dbplyr package, once you’ve installed, read
<code>vignette("dbplyr")</code> to learn more.</p>
<div id="data-starwars" class="section level2">
<h2>Data: starwars</h2>
<p>To explore the basic data manipulation verbs of dplyr, we’ll use the
dataset <code>starwars</code>. This dataset contains 87 characters and
comes from the <a href="https://swapi.dev">Star Wars API</a>, and is
documented in <code>?starwars</code></p>
<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">dim</span>(starwars)</span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="co">#> [1] 87 14</span></span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a>starwars</span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 14</span></span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="co">#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…</span></span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span>
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="co">#> 4 Darth Va… 202 136 none white yellow 41.9 male mascu…</span></span>
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
<p>Note that <code>starwars</code> is a tibble, a modern reimagining of
the data frame. It’s particularly useful for large datasets because it
only prints the first few rows. You can learn more about tibbles at <a href="https://tibble.tidyverse.org" class="uri">https://tibble.tidyverse.org</a>; in particular you can
convert data frames to tibbles with <code>as_tibble()</code>.</p>
</div>
<div id="single-table-verbs" class="section level2">
<h2>Single table verbs</h2>
<p>dplyr aims to provide a function for each basic verb of data
manipulation. These verbs can be organised into three categories based
on the component of the dataset that they work with:</p>
<ul>
<li>Rows:
<ul>
<li><code>filter()</code> chooses rows based on column values.</li>
<li><code>slice()</code> chooses rows based on location.</li>
<li><code>arrange()</code> changes the order of the rows.</li>
</ul></li>
<li>Columns:
<ul>
<li><code>select()</code> changes whether or not a column is
included.</li>
<li><code>rename()</code> changes the name of columns.</li>
<li><code>mutate()</code> changes the values of columns and creates new
columns.</li>
<li><code>relocate()</code> changes the order of the columns.</li>
</ul></li>
<li>Groups of rows:
<ul>
<li><code>summarise()</code> collapses a group into a single row.</li>
</ul></li>
</ul>
<div id="the-pipe" class="section level3">
<h3>The pipe</h3>
<p>All of the dplyr functions take a data frame (or tibble) as the first
argument. Rather than forcing the user to either save intermediate
objects or nest functions, dplyr provides the <code>%>%</code>
operator from magrittr. <code>x %>% f(y)</code> turns into
<code>f(x, y)</code> so the result from one step is then “piped” into
the next step. You can use the pipe to rewrite multiple operations that
you can read left-to-right, top-to-bottom (reading the pipe operator as
“then”).</p>
</div>
<div id="filter-rows-with-filter" class="section level3">
<h3>Filter rows with <code>filter()</code></h3>
<p><code>filter()</code> allows you to select a subset of rows in a data
frame. Like all single verbs, the first argument is the tibble (or data
frame). The second and subsequent arguments refer to variables within
that data frame, selecting rows where the expression is
<code>TRUE</code>.</p>
<p>For example, we can select all character with light skin color and
brown eyes with:</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>starwars <span class="sc">%>%</span> <span class="fu">filter</span>(skin_color <span class="sc">==</span> <span class="st">"light"</span>, eye_color <span class="sc">==</span> <span class="st">"brown"</span>)</span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="co">#> # A tibble: 7 × 14</span></span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="co">#> 1 Leia Org… 150 49 brown light brown 19 fema… femin…</span></span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co">#> 2 Biggs Da… 183 84 black light brown 24 male mascu…</span></span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co">#> 3 Padmé Am… 185 45 brown light brown 46 fema… femin…</span></span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co">#> 4 Cordé 157 NA brown light brown NA <NA> <NA> </span></span>
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co">#> # ℹ 3 more rows</span></span>
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
<p>This is roughly equivalent to this base R 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>starwars[starwars<span class="sc">$</span>skin_color <span class="sc">==</span> <span class="st">"light"</span> <span class="sc">&</span> starwars<span class="sc">$</span>eye_color <span class="sc">==</span> <span class="st">"brown"</span>, ]</span></code></pre></div>
</div>
<div id="arrange-rows-with-arrange" class="section level3">
<h3>Arrange rows with <code>arrange()</code></h3>
<p><code>arrange()</code> works similarly to <code>filter()</code>
except that instead of filtering or selecting rows, it reorders them. It
takes a data frame, and a set of column names (or more complicated
expressions) to order by. If you provide more than one column name, each
additional column will be used to break ties in the values of preceding
columns:</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>starwars <span class="sc">%>%</span> <span class="fu">arrange</span>(height, mass)</span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 14</span></span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#> 1 Yoda 66 17 white green brown 896 male mascu…</span></span>
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#> 2 Ratts Ty… 79 15 none grey, blue unknown NA male mascu…</span></span>
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#> 3 Wicket S… 88 20 brown brown brown 8 male mascu…</span></span>
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#> 4 Dud Bolt 94 45 none blue, grey yellow NA male mascu…</span></span>
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
<p>Use <code>desc()</code> to order a column in descending order:</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>starwars <span class="sc">%>%</span> <span class="fu">arrange</span>(<span class="fu">desc</span>(height))</span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 14</span></span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="co">#> 1 Yarael P… 264 NA none white yellow NA male mascu…</span></span>
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="co">#> 2 Tarfful 234 136 brown brown blue NA male mascu…</span></span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#> 3 Lama Su 229 88 none grey black NA male mascu…</span></span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#> 4 Chewbacca 228 112 brown unknown blue 200 male mascu…</span></span>
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
</div>
<div id="choose-rows-using-their-position-with-slice" class="section level3">
<h3>Choose rows using their position with <code>slice()</code></h3>
<p><code>slice()</code> lets you index rows by their (integer)
locations. It allows you to select, remove, and duplicate rows.</p>
<p>We can get characters from row numbers 5 through 10.</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>starwars <span class="sc">%>%</span> <span class="fu">slice</span>(<span class="dv">5</span><span class="sc">:</span><span class="dv">10</span>)</span>
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 14</span></span>
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="co">#> 1 Leia Org… 150 49 brown light brown 19 fema… femin…</span></span>
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co">#> 2 Owen Lars 178 120 brown, gr… light blue 52 male mascu…</span></span>
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co">#> 3 Beru Whi… 165 75 brown light blue 47 fema… femin…</span></span>
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#> 4 R5-D4 97 32 <NA> white, red red NA none mascu…</span></span>
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="co">#> # ℹ 2 more rows</span></span>
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
<p>It is accompanied by a number of helpers for common use cases:</p>
<ul>
<li><code>slice_head()</code> and <code>slice_tail()</code> select the
first or last rows.</li>
</ul>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>starwars <span class="sc">%>%</span> <span class="fu">slice_head</span>(<span class="at">n =</span> <span class="dv">3</span>)</span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 14</span></span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…</span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
<ul>
<li><code>slice_sample()</code> randomly selects rows. Use the option
prop to choose a certain proportion of the cases.</li>
</ul>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>starwars <span class="sc">%>%</span> <span class="fu">slice_sample</span>(<span class="at">n =</span> <span class="dv">5</span>)</span>
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="co">#> # A tibble: 5 × 14</span></span>
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co">#> 1 Ayla Sec… 178 55 none blue hazel 48 fema… femin…</span></span>
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="co">#> 2 Bossk 190 113 none green red 53 male mascu…</span></span>
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co">#> 3 San Hill 191 NA none grey gold NA male mascu…</span></span>
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="co">#> 4 Luminara… 170 56.2 black yellow blue 58 fema… femin…</span></span>
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a><span class="co">#> # ℹ 1 more row</span></span>
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span>
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a>starwars <span class="sc">%>%</span> <span class="fu">slice_sample</span>(<span class="at">prop =</span> <span class="fl">0.1</span>)</span>
<span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a><span class="co">#> # A tibble: 8 × 14</span></span>
<span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb8-15"><a href="#cb8-15" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb8-16"><a href="#cb8-16" tabindex="-1"></a><span class="co">#> 1 Qui-Gon … 193 89 brown fair blue 92 male mascu…</span></span>
<span id="cb8-17"><a href="#cb8-17" tabindex="-1"></a><span class="co">#> 2 Jango Fe… 183 79 black tan brown 66 male mascu…</span></span>
<span id="cb8-18"><a href="#cb8-18" tabindex="-1"></a><span class="co">#> 3 Jocasta … 167 NA white fair blue NA fema… femin…</span></span>
<span id="cb8-19"><a href="#cb8-19" tabindex="-1"></a><span class="co">#> 4 Zam Wese… 168 55 blonde fair, gre… yellow NA fema… femin…</span></span>
<span id="cb8-20"><a href="#cb8-20" tabindex="-1"></a><span class="co">#> # ℹ 4 more rows</span></span>
<span id="cb8-21"><a href="#cb8-21" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb8-22"><a href="#cb8-22" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
<p>Use <code>replace = TRUE</code> to perform a bootstrap sample. If
needed, you can weight the sample with the <code>weight</code>
argument.</p>
<ul>
<li><code>slice_min()</code> and <code>slice_max()</code> select rows
with highest or lowest values of a variable. Note that we first must
choose only the values which are not NA.</li>
</ul>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>starwars <span class="sc">%>%</span></span>
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> <span class="fu">filter</span>(<span class="sc">!</span><span class="fu">is.na</span>(height)) <span class="sc">%>%</span></span>
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> <span class="fu">slice_max</span>(height, <span class="at">n =</span> <span class="dv">3</span>)</span>
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="co">#> # A tibble: 3 × 14</span></span>
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="co">#> 1 Yarael P… 264 NA none white yellow NA male mascu…</span></span>
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#> 2 Tarfful 234 136 brown brown blue NA male mascu…</span></span>
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co">#> 3 Lama Su 229 88 none grey black NA male mascu…</span></span>
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
</div>
<div id="select-columns-with-select" class="section level3">
<h3>Select columns with <code>select()</code></h3>
<p>Often you work with large datasets with many columns but only a few
are actually of interest to you. <code>select()</code> allows you to
rapidly zoom in on a useful subset using operations that usually only
work on numeric variable positions:</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><span class="co"># Select columns by name</span></span>
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a>starwars <span class="sc">%>%</span> <span class="fu">select</span>(hair_color, skin_color, eye_color)</span>
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 3</span></span>
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="co">#> hair_color skin_color eye_color</span></span>
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#> <chr> <chr> <chr> </span></span>
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co">#> 1 blond fair blue </span></span>
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#> 2 <NA> gold yellow </span></span>
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co">#> 3 <NA> white, blue red </span></span>
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a><span class="co">#> 4 none white yellow </span></span>
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co"># Select all columns between hair_color and eye_color (inclusive)</span></span>
<span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a>starwars <span class="sc">%>%</span> <span class="fu">select</span>(hair_color<span class="sc">:</span>eye_color)</span>
<span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 3</span></span>
<span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a><span class="co">#> hair_color skin_color eye_color</span></span>
<span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a><span class="co">#> <chr> <chr> <chr> </span></span>
<span id="cb10-16"><a href="#cb10-16" tabindex="-1"></a><span class="co">#> 1 blond fair blue </span></span>
<span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a><span class="co">#> 2 <NA> gold yellow </span></span>
<span id="cb10-18"><a href="#cb10-18" tabindex="-1"></a><span class="co">#> 3 <NA> white, blue red </span></span>
<span id="cb10-19"><a href="#cb10-19" tabindex="-1"></a><span class="co">#> 4 none white yellow </span></span>
<span id="cb10-20"><a href="#cb10-20" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb10-21"><a href="#cb10-21" tabindex="-1"></a><span class="co"># Select all columns except those from hair_color to eye_color (inclusive)</span></span>
<span id="cb10-22"><a href="#cb10-22" tabindex="-1"></a>starwars <span class="sc">%>%</span> <span class="fu">select</span>(<span class="sc">!</span>(hair_color<span class="sc">:</span>eye_color))</span>
<span id="cb10-23"><a href="#cb10-23" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 11</span></span>
<span id="cb10-24"><a href="#cb10-24" tabindex="-1"></a><span class="co">#> name height mass birth_year sex gender homeworld species films vehicles</span></span>
<span id="cb10-25"><a href="#cb10-25" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <dbl> <chr> <chr> <chr> <chr> <lis> <list> </span></span>
<span id="cb10-26"><a href="#cb10-26" tabindex="-1"></a><span class="co">#> 1 Luke Sk… 172 77 19 male mascu… Tatooine Human <chr> <chr> </span></span>
<span id="cb10-27"><a href="#cb10-27" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 112 none mascu… Tatooine Droid <chr> <chr> </span></span>
<span id="cb10-28"><a href="#cb10-28" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 33 none mascu… Naboo Droid <chr> <chr> </span></span>
<span id="cb10-29"><a href="#cb10-29" tabindex="-1"></a><span class="co">#> 4 Darth V… 202 136 41.9 male mascu… Tatooine Human <chr> <chr> </span></span>
<span id="cb10-30"><a href="#cb10-30" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb10-31"><a href="#cb10-31" tabindex="-1"></a><span class="co">#> # ℹ 1 more variable: starships <list></span></span>
<span id="cb10-32"><a href="#cb10-32" tabindex="-1"></a><span class="co"># Select all columns ending with color</span></span>
<span id="cb10-33"><a href="#cb10-33" tabindex="-1"></a>starwars <span class="sc">%>%</span> <span class="fu">select</span>(<span class="fu">ends_with</span>(<span class="st">"color"</span>))</span>
<span id="cb10-34"><a href="#cb10-34" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 3</span></span>
<span id="cb10-35"><a href="#cb10-35" tabindex="-1"></a><span class="co">#> hair_color skin_color eye_color</span></span>
<span id="cb10-36"><a href="#cb10-36" tabindex="-1"></a><span class="co">#> <chr> <chr> <chr> </span></span>
<span id="cb10-37"><a href="#cb10-37" tabindex="-1"></a><span class="co">#> 1 blond fair blue </span></span>
<span id="cb10-38"><a href="#cb10-38" tabindex="-1"></a><span class="co">#> 2 <NA> gold yellow </span></span>
<span id="cb10-39"><a href="#cb10-39" tabindex="-1"></a><span class="co">#> 3 <NA> white, blue red </span></span>
<span id="cb10-40"><a href="#cb10-40" tabindex="-1"></a><span class="co">#> 4 none white yellow </span></span>
<span id="cb10-41"><a href="#cb10-41" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p>There are a number of helper functions you can use within
<code>select()</code>, like <code>starts_with()</code>,
<code>ends_with()</code>, <code>matches()</code> and
<code>contains()</code>. These let you quickly match larger blocks of
variables that meet some criterion. See <code>?select</code> for more
details.</p>
<p>You can rename variables with <code>select()</code> by using named
arguments:</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>starwars <span class="sc">%>%</span> <span class="fu">select</span>(<span class="at">home_world =</span> homeworld)</span>
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 1</span></span>
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co">#> home_world</span></span>
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="co">#> <chr> </span></span>
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="co">#> 1 Tatooine </span></span>
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a><span class="co">#> 2 Tatooine </span></span>
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co">#> 3 Naboo </span></span>
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a><span class="co">#> 4 Tatooine </span></span>
<span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p>But because <code>select()</code> drops all the variables not
explicitly mentioned, it’s not that useful. Instead, use
<code>rename()</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>starwars <span class="sc">%>%</span> <span class="fu">rename</span>(<span class="at">home_world =</span> homeworld)</span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 14</span></span>
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="co">#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…</span></span>
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span>
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span>
<span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="co">#> 4 Darth Va… 202 136 none white yellow 41.9 male mascu…</span></span>
<span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: home_world <chr>, species <chr>, films <list>,</span></span>
<span id="cb12-11"><a href="#cb12-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
</div>
<div id="add-new-columns-with-mutate" class="section level3">
<h3>Add new columns with <code>mutate()</code></h3>
<p>Besides selecting sets of existing columns, it’s often useful to add
new columns that are functions of existing columns. This is the job of
<code>mutate()</code>:</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>starwars <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">height_m =</span> height <span class="sc">/</span> <span class="dv">100</span>)</span>
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 15</span></span>
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…</span></span>
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span>
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span>
<span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a><span class="co">#> 4 Darth Va… 202 136 none white yellow 41.9 male mascu…</span></span>
<span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a><span class="co">#> # ℹ 6 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list>, height_m <dbl></span></span></code></pre></div>
<p>We can’t see the height in meters we just calculated, but we can fix
that using a select command.</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>starwars <span class="sc">%>%</span></span>
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">height_m =</span> height <span class="sc">/</span> <span class="dv">100</span>) <span class="sc">%>%</span></span>
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a> <span class="fu">select</span>(height_m, height, <span class="fu">everything</span>())</span>
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 15</span></span>
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#> height_m height name mass hair_color skin_color eye_color birth_year sex </span></span>
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="co">#> <dbl> <int> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr></span></span>
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#> 1 1.72 172 Luke S… 77 blond fair blue 19 male </span></span>
<span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="co">#> 2 1.67 167 C-3PO 75 <NA> gold yellow 112 none </span></span>
<span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="co">#> 3 0.96 96 R2-D2 32 <NA> white, bl… red 33 none </span></span>
<span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="co">#> 4 2.02 202 Darth … 136 none white yellow 41.9 male </span></span>
<span id="cb14-11"><a href="#cb14-11" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb14-12"><a href="#cb14-12" tabindex="-1"></a><span class="co">#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,</span></span>
<span id="cb14-13"><a href="#cb14-13" tabindex="-1"></a><span class="co">#> # films <list>, vehicles <list>, starships <list></span></span></code></pre></div>
<p><code>dplyr::mutate()</code> is similar to the base
<code>transform()</code>, but allows you to refer to columns that you’ve
just created:</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>starwars <span class="sc">%>%</span></span>
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> <span class="at">height_m =</span> height <span class="sc">/</span> <span class="dv">100</span>,</span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a> <span class="at">BMI =</span> mass <span class="sc">/</span> (height_m<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a> ) <span class="sc">%>%</span></span>
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a> <span class="fu">select</span>(BMI, <span class="fu">everything</span>())</span>
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 16</span></span>
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a><span class="co">#> BMI name height mass hair_color skin_color eye_color birth_year sex </span></span>
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a><span class="co">#> <dbl> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr></span></span>
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a><span class="co">#> 1 26.0 Luke Skyw… 172 77 blond fair blue 19 male </span></span>
<span id="cb15-11"><a href="#cb15-11" tabindex="-1"></a><span class="co">#> 2 26.9 C-3PO 167 75 <NA> gold yellow 112 none </span></span>
<span id="cb15-12"><a href="#cb15-12" tabindex="-1"></a><span class="co">#> 3 34.7 R2-D2 96 32 <NA> white, bl… red 33 none </span></span>
<span id="cb15-13"><a href="#cb15-13" tabindex="-1"></a><span class="co">#> 4 33.3 Darth Vad… 202 136 none white yellow 41.9 male </span></span>
<span id="cb15-14"><a href="#cb15-14" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb15-15"><a href="#cb15-15" tabindex="-1"></a><span class="co">#> # ℹ 7 more variables: gender <chr>, homeworld <chr>, species <chr>,</span></span>
<span id="cb15-16"><a href="#cb15-16" tabindex="-1"></a><span class="co">#> # films <list>, vehicles <list>, starships <list>, height_m <dbl></span></span></code></pre></div>
<p>If you only want to keep the new variables, use
<code>.keep = "none"</code>:</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>starwars <span class="sc">%>%</span></span>
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> <span class="at">height_m =</span> height <span class="sc">/</span> <span class="dv">100</span>,</span>
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a> <span class="at">BMI =</span> mass <span class="sc">/</span> (height_m<span class="sc">^</span><span class="dv">2</span>),</span>
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a> <span class="at">.keep =</span> <span class="st">"none"</span></span>
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a> )</span>
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 2</span></span>
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="co">#> height_m BMI</span></span>
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a><span class="co">#> <dbl> <dbl></span></span>
<span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a><span class="co">#> 1 1.72 26.0</span></span>
<span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a><span class="co">#> 2 1.67 26.9</span></span>
<span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a><span class="co">#> 3 0.96 34.7</span></span>
<span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a><span class="co">#> 4 2.02 33.3</span></span>
<span id="cb16-14"><a href="#cb16-14" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
</div>
<div id="change-column-order-with-relocate" class="section level3">
<h3>Change column order with <code>relocate()</code></h3>
<p>Use a similar syntax as <code>select()</code> to move blocks of
columns at once</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>starwars <span class="sc">%>%</span> <span class="fu">relocate</span>(sex<span class="sc">:</span>homeworld, <span class="at">.before =</span> height)</span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 14</span></span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a><span class="co">#> name sex gender homeworld height mass hair_color skin_color eye_color</span></span>
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a><span class="co">#> <chr> <chr> <chr> <chr> <int> <dbl> <chr> <chr> <chr> </span></span>
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="co">#> 1 Luke Skyw… male mascu… Tatooine 172 77 blond fair blue </span></span>
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co">#> 2 C-3PO none mascu… Tatooine 167 75 <NA> gold yellow </span></span>
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a><span class="co">#> 3 R2-D2 none mascu… Naboo 96 32 <NA> white, bl… red </span></span>
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a><span class="co">#> 4 Darth Vad… male mascu… Tatooine 202 136 none white yellow </span></span>
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: birth_year <dbl>, species <chr>, films <list>,</span></span>
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span></code></pre></div>
</div>
<div id="summarise-values-with-summarise" class="section level3">
<h3>Summarise values with <code>summarise()</code></h3>
<p>The last verb is <code>summarise()</code>. It collapses a data frame
to a single row.</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>starwars <span class="sc">%>%</span> <span class="fu">summarise</span>(<span class="at">height =</span> <span class="fu">mean</span>(height, <span class="at">na.rm =</span> <span class="cn">TRUE</span>))</span>
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a><span class="co">#> # A tibble: 1 × 1</span></span>
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="co">#> height</span></span>
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a><span class="co">#> <dbl></span></span>
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a><span class="co">#> 1 175.</span></span></code></pre></div>
<p>It’s not that useful until we learn the <code>group_by()</code> verb
below.</p>
</div>
<div id="commonalities" class="section level3">
<h3>Commonalities</h3>
<p>You may have noticed that the syntax and function of all these verbs
are very similar:</p>
<ul>
<li><p>The first argument is a data frame.</p></li>
<li><p>The subsequent arguments describe what to do with the data frame.
You can refer to columns in the data frame directly without using
<code>$</code>.</p></li>
<li><p>The result is a new data frame</p></li>
</ul>
<p>Together these properties make it easy to chain together multiple
simple steps to achieve a complex result.</p>
<p>These five functions provide the basis of a language of data
manipulation. At the most basic level, you can only alter a tidy data
frame in five useful ways: you can reorder the rows
(<code>arrange()</code>), pick observations and variables of interest
(<code>filter()</code> and <code>select()</code>), add new variables
that are functions of existing variables (<code>mutate()</code>), or
collapse many values to a summary (<code>summarise()</code>).</p>
</div>
</div>
<div id="combining-functions-with" class="section level2">
<h2>Combining functions with <code>%>%</code></h2>
<p>The dplyr API is functional in the sense that function calls don’t
have side-effects. You must always save their results. This doesn’t lead
to particularly elegant code, especially if you want to do many
operations at once. You either have to do it step-by-step:</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>a1 <span class="ot"><-</span> <span class="fu">group_by</span>(starwars, species, sex)</span>
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a>a2 <span class="ot"><-</span> <span class="fu">select</span>(a1, height, mass)</span>
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a>a3 <span class="ot"><-</span> <span class="fu">summarise</span>(a2,</span>
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a> <span class="at">height =</span> <span class="fu">mean</span>(height, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a> <span class="at">mass =</span> <span class="fu">mean</span>(mass, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a>)</span></code></pre></div>
<p>Or if you don’t want to name the intermediate results, you need to
wrap the function calls inside each other:</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><span class="fu">summarise</span>(</span>
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a> <span class="fu">select</span>(</span>
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a> <span class="fu">group_by</span>(starwars, species, sex),</span>
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a> height, mass</span>
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a> ),</span>
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a> <span class="at">height =</span> <span class="fu">mean</span>(height, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a> <span class="at">mass =</span> <span class="fu">mean</span>(mass, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a>)</span>
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a><span class="co">#> Adding missing grouping variables: `species`, `sex`</span></span>
<span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a><span class="co">#> `summarise()` has grouped output by 'species'. You can override using the</span></span>
<span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a><span class="co">#> `.groups` argument.</span></span>
<span id="cb20-12"><a href="#cb20-12" tabindex="-1"></a><span class="co">#> # A tibble: 41 × 4</span></span>
<span id="cb20-13"><a href="#cb20-13" tabindex="-1"></a><span class="co">#> # Groups: species [38]</span></span>
<span id="cb20-14"><a href="#cb20-14" tabindex="-1"></a><span class="co">#> species sex height mass</span></span>
<span id="cb20-15"><a href="#cb20-15" tabindex="-1"></a><span class="co">#> <chr> <chr> <dbl> <dbl></span></span>
<span id="cb20-16"><a href="#cb20-16" tabindex="-1"></a><span class="co">#> 1 Aleena male 79 15</span></span>
<span id="cb20-17"><a href="#cb20-17" tabindex="-1"></a><span class="co">#> 2 Besalisk male 198 102</span></span>
<span id="cb20-18"><a href="#cb20-18" tabindex="-1"></a><span class="co">#> 3 Cerean male 198 82</span></span>
<span id="cb20-19"><a href="#cb20-19" tabindex="-1"></a><span class="co">#> 4 Chagrian male 196 NaN</span></span>
<span id="cb20-20"><a href="#cb20-20" tabindex="-1"></a><span class="co">#> # ℹ 37 more rows</span></span></code></pre></div>
<p>This is difficult to read because the order of the operations is from
inside to out. Thus, the arguments are a long way away from the
function. To get around this problem, dplyr provides the
<code>%>%</code> operator from magrittr. <code>x %>% f(y)</code>
turns into <code>f(x, y)</code> so you can use it to rewrite multiple
operations that you can read left-to-right, top-to-bottom (reading the
pipe operator as “then”):</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>starwars <span class="sc">%>%</span></span>
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a> <span class="fu">group_by</span>(species, sex) <span class="sc">%>%</span></span>
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a> <span class="fu">select</span>(height, mass) <span class="sc">%>%</span></span>
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a> <span class="fu">summarise</span>(</span>
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a> <span class="at">height =</span> <span class="fu">mean</span>(height, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a> <span class="at">mass =</span> <span class="fu">mean</span>(mass, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a> )</span></code></pre></div>
</div>
<div id="patterns-of-operations" class="section level2">
<h2>Patterns of operations</h2>
<p>The dplyr verbs can be classified by the type of operations they
accomplish (we sometimes speak of their <strong>semantics</strong>,
i.e., their meaning). It’s helpful to have a good grasp of the
difference between select and mutate operations.</p>
<div id="selecting-operations" class="section level3">
<h3>Selecting operations</h3>
<p>One of the appealing features of dplyr is that you can refer to
columns from the tibble as if they were regular variables. However, the
syntactic uniformity of referring to bare column names hides semantical
differences across the verbs. A column symbol supplied to
<code>select()</code> does not have the same meaning as the same symbol
supplied to <code>mutate()</code>.</p>
<p>Selecting operations expect column names and positions. Hence, when
you call <code>select()</code> with bare variable names, they actually
represent their own positions in the tibble. The following calls are
completely equivalent from dplyr’s point of view:</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><span class="co"># `name` represents the integer 1</span></span>
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a><span class="fu">select</span>(starwars, name)</span>
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 1</span></span>
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a><span class="co">#> name </span></span>
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="co">#> <chr> </span></span>
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a><span class="co">#> 1 Luke Skywalker</span></span>
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a><span class="co">#> 2 C-3PO </span></span>
<span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a><span class="co">#> 3 R2-D2 </span></span>
<span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a><span class="co">#> 4 Darth Vader </span></span>
<span id="cb22-10"><a href="#cb22-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb22-11"><a href="#cb22-11" tabindex="-1"></a><span class="fu">select</span>(starwars, <span class="dv">1</span>)</span>
<span id="cb22-12"><a href="#cb22-12" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 1</span></span>
<span id="cb22-13"><a href="#cb22-13" tabindex="-1"></a><span class="co">#> name </span></span>
<span id="cb22-14"><a href="#cb22-14" tabindex="-1"></a><span class="co">#> <chr> </span></span>
<span id="cb22-15"><a href="#cb22-15" tabindex="-1"></a><span class="co">#> 1 Luke Skywalker</span></span>
<span id="cb22-16"><a href="#cb22-16" tabindex="-1"></a><span class="co">#> 2 C-3PO </span></span>
<span id="cb22-17"><a href="#cb22-17" tabindex="-1"></a><span class="co">#> 3 R2-D2 </span></span>
<span id="cb22-18"><a href="#cb22-18" tabindex="-1"></a><span class="co">#> 4 Darth Vader </span></span>
<span id="cb22-19"><a href="#cb22-19" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p>By the same token, this means that you cannot refer to variables from
the surrounding context if they have the same name as one of the
columns. In the following example, <code>height</code> still represents
2, not 5:</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>height <span class="ot"><-</span> <span class="dv">5</span></span>
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="fu">select</span>(starwars, height)</span>
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 1</span></span>
<span id="cb23-4"><a href="#cb23-4" tabindex="-1"></a><span class="co">#> height</span></span>
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a><span class="co">#> <int></span></span>
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="co">#> 1 172</span></span>
<span id="cb23-7"><a href="#cb23-7" tabindex="-1"></a><span class="co">#> 2 167</span></span>
<span id="cb23-8"><a href="#cb23-8" tabindex="-1"></a><span class="co">#> 3 96</span></span>
<span id="cb23-9"><a href="#cb23-9" tabindex="-1"></a><span class="co">#> 4 202</span></span>
<span id="cb23-10"><a href="#cb23-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p>One useful subtlety is that this only applies to bare names and to
selecting calls like <code>c(height, mass)</code> or
<code>height:mass</code>. In all other cases, the columns of the data
frame are not put in scope. This allows you to refer to contextual
variables in selection helpers:</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>name <span class="ot"><-</span> <span class="st">"color"</span></span>
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="fu">select</span>(starwars, <span class="fu">ends_with</span>(name))</span>
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 3</span></span>
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a><span class="co">#> hair_color skin_color eye_color</span></span>
<span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a><span class="co">#> <chr> <chr> <chr> </span></span>
<span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a><span class="co">#> 1 blond fair blue </span></span>
<span id="cb24-7"><a href="#cb24-7" tabindex="-1"></a><span class="co">#> 2 <NA> gold yellow </span></span>
<span id="cb24-8"><a href="#cb24-8" tabindex="-1"></a><span class="co">#> 3 <NA> white, blue red </span></span>
<span id="cb24-9"><a href="#cb24-9" tabindex="-1"></a><span class="co">#> 4 none white yellow </span></span>
<span id="cb24-10"><a href="#cb24-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p>These semantics are usually intuitive. But note the subtle
difference:</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>name <span class="ot"><-</span> <span class="dv">5</span></span>
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a><span class="fu">select</span>(starwars, name, <span class="fu">identity</span>(name))</span>
<span id="cb25-3"><a href="#cb25-3" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 2</span></span>
<span id="cb25-4"><a href="#cb25-4" tabindex="-1"></a><span class="co">#> name skin_color </span></span>
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a><span class="co">#> <chr> <chr> </span></span>
<span id="cb25-6"><a href="#cb25-6" tabindex="-1"></a><span class="co">#> 1 Luke Skywalker fair </span></span>
<span id="cb25-7"><a href="#cb25-7" tabindex="-1"></a><span class="co">#> 2 C-3PO gold </span></span>
<span id="cb25-8"><a href="#cb25-8" tabindex="-1"></a><span class="co">#> 3 R2-D2 white, blue</span></span>
<span id="cb25-9"><a href="#cb25-9" tabindex="-1"></a><span class="co">#> 4 Darth Vader white </span></span>
<span id="cb25-10"><a href="#cb25-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p>In the first argument, <code>name</code> represents its own position
<code>1</code>. In the second argument, <code>name</code> is evaluated
in the surrounding context and represents the fifth column.</p>
<p>For a long time, <code>select()</code> used to only understand column
positions. Counting from dplyr 0.6, it now understands column names as
well. This makes it a bit easier to program with
<code>select()</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>vars <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"name"</span>, <span class="st">"height"</span>)</span>
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a><span class="fu">select</span>(starwars, <span class="fu">all_of</span>(vars), <span class="st">"mass"</span>)</span>
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 3</span></span>
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a><span class="co">#> name height mass</span></span>
<span id="cb26-5"><a href="#cb26-5" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl></span></span>
<span id="cb26-6"><a href="#cb26-6" tabindex="-1"></a><span class="co">#> 1 Luke Skywalker 172 77</span></span>
<span id="cb26-7"><a href="#cb26-7" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75</span></span>
<span id="cb26-8"><a href="#cb26-8" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32</span></span>
<span id="cb26-9"><a href="#cb26-9" tabindex="-1"></a><span class="co">#> 4 Darth Vader 202 136</span></span>
<span id="cb26-10"><a href="#cb26-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
</div>
<div id="mutating-operations" class="section level3">
<h3>Mutating operations</h3>
<p>Mutate semantics are quite different from selection semantics.
Whereas <code>select()</code> expects column names or positions,
<code>mutate()</code> expects <em>column vectors</em>. We will set up a
smaller tibble to use for our examples.</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="ot"><-</span> starwars <span class="sc">%>%</span> <span class="fu">select</span>(name, height, mass)</span></code></pre></div>
<p>When we use <code>select()</code>, the bare column names stand for
their own positions in the tibble. For <code>mutate()</code> on the
other hand, column symbols represent the actual column vectors stored in
the tibble. Consider what happens if we give a string or a number to
<code>mutate()</code>:</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><span class="fu">mutate</span>(df, <span class="st">"height"</span>, <span class="dv">2</span>)</span>
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 5</span></span>
<span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a><span class="co">#> name height mass `"height"` `2`</span></span>
<span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <dbl></span></span>
<span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a><span class="co">#> 1 Luke Skywalker 172 77 height 2</span></span>
<span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 height 2</span></span>
<span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 height 2</span></span>
<span id="cb28-8"><a href="#cb28-8" tabindex="-1"></a><span class="co">#> 4 Darth Vader 202 136 height 2</span></span>
<span id="cb28-9"><a href="#cb28-9" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p><code>mutate()</code> gets length-1 vectors that it interprets as new
columns in the data frame. These vectors are recycled so they match the
number of rows. That’s why it doesn’t make sense to supply expressions
like <code>"height" + 10</code> to <code>mutate()</code>. This amounts
to adding 10 to a string! The correct expression is:</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><span class="fu">mutate</span>(df, height <span class="sc">+</span> <span class="dv">10</span>)</span>
<span id="cb29-2"><a href="#cb29-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 4</span></span>
<span id="cb29-3"><a href="#cb29-3" tabindex="-1"></a><span class="co">#> name height mass `height + 10`</span></span>
<span id="cb29-4"><a href="#cb29-4" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <dbl></span></span>
<span id="cb29-5"><a href="#cb29-5" tabindex="-1"></a><span class="co">#> 1 Luke Skywalker 172 77 182</span></span>
<span id="cb29-6"><a href="#cb29-6" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 177</span></span>
<span id="cb29-7"><a href="#cb29-7" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 106</span></span>
<span id="cb29-8"><a href="#cb29-8" tabindex="-1"></a><span class="co">#> 4 Darth Vader 202 136 212</span></span>
<span id="cb29-9"><a href="#cb29-9" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p>In the same way, you can unquote values from the context if these
values represent a valid column. They must be either length 1 (they then
get recycled) or have the same length as the number of rows. In the
following example we create a new vector that we add to the data
frame:</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>var <span class="ot"><-</span> <span class="fu">seq</span>(<span class="dv">1</span>, <span class="fu">nrow</span>(df))</span>
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a><span class="fu">mutate</span>(df, <span class="at">new =</span> var)</span>
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 4</span></span>
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a><span class="co">#> name height mass new</span></span>
<span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <int></span></span>
<span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a><span class="co">#> 1 Luke Skywalker 172 77 1</span></span>
<span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 2</span></span>
<span id="cb30-8"><a href="#cb30-8" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 3</span></span>
<span id="cb30-9"><a href="#cb30-9" tabindex="-1"></a><span class="co">#> 4 Darth Vader 202 136 4</span></span>
<span id="cb30-10"><a href="#cb30-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
<p>A case in point is <code>group_by()</code>. While you might think it
has select semantics, it actually has mutate semantics. This is quite
handy as it allows to group by a modified column:</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><span class="fu">group_by</span>(starwars, sex)</span>
<span id="cb31-2"><a href="#cb31-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 14</span></span>
<span id="cb31-3"><a href="#cb31-3" tabindex="-1"></a><span class="co">#> # Groups: sex [5]</span></span>
<span id="cb31-4"><a href="#cb31-4" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb31-5"><a href="#cb31-5" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb31-6"><a href="#cb31-6" tabindex="-1"></a><span class="co">#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…</span></span>
<span id="cb31-7"><a href="#cb31-7" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span>
<span id="cb31-8"><a href="#cb31-8" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span>
<span id="cb31-9"><a href="#cb31-9" tabindex="-1"></a><span class="co">#> 4 Darth Va… 202 136 none white yellow 41.9 male mascu…</span></span>
<span id="cb31-10"><a href="#cb31-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb31-11"><a href="#cb31-11" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb31-12"><a href="#cb31-12" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span>
<span id="cb31-13"><a href="#cb31-13" tabindex="-1"></a><span class="fu">group_by</span>(starwars, <span class="at">sex =</span> <span class="fu">as.factor</span>(sex))</span>
<span id="cb31-14"><a href="#cb31-14" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 14</span></span>
<span id="cb31-15"><a href="#cb31-15" tabindex="-1"></a><span class="co">#> # Groups: sex [5]</span></span>
<span id="cb31-16"><a href="#cb31-16" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb31-17"><a href="#cb31-17" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <fct> <chr> </span></span>
<span id="cb31-18"><a href="#cb31-18" tabindex="-1"></a><span class="co">#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…</span></span>
<span id="cb31-19"><a href="#cb31-19" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span>
<span id="cb31-20"><a href="#cb31-20" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span>
<span id="cb31-21"><a href="#cb31-21" tabindex="-1"></a><span class="co">#> 4 Darth Va… 202 136 none white yellow 41.9 male mascu…</span></span>
<span id="cb31-22"><a href="#cb31-22" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb31-23"><a href="#cb31-23" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb31-24"><a href="#cb31-24" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list></span></span>
<span id="cb31-25"><a href="#cb31-25" tabindex="-1"></a><span class="fu">group_by</span>(starwars, <span class="at">height_binned =</span> <span class="fu">cut</span>(height, <span class="dv">3</span>))</span>
<span id="cb31-26"><a href="#cb31-26" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 15</span></span>
<span id="cb31-27"><a href="#cb31-27" tabindex="-1"></a><span class="co">#> # Groups: height_binned [4]</span></span>
<span id="cb31-28"><a href="#cb31-28" tabindex="-1"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span>
<span id="cb31-29"><a href="#cb31-29" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span>
<span id="cb31-30"><a href="#cb31-30" tabindex="-1"></a><span class="co">#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…</span></span>
<span id="cb31-31"><a href="#cb31-31" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span>
<span id="cb31-32"><a href="#cb31-32" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span>
<span id="cb31-33"><a href="#cb31-33" tabindex="-1"></a><span class="co">#> 4 Darth Va… 202 136 none white yellow 41.9 male mascu…</span></span>
<span id="cb31-34"><a href="#cb31-34" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span>
<span id="cb31-35"><a href="#cb31-35" tabindex="-1"></a><span class="co">#> # ℹ 6 more variables: homeworld <chr>, species <chr>, films <list>,</span></span>
<span id="cb31-36"><a href="#cb31-36" tabindex="-1"></a><span class="co">#> # vehicles <list>, starships <list>, height_binned <fct></span></span></code></pre></div>
<p>This is why you can’t supply a column name to
<code>group_by()</code>. This amounts to creating a new column
containing the string recycled to the number of rows:</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><span class="fu">group_by</span>(df, <span class="st">"month"</span>)</span>
<span id="cb32-2"><a href="#cb32-2" tabindex="-1"></a><span class="co">#> # A tibble: 87 × 4</span></span>
<span id="cb32-3"><a href="#cb32-3" tabindex="-1"></a><span class="co">#> # Groups: "month" [1]</span></span>
<span id="cb32-4"><a href="#cb32-4" tabindex="-1"></a><span class="co">#> name height mass `"month"`</span></span>
<span id="cb32-5"><a href="#cb32-5" tabindex="-1"></a><span class="co">#> <chr> <int> <dbl> <chr> </span></span>
<span id="cb32-6"><a href="#cb32-6" tabindex="-1"></a><span class="co">#> 1 Luke Skywalker 172 77 month </span></span>
<span id="cb32-7"><a href="#cb32-7" tabindex="-1"></a><span class="co">#> 2 C-3PO 167 75 month </span></span>
<span id="cb32-8"><a href="#cb32-8" tabindex="-1"></a><span class="co">#> 3 R2-D2 96 32 month </span></span>
<span id="cb32-9"><a href="#cb32-9" tabindex="-1"></a><span class="co">#> 4 Darth Vader 202 136 month </span></span>
<span id="cb32-10"><a href="#cb32-10" tabindex="-1"></a><span class="co">#> # ℹ 83 more rows</span></span></code></pre></div>
</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>
|