skimr
Version:
CLI EDA for CSVs
705 lines (676 loc) • 83.3 kB
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>// 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 { display: inline-block; 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 Cordé 157 NA brown light brown NA fema… femin…</span></span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co">#> 4 Dormé 165 NA brown light brown NA fema… femin…</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 Dud Bolt 94 45 none blue, grey yellow NA male mascu…</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 Shaak Ti 178 57 none red, blue,… black NA fema… femin…</span></span>
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="co">#> 4 Dormé 165 NA brown light brown NA 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 Dexter J… 198 102 none brown yellow NA male mascu…</span></span>
<span id="cb8-18"><a href="#cb8-18" tabindex="-1"></a><span class="co">#> 3 R4-P17 96 NA none silver, r… red, blue NA none femin…</span></span>
<span id="cb8-19"><a href="#cb8-19" tabindex="-1"></a><span class="co">#> 4 Lama Su 229 88 none grey black NA male mascu…</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">%&