EVOLUTION-MANAGER
Edit File: dplyr.html
<!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>// Hide empty <a> tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> // v0.0.1 // Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. document.addEventListener('DOMContentLoaded', function() { const codeList = document.getElementsByClassName("sourceCode"); for (var i = 0; i < codeList.length; i++) { var linkList = codeList[i].getElementsByTagName('a'); for (var j = 0; j < linkList.length; j++) { if (linkList[j].innerHTML === "") { linkList[j].setAttribute('aria-hidden', 'true'); } } } }); </script> <style type="text/css">code{white-space: pre;}</style> <style type="text/css" data-origin="pandoc"> code.sourceCode > span { display: inline-block; line-height: 1.25; } code.sourceCode > span { color: inherit; text-decoration: inherit; } code.sourceCode > span:empty { height: 1.2em; } .sourceCode { overflow: visible; } code.sourceCode { white-space: pre; position: relative; } div.sourceCode { margin: 1em 0; } pre.sourceCode { margin: 0; } @media screen { div.sourceCode { overflow: auto; } } @media print { code.sourceCode { white-space: pre-wrap; } 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 { code.sourceCode > span > a:first-child::before { text-decoration: underline; } } code span.al { color: #ff0000; font-weight: bold; } /* Alert */ code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */ code span.at { color: #7d9029; } /* Attribute */ code span.bn { color: #40a070; } /* BaseN */ code span.bu { } /* BuiltIn */ code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */ code span.ch { color: #4070a0; } /* Char */ code span.cn { color: #880000; } /* Constant */ code span.co { color: #60a0b0; font-style: italic; } /* Comment */ code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */ code span.do { color: #ba2121; font-style: italic; } /* Documentation */ code span.dt { color: #902000; } /* DataType */ code span.dv { color: #40a070; } /* DecVal */ code span.er { color: #ff0000; font-weight: bold; } /* Error */ code span.ex { } /* Extension */ code span.fl { color: #40a070; } /* Float */ code span.fu { color: #06287e; } /* Function */ code span.im { } /* Import */ code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */ code span.kw { color: #007020; font-weight: bold; } /* Keyword */ code span.op { color: #666666; } /* Operator */ code span.ot { color: #007020; } /* Other */ code span.pp { color: #bc7a00; } /* Preprocessor */ code span.sc { color: #4070a0; } /* SpecialChar */ code span.ss { color: #bb6688; } /* SpecialString */ code span.st { color: #4070a0; } /* String */ code span.va { color: #19177c; } /* Variable */ code span.vs { color: #4070a0; } /* VerbatimString */ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */ </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; } for (var j = 0; j < rules.length; j++) { var rule = rules[j]; // check if there is a div.sourceCode rule if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue; var style = rule.style.cssText; // check if color or background-color is set if (rule.style.color === '' && rule.style.backgroundColor === '') 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"></a><span class="kw">dim</span>(starwars)</span> <span id="cb1-2"><a href="#cb1-2"></a><span class="co">#> [1] 87 14</span></span> <span id="cb1-3"><a href="#cb1-3"></a>starwars</span> <span id="cb1-4"><a href="#cb1-4"></a><span class="co">#> # A tibble: 87 x 14</span></span> <span id="cb1-5"><a href="#cb1-5"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb1-7"><a href="#cb1-7"></a><span class="co">#> 1 Luke… 172 77 blond fair blue 19 male mascu…</span></span> <span id="cb1-8"><a href="#cb1-8"></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"></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"></a><span class="co">#> 4 Dart… 202 136 none white yellow 41.9 male mascu…</span></span> <span id="cb1-11"><a href="#cb1-11"></a><span class="co">#> # … with 83 more rows, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb1-12"><a href="#cb1-12"></a><span class="co">#> # films <list>, 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">filter</span>(skin_color <span class="op">==</span><span class="st"> "light"</span>, eye_color <span class="op">==</span><span class="st"> "brown"</span>)</span> <span id="cb2-2"><a href="#cb2-2"></a><span class="co">#> # A tibble: 7 x 14</span></span> <span id="cb2-3"><a href="#cb2-3"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb2-5"><a href="#cb2-5"></a><span class="co">#> 1 Leia… 150 49 brown light brown 19 fema… femin…</span></span> <span id="cb2-6"><a href="#cb2-6"></a><span class="co">#> 2 Bigg… 183 84 black light brown 24 male mascu…</span></span> <span id="cb2-7"><a href="#cb2-7"></a><span class="co">#> 3 Cordé 157 NA brown light brown NA fema… femin…</span></span> <span id="cb2-8"><a href="#cb2-8"></a><span class="co">#> 4 Dormé 165 NA brown light brown NA fema… femin…</span></span> <span id="cb2-9"><a href="#cb2-9"></a><span class="co">#> # … with 3 more rows, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb2-10"><a href="#cb2-10"></a><span class="co">#> # films <list>, 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"></a>starwars[starwars<span class="op">$</span>skin_color <span class="op">==</span><span class="st"> "light"</span> <span class="op">&</span><span class="st"> </span>starwars<span class="op">$</span>eye_color <span class="op">==</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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">arrange</span>(height, mass)</span> <span id="cb4-2"><a href="#cb4-2"></a><span class="co">#> # A tibble: 87 x 14</span></span> <span id="cb4-3"><a href="#cb4-3"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb4-5"><a href="#cb4-5"></a><span class="co">#> 1 Yoda 66 17 white green brown 896 male mascu…</span></span> <span id="cb4-6"><a href="#cb4-6"></a><span class="co">#> 2 Ratt… 79 15 none grey, blue unknown NA male mascu…</span></span> <span id="cb4-7"><a href="#cb4-7"></a><span class="co">#> 3 Wick… 88 20 brown brown brown 8 male mascu…</span></span> <span id="cb4-8"><a href="#cb4-8"></a><span class="co">#> 4 Dud … 94 45 none blue, grey yellow NA male mascu…</span></span> <span id="cb4-9"><a href="#cb4-9"></a><span class="co">#> # … with 83 more rows, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb4-10"><a href="#cb4-10"></a><span class="co">#> # films <list>, 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">arrange</span>(<span class="kw">desc</span>(height))</span> <span id="cb5-2"><a href="#cb5-2"></a><span class="co">#> # A tibble: 87 x 14</span></span> <span id="cb5-3"><a href="#cb5-3"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb5-5"><a href="#cb5-5"></a><span class="co">#> 1 Yara… 264 NA none white yellow NA male mascu…</span></span> <span id="cb5-6"><a href="#cb5-6"></a><span class="co">#> 2 Tarf… 234 136 brown brown blue NA male mascu…</span></span> <span id="cb5-7"><a href="#cb5-7"></a><span class="co">#> 3 Lama… 229 88 none grey black NA male mascu…</span></span> <span id="cb5-8"><a href="#cb5-8"></a><span class="co">#> 4 Chew… 228 112 brown unknown blue 200 male mascu…</span></span> <span id="cb5-9"><a href="#cb5-9"></a><span class="co">#> # … with 83 more rows, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb5-10"><a href="#cb5-10"></a><span class="co">#> # films <list>, 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">slice</span>(<span class="dv">5</span><span class="op">:</span><span class="dv">10</span>)</span> <span id="cb6-2"><a href="#cb6-2"></a><span class="co">#> # A tibble: 6 x 14</span></span> <span id="cb6-3"><a href="#cb6-3"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb6-5"><a href="#cb6-5"></a><span class="co">#> 1 Leia… 150 49 brown light brown 19 fema… femin…</span></span> <span id="cb6-6"><a href="#cb6-6"></a><span class="co">#> 2 Owen… 178 120 brown, gr… light blue 52 male mascu…</span></span> <span id="cb6-7"><a href="#cb6-7"></a><span class="co">#> 3 Beru… 165 75 brown light blue 47 fema… femin…</span></span> <span id="cb6-8"><a href="#cb6-8"></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"></a><span class="co">#> # … with 2 more rows, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb6-10"><a href="#cb6-10"></a><span class="co">#> # films <list>, 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">slice_head</span>(<span class="dt">n =</span> <span class="dv">3</span>)</span> <span id="cb7-2"><a href="#cb7-2"></a><span class="co">#> # A tibble: 3 x 14</span></span> <span id="cb7-3"><a href="#cb7-3"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb7-5"><a href="#cb7-5"></a><span class="co">#> 1 Luke… 172 77 blond fair blue 19 male mascu…</span></span> <span id="cb7-6"><a href="#cb7-6"></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"></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"></a><span class="co">#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span> <span id="cb7-9"><a href="#cb7-9"></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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">slice_sample</span>(<span class="dt">n =</span> <span class="dv">5</span>)</span> <span id="cb8-2"><a href="#cb8-2"></a><span class="co">#> # A tibble: 5 x 14</span></span> <span id="cb8-3"><a href="#cb8-3"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb8-5"><a href="#cb8-5"></a><span class="co">#> 1 Dud … 94 45 none blue, grey yellow NA male mascu…</span></span> <span id="cb8-6"><a href="#cb8-6"></a><span class="co">#> 2 Bossk 190 113 none green red 53 male mascu…</span></span> <span id="cb8-7"><a href="#cb8-7"></a><span class="co">#> 3 Shaa… 178 57 none red, blue… black NA fema… femin…</span></span> <span id="cb8-8"><a href="#cb8-8"></a><span class="co">#> 4 Dormé 165 NA brown light brown NA fema… femin…</span></span> <span id="cb8-9"><a href="#cb8-9"></a><span class="co">#> # … with 1 more row, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb8-10"><a href="#cb8-10"></a><span class="co">#> # films <list>, vehicles <list>, starships <list></span></span> <span id="cb8-11"><a href="#cb8-11"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">slice_sample</span>(<span class="dt">prop =</span> <span class="fl">0.1</span>)</span> <span id="cb8-12"><a href="#cb8-12"></a><span class="co">#> # A tibble: 8 x 14</span></span> <span id="cb8-13"><a href="#cb8-13"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span> <span id="cb8-14"><a href="#cb8-14"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb8-15"><a href="#cb8-15"></a><span class="co">#> 1 Qui-… 193 89 brown fair blue 92 male mascu…</span></span> <span id="cb8-16"><a href="#cb8-16"></a><span class="co">#> 2 Dext… 198 102 none brown yellow NA male mascu…</span></span> <span id="cb8-17"><a href="#cb8-17"></a><span class="co">#> 3 R4-P… 96 NA none silver, r… red, blue NA none femin…</span></span> <span id="cb8-18"><a href="#cb8-18"></a><span class="co">#> 4 Lama… 229 88 none grey black NA male mascu…</span></span> <span id="cb8-19"><a href="#cb8-19"></a><span class="co">#> # … with 4 more rows, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb8-20"><a href="#cb8-20"></a><span class="co">#> # films <list>, 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"></a>starwars <span class="op">%>%</span></span> <span id="cb9-2"><a href="#cb9-2"></a><span class="st"> </span><span class="kw">filter</span>(<span class="op">!</span><span class="kw">is.na</span>(height)) <span class="op">%>%</span></span> <span id="cb9-3"><a href="#cb9-3"></a><span class="st"> </span><span class="kw">slice_max</span>(height, <span class="dt">n =</span> <span class="dv">3</span>)</span> <span id="cb9-4"><a href="#cb9-4"></a><span class="co">#> # A tibble: 3 x 14</span></span> <span id="cb9-5"><a href="#cb9-5"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb9-7"><a href="#cb9-7"></a><span class="co">#> 1 Yara… 264 NA none white yellow NA male mascu…</span></span> <span id="cb9-8"><a href="#cb9-8"></a><span class="co">#> 2 Tarf… 234 136 brown brown blue NA male mascu…</span></span> <span id="cb9-9"><a href="#cb9-9"></a><span class="co">#> 3 Lama… 229 88 none grey black NA male mascu…</span></span> <span id="cb9-10"><a href="#cb9-10"></a><span class="co">#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,</span></span> <span id="cb9-11"><a href="#cb9-11"></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"></a><span class="co"># Select columns by name</span></span> <span id="cb10-2"><a href="#cb10-2"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(hair_color, skin_color, eye_color)</span> <span id="cb10-3"><a href="#cb10-3"></a><span class="co">#> # A tibble: 87 x 3</span></span> <span id="cb10-4"><a href="#cb10-4"></a><span class="co">#> hair_color skin_color eye_color</span></span> <span id="cb10-5"><a href="#cb10-5"></a><span class="co">#> <chr> <chr> <chr> </span></span> <span id="cb10-6"><a href="#cb10-6"></a><span class="co">#> 1 blond fair blue </span></span> <span id="cb10-7"><a href="#cb10-7"></a><span class="co">#> 2 <NA> gold yellow </span></span> <span id="cb10-8"><a href="#cb10-8"></a><span class="co">#> 3 <NA> white, blue red </span></span> <span id="cb10-9"><a href="#cb10-9"></a><span class="co">#> 4 none white yellow </span></span> <span id="cb10-10"><a href="#cb10-10"></a><span class="co">#> # … with 83 more rows</span></span> <span id="cb10-11"><a href="#cb10-11"></a><span class="co"># Select all columns between hair_color and eye_color (inclusive)</span></span> <span id="cb10-12"><a href="#cb10-12"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(hair_color<span class="op">:</span>eye_color)</span> <span id="cb10-13"><a href="#cb10-13"></a><span class="co">#> # A tibble: 87 x 3</span></span> <span id="cb10-14"><a href="#cb10-14"></a><span class="co">#> hair_color skin_color eye_color</span></span> <span id="cb10-15"><a href="#cb10-15"></a><span class="co">#> <chr> <chr> <chr> </span></span> <span id="cb10-16"><a href="#cb10-16"></a><span class="co">#> 1 blond fair blue </span></span> <span id="cb10-17"><a href="#cb10-17"></a><span class="co">#> 2 <NA> gold yellow </span></span> <span id="cb10-18"><a href="#cb10-18"></a><span class="co">#> 3 <NA> white, blue red </span></span> <span id="cb10-19"><a href="#cb10-19"></a><span class="co">#> 4 none white yellow </span></span> <span id="cb10-20"><a href="#cb10-20"></a><span class="co">#> # … with 83 more rows</span></span> <span id="cb10-21"><a href="#cb10-21"></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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(<span class="op">!</span>(hair_color<span class="op">:</span>eye_color))</span> <span id="cb10-23"><a href="#cb10-23"></a><span class="co">#> # A tibble: 87 x 11</span></span> <span id="cb10-24"><a href="#cb10-24"></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"></a><span class="co">#> <chr> <int> <dbl> <dbl> <chr> <chr> <chr> <chr> <lis> <list> </span></span> <span id="cb10-26"><a href="#cb10-26"></a><span class="co">#> 1 Luke… 172 77 19 male mascu… Tatooine Human <chr… <chr [2…</span></span> <span id="cb10-27"><a href="#cb10-27"></a><span class="co">#> 2 C-3PO 167 75 112 none mascu… Tatooine Droid <chr… <chr [0…</span></span> <span id="cb10-28"><a href="#cb10-28"></a><span class="co">#> 3 R2-D2 96 32 33 none mascu… Naboo Droid <chr… <chr [0…</span></span> <span id="cb10-29"><a href="#cb10-29"></a><span class="co">#> 4 Dart… 202 136 41.9 male mascu… Tatooine Human <chr… <chr [0…</span></span> <span id="cb10-30"><a href="#cb10-30"></a><span class="co">#> # … with 83 more rows, and 1 more variable: starships <list></span></span> <span id="cb10-31"><a href="#cb10-31"></a><span class="co"># Select all columns ending with color</span></span> <span id="cb10-32"><a href="#cb10-32"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(<span class="kw">ends_with</span>(<span class="st">"color"</span>))</span> <span id="cb10-33"><a href="#cb10-33"></a><span class="co">#> # A tibble: 87 x 3</span></span> <span id="cb10-34"><a href="#cb10-34"></a><span class="co">#> hair_color skin_color eye_color</span></span> <span id="cb10-35"><a href="#cb10-35"></a><span class="co">#> <chr> <chr> <chr> </span></span> <span id="cb10-36"><a href="#cb10-36"></a><span class="co">#> 1 blond fair blue </span></span> <span id="cb10-37"><a href="#cb10-37"></a><span class="co">#> 2 <NA> gold yellow </span></span> <span id="cb10-38"><a href="#cb10-38"></a><span class="co">#> 3 <NA> white, blue red </span></span> <span id="cb10-39"><a href="#cb10-39"></a><span class="co">#> 4 none white yellow </span></span> <span id="cb10-40"><a href="#cb10-40"></a><span class="co">#> # … with 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(<span class="dt">home_world =</span> homeworld)</span> <span id="cb11-2"><a href="#cb11-2"></a><span class="co">#> # A tibble: 87 x 1</span></span> <span id="cb11-3"><a href="#cb11-3"></a><span class="co">#> home_world</span></span> <span id="cb11-4"><a href="#cb11-4"></a><span class="co">#> <chr> </span></span> <span id="cb11-5"><a href="#cb11-5"></a><span class="co">#> 1 Tatooine </span></span> <span id="cb11-6"><a href="#cb11-6"></a><span class="co">#> 2 Tatooine </span></span> <span id="cb11-7"><a href="#cb11-7"></a><span class="co">#> 3 Naboo </span></span> <span id="cb11-8"><a href="#cb11-8"></a><span class="co">#> 4 Tatooine </span></span> <span id="cb11-9"><a href="#cb11-9"></a><span class="co">#> # … with 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">rename</span>(<span class="dt">home_world =</span> homeworld)</span> <span id="cb12-2"><a href="#cb12-2"></a><span class="co">#> # A tibble: 87 x 14</span></span> <span id="cb12-3"><a href="#cb12-3"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb12-5"><a href="#cb12-5"></a><span class="co">#> 1 Luke… 172 77 blond fair blue 19 male mascu…</span></span> <span id="cb12-6"><a href="#cb12-6"></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"></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"></a><span class="co">#> 4 Dart… 202 136 none white yellow 41.9 male mascu…</span></span> <span id="cb12-9"><a href="#cb12-9"></a><span class="co">#> # … with 83 more rows, and 5 more variables: home_world <chr>, species <chr>,</span></span> <span id="cb12-10"><a href="#cb12-10"></a><span class="co">#> # films <list>, 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">mutate</span>(<span class="dt">height_m =</span> height <span class="op">/</span><span class="st"> </span><span class="dv">100</span>)</span> <span id="cb13-2"><a href="#cb13-2"></a><span class="co">#> # A tibble: 87 x 15</span></span> <span id="cb13-3"><a href="#cb13-3"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb13-5"><a href="#cb13-5"></a><span class="co">#> 1 Luke… 172 77 blond fair blue 19 male mascu…</span></span> <span id="cb13-6"><a href="#cb13-6"></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"></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"></a><span class="co">#> 4 Dart… 202 136 none white yellow 41.9 male mascu…</span></span> <span id="cb13-9"><a href="#cb13-9"></a><span class="co">#> # … with 83 more rows, and 6 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb13-10"><a href="#cb13-10"></a><span class="co">#> # films <list>, 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"></a>starwars <span class="op">%>%</span></span> <span id="cb14-2"><a href="#cb14-2"></a><span class="st"> </span><span class="kw">mutate</span>(<span class="dt">height_m =</span> height <span class="op">/</span><span class="st"> </span><span class="dv">100</span>) <span class="op">%>%</span></span> <span id="cb14-3"><a href="#cb14-3"></a><span class="st"> </span><span class="kw">select</span>(height_m, height, <span class="kw">everything</span>())</span> <span id="cb14-4"><a href="#cb14-4"></a><span class="co">#> # A tibble: 87 x 15</span></span> <span id="cb14-5"><a href="#cb14-5"></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"></a><span class="co">#> <dbl> <int> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr></span></span> <span id="cb14-7"><a href="#cb14-7"></a><span class="co">#> 1 1.72 172 Luke… 77 blond fair blue 19 male </span></span> <span id="cb14-8"><a href="#cb14-8"></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"></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"></a><span class="co">#> 4 2.02 202 Dart… 136 none white yellow 41.9 male </span></span> <span id="cb14-11"><a href="#cb14-11"></a><span class="co">#> # … with 83 more rows, and 6 more variables: gender <chr>, homeworld <chr>,</span></span> <span id="cb14-12"><a href="#cb14-12"></a><span class="co">#> # species <chr>, 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"></a>starwars <span class="op">%>%</span></span> <span id="cb15-2"><a href="#cb15-2"></a><span class="st"> </span><span class="kw">mutate</span>(</span> <span id="cb15-3"><a href="#cb15-3"></a> <span class="dt">height_m =</span> height <span class="op">/</span><span class="st"> </span><span class="dv">100</span>,</span> <span id="cb15-4"><a href="#cb15-4"></a> <span class="dt">BMI =</span> mass <span class="op">/</span><span class="st"> </span>(height_m<span class="op">^</span><span class="dv">2</span>)</span> <span id="cb15-5"><a href="#cb15-5"></a> ) <span class="op">%>%</span></span> <span id="cb15-6"><a href="#cb15-6"></a><span class="st"> </span><span class="kw">select</span>(BMI, <span class="kw">everything</span>())</span> <span id="cb15-7"><a href="#cb15-7"></a><span class="co">#> # A tibble: 87 x 16</span></span> <span id="cb15-8"><a href="#cb15-8"></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"></a><span class="co">#> <dbl> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr></span></span> <span id="cb15-10"><a href="#cb15-10"></a><span class="co">#> 1 26.0 Luke… 172 77 blond fair blue 19 male </span></span> <span id="cb15-11"><a href="#cb15-11"></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"></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"></a><span class="co">#> 4 33.3 Dart… 202 136 none white yellow 41.9 male </span></span> <span id="cb15-14"><a href="#cb15-14"></a><span class="co">#> # … with 83 more rows, and 7 more variables: gender <chr>, homeworld <chr>,</span></span> <span id="cb15-15"><a href="#cb15-15"></a><span class="co">#> # species <chr>, films <list>, vehicles <list>, starships <list>,</span></span> <span id="cb15-16"><a href="#cb15-16"></a><span class="co">#> # height_m <dbl></span></span></code></pre></div> <p>If you only want to keep the new variables, use <code>transmute()</code>:</p> <div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1"></a>starwars <span class="op">%>%</span></span> <span id="cb16-2"><a href="#cb16-2"></a><span class="st"> </span><span class="kw">transmute</span>(</span> <span id="cb16-3"><a href="#cb16-3"></a> <span class="dt">height_m =</span> height <span class="op">/</span><span class="st"> </span><span class="dv">100</span>,</span> <span id="cb16-4"><a href="#cb16-4"></a> <span class="dt">BMI =</span> mass <span class="op">/</span><span class="st"> </span>(height_m<span class="op">^</span><span class="dv">2</span>)</span> <span id="cb16-5"><a href="#cb16-5"></a> )</span> <span id="cb16-6"><a href="#cb16-6"></a><span class="co">#> # A tibble: 87 x 2</span></span> <span id="cb16-7"><a href="#cb16-7"></a><span class="co">#> height_m BMI</span></span> <span id="cb16-8"><a href="#cb16-8"></a><span class="co">#> <dbl> <dbl></span></span> <span id="cb16-9"><a href="#cb16-9"></a><span class="co">#> 1 1.72 26.0</span></span> <span id="cb16-10"><a href="#cb16-10"></a><span class="co">#> 2 1.67 26.9</span></span> <span id="cb16-11"><a href="#cb16-11"></a><span class="co">#> 3 0.96 34.7</span></span> <span id="cb16-12"><a href="#cb16-12"></a><span class="co">#> 4 2.02 33.3</span></span> <span id="cb16-13"><a href="#cb16-13"></a><span class="co">#> # … with 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">relocate</span>(sex<span class="op">:</span>homeworld, <span class="dt">.before =</span> height)</span> <span id="cb17-2"><a href="#cb17-2"></a><span class="co">#> # A tibble: 87 x 14</span></span> <span id="cb17-3"><a href="#cb17-3"></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"></a><span class="co">#> <chr> <chr> <chr> <chr> <int> <dbl> <chr> <chr> <chr> </span></span> <span id="cb17-5"><a href="#cb17-5"></a><span class="co">#> 1 Luke… male mascu… Tatooine 172 77 blond fair blue </span></span> <span id="cb17-6"><a href="#cb17-6"></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"></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"></a><span class="co">#> 4 Dart… male mascu… Tatooine 202 136 none white yellow </span></span> <span id="cb17-9"><a href="#cb17-9"></a><span class="co">#> # … with 83 more rows, and 5 more variables: birth_year <dbl>, species <chr>,</span></span> <span id="cb17-10"><a href="#cb17-10"></a><span class="co">#> # films <list>, 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"></a>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">summarise</span>(<span class="dt">height =</span> <span class="kw">mean</span>(height, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>))</span> <span id="cb18-2"><a href="#cb18-2"></a><span class="co">#> # A tibble: 1 x 1</span></span> <span id="cb18-3"><a href="#cb18-3"></a><span class="co">#> height</span></span> <span id="cb18-4"><a href="#cb18-4"></a><span class="co">#> <dbl></span></span> <span id="cb18-5"><a href="#cb18-5"></a><span class="co">#> 1 174.</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"></a>a1 <-<span class="st"> </span><span class="kw">group_by</span>(starwars, species, sex)</span> <span id="cb19-2"><a href="#cb19-2"></a>a2 <-<span class="st"> </span><span class="kw">select</span>(a1, height, mass)</span> <span id="cb19-3"><a href="#cb19-3"></a>a3 <-<span class="st"> </span><span class="kw">summarise</span>(a2,</span> <span id="cb19-4"><a href="#cb19-4"></a> <span class="dt">height =</span> <span class="kw">mean</span>(height, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>),</span> <span id="cb19-5"><a href="#cb19-5"></a> <span class="dt">mass =</span> <span class="kw">mean</span>(mass, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)</span> <span id="cb19-6"><a href="#cb19-6"></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"></a><span class="kw">summarise</span>(</span> <span id="cb20-2"><a href="#cb20-2"></a> <span class="kw">select</span>(</span> <span id="cb20-3"><a href="#cb20-3"></a> <span class="kw">group_by</span>(starwars, species, sex),</span> <span id="cb20-4"><a href="#cb20-4"></a> height, mass</span> <span id="cb20-5"><a href="#cb20-5"></a> ),</span> <span id="cb20-6"><a href="#cb20-6"></a> <span class="dt">height =</span> <span class="kw">mean</span>(height, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>),</span> <span id="cb20-7"><a href="#cb20-7"></a> <span class="dt">mass =</span> <span class="kw">mean</span>(mass, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)</span> <span id="cb20-8"><a href="#cb20-8"></a>)</span> <span id="cb20-9"><a href="#cb20-9"></a><span class="co">#> Adding missing grouping variables: `species`, `sex`</span></span> <span id="cb20-10"><a href="#cb20-10"></a><span class="co">#> `summarise()` regrouping output by 'species' (override with `.groups` argument)</span></span> <span id="cb20-11"><a href="#cb20-11"></a><span class="co">#> # A tibble: 41 x 4</span></span> <span id="cb20-12"><a href="#cb20-12"></a><span class="co">#> # Groups: species [38]</span></span> <span id="cb20-13"><a href="#cb20-13"></a><span class="co">#> species sex height mass</span></span> <span id="cb20-14"><a href="#cb20-14"></a><span class="co">#> <chr> <chr> <dbl> <dbl></span></span> <span id="cb20-15"><a href="#cb20-15"></a><span class="co">#> 1 Aleena male 79 15</span></span> <span id="cb20-16"><a href="#cb20-16"></a><span class="co">#> 2 Besalisk male 198 102</span></span> <span id="cb20-17"><a href="#cb20-17"></a><span class="co">#> 3 Cerean male 198 82</span></span> <span id="cb20-18"><a href="#cb20-18"></a><span class="co">#> 4 Chagrian male 196 NaN</span></span> <span id="cb20-19"><a href="#cb20-19"></a><span class="co">#> # … with 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"></a>starwars <span class="op">%>%</span></span> <span id="cb21-2"><a href="#cb21-2"></a><span class="st"> </span><span class="kw">group_by</span>(species, sex) <span class="op">%>%</span></span> <span id="cb21-3"><a href="#cb21-3"></a><span class="st"> </span><span class="kw">select</span>(height, mass) <span class="op">%>%</span></span> <span id="cb21-4"><a href="#cb21-4"></a><span class="st"> </span><span class="kw">summarise</span>(</span> <span id="cb21-5"><a href="#cb21-5"></a> <span class="dt">height =</span> <span class="kw">mean</span>(height, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>),</span> <span id="cb21-6"><a href="#cb21-6"></a> <span class="dt">mass =</span> <span class="kw">mean</span>(mass, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)</span> <span id="cb21-7"><a href="#cb21-7"></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"></a><span class="co"># `name` represents the integer 1</span></span> <span id="cb22-2"><a href="#cb22-2"></a><span class="kw">select</span>(starwars, name)</span> <span id="cb22-3"><a href="#cb22-3"></a><span class="co">#> # A tibble: 87 x 1</span></span> <span id="cb22-4"><a href="#cb22-4"></a><span class="co">#> name </span></span> <span id="cb22-5"><a href="#cb22-5"></a><span class="co">#> <chr> </span></span> <span id="cb22-6"><a href="#cb22-6"></a><span class="co">#> 1 Luke Skywalker</span></span> <span id="cb22-7"><a href="#cb22-7"></a><span class="co">#> 2 C-3PO </span></span> <span id="cb22-8"><a href="#cb22-8"></a><span class="co">#> 3 R2-D2 </span></span> <span id="cb22-9"><a href="#cb22-9"></a><span class="co">#> 4 Darth Vader </span></span> <span id="cb22-10"><a href="#cb22-10"></a><span class="co">#> # … with 83 more rows</span></span> <span id="cb22-11"><a href="#cb22-11"></a><span class="kw">select</span>(starwars, <span class="dv">1</span>)</span> <span id="cb22-12"><a href="#cb22-12"></a><span class="co">#> # A tibble: 87 x 1</span></span> <span id="cb22-13"><a href="#cb22-13"></a><span class="co">#> name </span></span> <span id="cb22-14"><a href="#cb22-14"></a><span class="co">#> <chr> </span></span> <span id="cb22-15"><a href="#cb22-15"></a><span class="co">#> 1 Luke Skywalker</span></span> <span id="cb22-16"><a href="#cb22-16"></a><span class="co">#> 2 C-3PO </span></span> <span id="cb22-17"><a href="#cb22-17"></a><span class="co">#> 3 R2-D2 </span></span> <span id="cb22-18"><a href="#cb22-18"></a><span class="co">#> 4 Darth Vader </span></span> <span id="cb22-19"><a href="#cb22-19"></a><span class="co">#> # … with 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"></a>height <-<span class="st"> </span><span class="dv">5</span></span> <span id="cb23-2"><a href="#cb23-2"></a><span class="kw">select</span>(starwars, height)</span> <span id="cb23-3"><a href="#cb23-3"></a><span class="co">#> # A tibble: 87 x 1</span></span> <span id="cb23-4"><a href="#cb23-4"></a><span class="co">#> height</span></span> <span id="cb23-5"><a href="#cb23-5"></a><span class="co">#> <int></span></span> <span id="cb23-6"><a href="#cb23-6"></a><span class="co">#> 1 172</span></span> <span id="cb23-7"><a href="#cb23-7"></a><span class="co">#> 2 167</span></span> <span id="cb23-8"><a href="#cb23-8"></a><span class="co">#> 3 96</span></span> <span id="cb23-9"><a href="#cb23-9"></a><span class="co">#> 4 202</span></span> <span id="cb23-10"><a href="#cb23-10"></a><span class="co">#> # … with 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"></a>name <-<span class="st"> "color"</span></span> <span id="cb24-2"><a href="#cb24-2"></a><span class="kw">select</span>(starwars, <span class="kw">ends_with</span>(name))</span> <span id="cb24-3"><a href="#cb24-3"></a><span class="co">#> # A tibble: 87 x 3</span></span> <span id="cb24-4"><a href="#cb24-4"></a><span class="co">#> hair_color skin_color eye_color</span></span> <span id="cb24-5"><a href="#cb24-5"></a><span class="co">#> <chr> <chr> <chr> </span></span> <span id="cb24-6"><a href="#cb24-6"></a><span class="co">#> 1 blond fair blue </span></span> <span id="cb24-7"><a href="#cb24-7"></a><span class="co">#> 2 <NA> gold yellow </span></span> <span id="cb24-8"><a href="#cb24-8"></a><span class="co">#> 3 <NA> white, blue red </span></span> <span id="cb24-9"><a href="#cb24-9"></a><span class="co">#> 4 none white yellow </span></span> <span id="cb24-10"><a href="#cb24-10"></a><span class="co">#> # … with 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"></a>name <-<span class="st"> </span><span class="dv">5</span></span> <span id="cb25-2"><a href="#cb25-2"></a><span class="kw">select</span>(starwars, name, <span class="kw">identity</span>(name))</span> <span id="cb25-3"><a href="#cb25-3"></a><span class="co">#> # A tibble: 87 x 2</span></span> <span id="cb25-4"><a href="#cb25-4"></a><span class="co">#> name skin_color </span></span> <span id="cb25-5"><a href="#cb25-5"></a><span class="co">#> <chr> <chr> </span></span> <span id="cb25-6"><a href="#cb25-6"></a><span class="co">#> 1 Luke Skywalker fair </span></span> <span id="cb25-7"><a href="#cb25-7"></a><span class="co">#> 2 C-3PO gold </span></span> <span id="cb25-8"><a href="#cb25-8"></a><span class="co">#> 3 R2-D2 white, blue</span></span> <span id="cb25-9"><a href="#cb25-9"></a><span class="co">#> 4 Darth Vader white </span></span> <span id="cb25-10"><a href="#cb25-10"></a><span class="co">#> # … with 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"></a>vars <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"name"</span>, <span class="st">"height"</span>)</span> <span id="cb26-2"><a href="#cb26-2"></a><span class="kw">select</span>(starwars, <span class="kw">all_of</span>(vars), <span class="st">"mass"</span>)</span> <span id="cb26-3"><a href="#cb26-3"></a><span class="co">#> # A tibble: 87 x 3</span></span> <span id="cb26-4"><a href="#cb26-4"></a><span class="co">#> name height mass</span></span> <span id="cb26-5"><a href="#cb26-5"></a><span class="co">#> <chr> <int> <dbl></span></span> <span id="cb26-6"><a href="#cb26-6"></a><span class="co">#> 1 Luke Skywalker 172 77</span></span> <span id="cb26-7"><a href="#cb26-7"></a><span class="co">#> 2 C-3PO 167 75</span></span> <span id="cb26-8"><a href="#cb26-8"></a><span class="co">#> 3 R2-D2 96 32</span></span> <span id="cb26-9"><a href="#cb26-9"></a><span class="co">#> 4 Darth Vader 202 136</span></span> <span id="cb26-10"><a href="#cb26-10"></a><span class="co">#> # … with 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"></a>df <-<span class="st"> </span>starwars <span class="op">%>%</span><span class="st"> </span><span class="kw">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"></a><span class="kw">mutate</span>(df, <span class="st">"height"</span>, <span class="dv">2</span>)</span> <span id="cb28-2"><a href="#cb28-2"></a><span class="co">#> # A tibble: 87 x 5</span></span> <span id="cb28-3"><a href="#cb28-3"></a><span class="co">#> name height mass `"height"` `2`</span></span> <span id="cb28-4"><a href="#cb28-4"></a><span class="co">#> <chr> <int> <dbl> <chr> <dbl></span></span> <span id="cb28-5"><a href="#cb28-5"></a><span class="co">#> 1 Luke Skywalker 172 77 height 2</span></span> <span id="cb28-6"><a href="#cb28-6"></a><span class="co">#> 2 C-3PO 167 75 height 2</span></span> <span id="cb28-7"><a href="#cb28-7"></a><span class="co">#> 3 R2-D2 96 32 height 2</span></span> <span id="cb28-8"><a href="#cb28-8"></a><span class="co">#> 4 Darth Vader 202 136 height 2</span></span> <span id="cb28-9"><a href="#cb28-9"></a><span class="co">#> # … with 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"></a><span class="kw">mutate</span>(df, height <span class="op">+</span><span class="st"> </span><span class="dv">10</span>)</span> <span id="cb29-2"><a href="#cb29-2"></a><span class="co">#> # A tibble: 87 x 4</span></span> <span id="cb29-3"><a href="#cb29-3"></a><span class="co">#> name height mass `height + 10`</span></span> <span id="cb29-4"><a href="#cb29-4"></a><span class="co">#> <chr> <int> <dbl> <dbl></span></span> <span id="cb29-5"><a href="#cb29-5"></a><span class="co">#> 1 Luke Skywalker 172 77 182</span></span> <span id="cb29-6"><a href="#cb29-6"></a><span class="co">#> 2 C-3PO 167 75 177</span></span> <span id="cb29-7"><a href="#cb29-7"></a><span class="co">#> 3 R2-D2 96 32 106</span></span> <span id="cb29-8"><a href="#cb29-8"></a><span class="co">#> 4 Darth Vader 202 136 212</span></span> <span id="cb29-9"><a href="#cb29-9"></a><span class="co">#> # … with 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"></a>var <-<span class="st"> </span><span class="kw">seq</span>(<span class="dv">1</span>, <span class="kw">nrow</span>(df))</span> <span id="cb30-2"><a href="#cb30-2"></a><span class="kw">mutate</span>(df, <span class="dt">new =</span> var)</span> <span id="cb30-3"><a href="#cb30-3"></a><span class="co">#> # A tibble: 87 x 4</span></span> <span id="cb30-4"><a href="#cb30-4"></a><span class="co">#> name height mass new</span></span> <span id="cb30-5"><a href="#cb30-5"></a><span class="co">#> <chr> <int> <dbl> <int></span></span> <span id="cb30-6"><a href="#cb30-6"></a><span class="co">#> 1 Luke Skywalker 172 77 1</span></span> <span id="cb30-7"><a href="#cb30-7"></a><span class="co">#> 2 C-3PO 167 75 2</span></span> <span id="cb30-8"><a href="#cb30-8"></a><span class="co">#> 3 R2-D2 96 32 3</span></span> <span id="cb30-9"><a href="#cb30-9"></a><span class="co">#> 4 Darth Vader 202 136 4</span></span> <span id="cb30-10"><a href="#cb30-10"></a><span class="co">#> # … with 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"></a><span class="kw">group_by</span>(starwars, sex)</span> <span id="cb31-2"><a href="#cb31-2"></a><span class="co">#> # A tibble: 87 x 14</span></span> <span id="cb31-3"><a href="#cb31-3"></a><span class="co">#> # Groups: sex [5]</span></span> <span id="cb31-4"><a href="#cb31-4"></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"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb31-6"><a href="#cb31-6"></a><span class="co">#> 1 Luke… 172 77 blond fair blue 19 male mascu…</span></span> <span id="cb31-7"><a href="#cb31-7"></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"></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"></a><span class="co">#> 4 Dart… 202 136 none white yellow 41.9 male mascu…</span></span> <span id="cb31-10"><a href="#cb31-10"></a><span class="co">#> # … with 83 more rows, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb31-11"><a href="#cb31-11"></a><span class="co">#> # films <list>, vehicles <list>, starships <list></span></span> <span id="cb31-12"><a href="#cb31-12"></a><span class="kw">group_by</span>(starwars, <span class="dt">sex =</span> <span class="kw">as.factor</span>(sex))</span> <span id="cb31-13"><a href="#cb31-13"></a><span class="co">#> # A tibble: 87 x 14</span></span> <span id="cb31-14"><a href="#cb31-14"></a><span class="co">#> # Groups: sex [5]</span></span> <span id="cb31-15"><a href="#cb31-15"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span> <span id="cb31-16"><a href="#cb31-16"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <fct> <chr> </span></span> <span id="cb31-17"><a href="#cb31-17"></a><span class="co">#> 1 Luke… 172 77 blond fair blue 19 male mascu…</span></span> <span id="cb31-18"><a href="#cb31-18"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span> <span id="cb31-19"><a href="#cb31-19"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span> <span id="cb31-20"><a href="#cb31-20"></a><span class="co">#> 4 Dart… 202 136 none white yellow 41.9 male mascu…</span></span> <span id="cb31-21"><a href="#cb31-21"></a><span class="co">#> # … with 83 more rows, and 5 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb31-22"><a href="#cb31-22"></a><span class="co">#> # films <list>, vehicles <list>, starships <list></span></span> <span id="cb31-23"><a href="#cb31-23"></a><span class="kw">group_by</span>(starwars, <span class="dt">height_binned =</span> <span class="kw">cut</span>(height, <span class="dv">3</span>))</span> <span id="cb31-24"><a href="#cb31-24"></a><span class="co">#> # A tibble: 87 x 15</span></span> <span id="cb31-25"><a href="#cb31-25"></a><span class="co">#> # Groups: height_binned [4]</span></span> <span id="cb31-26"><a href="#cb31-26"></a><span class="co">#> name height mass hair_color skin_color eye_color birth_year sex gender</span></span> <span id="cb31-27"><a href="#cb31-27"></a><span class="co">#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> </span></span> <span id="cb31-28"><a href="#cb31-28"></a><span class="co">#> 1 Luke… 172 77 blond fair blue 19 male mascu…</span></span> <span id="cb31-29"><a href="#cb31-29"></a><span class="co">#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…</span></span> <span id="cb31-30"><a href="#cb31-30"></a><span class="co">#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…</span></span> <span id="cb31-31"><a href="#cb31-31"></a><span class="co">#> 4 Dart… 202 136 none white yellow 41.9 male mascu…</span></span> <span id="cb31-32"><a href="#cb31-32"></a><span class="co">#> # … with 83 more rows, and 6 more variables: homeworld <chr>, species <chr>,</span></span> <span id="cb31-33"><a href="#cb31-33"></a><span class="co">#> # films <list>, 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"></a><span class="kw">group_by</span>(df, <span class="st">"month"</span>)</span> <span id="cb32-2"><a href="#cb32-2"></a><span class="co">#> # A tibble: 87 x 4</span></span> <span id="cb32-3"><a href="#cb32-3"></a><span class="co">#> # Groups: "month" [1]</span></span> <span id="cb32-4"><a href="#cb32-4"></a><span class="co">#> name height mass `"month"`</span></span> <span id="cb32-5"><a href="#cb32-5"></a><span class="co">#> <chr> <int> <dbl> <chr> </span></span> <span id="cb32-6"><a href="#cb32-6"></a><span class="co">#> 1 Luke Skywalker 172 77 month </span></span> <span id="cb32-7"><a href="#cb32-7"></a><span class="co">#> 2 C-3PO 167 75 month </span></span> <span id="cb32-8"><a href="#cb32-8"></a><span class="co">#> 3 R2-D2 96 32 month </span></span> <span id="cb32-9"><a href="#cb32-9"></a><span class="co">#> 4 Darth Vader 202 136 month </span></span> <span id="cb32-10"><a href="#cb32-10"></a><span class="co">#> # … with 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>