UNPKG

skimr

Version:

CLI EDA for CSVs

683 lines (647 loc) 43.2 kB
<!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>Two-table verbs</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">Two-table verbs</h1> <p>It’s rare that a data analysis involves only a single table of data. In practice, you’ll normally have many tables that contribute to an analysis, and you need flexible tools to combine them. In dplyr, there are three families of verbs that work with two tables at a time:</p> <ul> <li><p>Mutating joins, which add new variables to one table from matching rows in another.</p></li> <li><p>Filtering joins, which filter observations from one table based on whether or not they match an observation in the other table.</p></li> <li><p>Set operations, which combine the observations in the data sets as if they were set elements.</p></li> </ul> <p>(This discussion assumes that you have <a href="https://www.jstatsoft.org/v59/i10/">tidy data</a>, where the rows are observations and the columns are variables. If you’re not familiar with that framework, I’d recommend reading up on it first.)</p> <p>All two-table verbs work similarly. The first two arguments are <code>x</code> and <code>y</code>, and provide the tables to combine. The output is always a new table with the same type as <code>x</code>.</p> <div id="mutating-joins" class="section level2"> <h2>Mutating joins</h2> <p>Mutating joins allow you to combine variables from multiple tables. For example, consider the flights and airlines data from the nycflights13 package. In one table we have flight information with an abbreviation for carrier, and in another we have a mapping between abbreviations and full names. You can use a join to add the carrier names to the flight data:</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">library</span>(nycflights13)</span> <span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="co"># Drop unimportant variables so it&#39;s easier to understand the join results.</span></span> <span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a>flights2 <span class="ot">&lt;-</span> flights <span class="sc">%&gt;%</span> <span class="fu">select</span>(year<span class="sc">:</span>day, hour, origin, dest, tailnum, carrier)</span> <span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a></span> <span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a>flights2 <span class="sc">%&gt;%</span> </span> <span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a> <span class="fu">left_join</span>(airlines)</span> <span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="co">#&gt; Joining with `by = join_by(carrier)`</span></span> <span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="co">#&gt; # A tibble: 336,776 × 9</span></span> <span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="co">#&gt; year month day hour origin dest tailnum carrier name </span></span> <span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; </span></span> <span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="co">#&gt; 1 2013 1 1 5 EWR IAH N14228 UA United Air Lines Inc. </span></span> <span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a><span class="co">#&gt; 2 2013 1 1 5 LGA IAH N24211 UA United Air Lines Inc. </span></span> <span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a><span class="co">#&gt; 3 2013 1 1 5 JFK MIA N619AA AA American Airlines Inc.</span></span> <span id="cb1-14"><a href="#cb1-14" tabindex="-1"></a><span class="co">#&gt; 4 2013 1 1 5 JFK BQN N804JB B6 JetBlue Airways </span></span> <span id="cb1-15"><a href="#cb1-15" tabindex="-1"></a><span class="co">#&gt; 5 2013 1 1 6 LGA ATL N668DN DL Delta Air Lines Inc. </span></span> <span id="cb1-16"><a href="#cb1-16" tabindex="-1"></a><span class="co">#&gt; # ℹ 336,771 more rows</span></span></code></pre></div> <div id="controlling-how-the-tables-are-matched" class="section level3"> <h3>Controlling how the tables are matched</h3> <p>As well as <code>x</code> and <code>y</code>, each mutating join takes an argument <code>by</code> that controls which variables are used to match observations in the two tables. There are a few ways to specify it, as I illustrate below with various tables from nycflights13:</p> <ul> <li><p><code>NULL</code>, the default. dplyr will will use all variables that appear in both tables, a <strong>natural</strong> join. For example, the flights and weather tables match on their common variables: year, month, day, hour and origin.</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>flights2 <span class="sc">%&gt;%</span> <span class="fu">left_join</span>(weather)</span> <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="co">#&gt; Joining with `by = join_by(year, month, day, hour, origin)`</span></span> <span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="co">#&gt; # A tibble: 336,776 × 18</span></span> <span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co">#&gt; year month day hour origin dest tailnum carrier temp dewp humid</span></span> <span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span> <span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co">#&gt; 1 2013 1 1 5 EWR IAH N14228 UA 39.0 28.0 64.4</span></span> <span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co">#&gt; 2 2013 1 1 5 LGA IAH N24211 UA 39.9 25.0 54.8</span></span> <span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co">#&gt; 3 2013 1 1 5 JFK MIA N619AA AA 39.0 27.0 61.6</span></span> <span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co">#&gt; 4 2013 1 1 5 JFK BQN N804JB B6 39.0 27.0 61.6</span></span> <span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="co">#&gt; 5 2013 1 1 6 LGA ATL N668DN DL 39.9 25.0 54.8</span></span> <span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a><span class="co">#&gt; # ℹ 336,771 more rows</span></span> <span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a><span class="co">#&gt; # ℹ 7 more variables: wind_dir &lt;dbl&gt;, wind_speed &lt;dbl&gt;, wind_gust &lt;dbl&gt;,</span></span> <span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a><span class="co">#&gt; # precip &lt;dbl&gt;, pressure &lt;dbl&gt;, visib &lt;dbl&gt;, time_hour &lt;dttm&gt;</span></span></code></pre></div></li> <li><p>A character vector, <code>by = &quot;x&quot;</code>. Like a natural join, but uses only some of the common variables. For example, <code>flights</code> and <code>planes</code> have <code>year</code> columns, but they mean different things so we only want to join by <code>tailnum</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>flights2 <span class="sc">%&gt;%</span> <span class="fu">left_join</span>(planes, <span class="at">by =</span> <span class="st">&quot;tailnum&quot;</span>)</span> <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="co">#&gt; # A tibble: 336,776 × 16</span></span> <span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#&gt; year.x month day hour origin dest tailnum carrier year.y type </span></span> <span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;chr&gt; </span></span> <span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#&gt; 1 2013 1 1 5 EWR IAH N14228 UA 1999 Fixed wing multi…</span></span> <span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#&gt; 2 2013 1 1 5 LGA IAH N24211 UA 1998 Fixed wing multi…</span></span> <span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co">#&gt; 3 2013 1 1 5 JFK MIA N619AA AA 1990 Fixed wing multi…</span></span> <span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#&gt; 4 2013 1 1 5 JFK BQN N804JB B6 2012 Fixed wing multi…</span></span> <span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co">#&gt; 5 2013 1 1 6 LGA ATL N668DN DL 1991 Fixed wing multi…</span></span> <span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co">#&gt; # ℹ 336,771 more rows</span></span> <span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="co">#&gt; # ℹ 6 more variables: manufacturer &lt;chr&gt;, model &lt;chr&gt;, engines &lt;int&gt;,</span></span> <span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a><span class="co">#&gt; # seats &lt;int&gt;, speed &lt;int&gt;, engine &lt;chr&gt;</span></span></code></pre></div> <p>Note that the year columns in the output are disambiguated with a suffix.</p></li> <li><p>A named character vector: <code>by = c(&quot;x&quot; = &quot;a&quot;)</code>. This will match variable <code>x</code> in table <code>x</code> to variable <code>a</code> in table <code>y</code>. The variables from use will be used in the output.</p> <p>Each flight has an origin and destination <code>airport</code>, so we need to specify which one we want to join to:</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>flights2 <span class="sc">%&gt;%</span> <span class="fu">left_join</span>(airports, <span class="fu">c</span>(<span class="st">&quot;dest&quot;</span> <span class="ot">=</span> <span class="st">&quot;faa&quot;</span>))</span> <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="co">#&gt; # A tibble: 336,776 × 15</span></span> <span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#&gt; year month day hour origin dest tailnum carrier name lat lon alt</span></span> <span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span> <span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#&gt; 1 2013 1 1 5 EWR IAH N14228 UA George… 30.0 -95.3 97</span></span> <span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#&gt; 2 2013 1 1 5 LGA IAH N24211 UA George… 30.0 -95.3 97</span></span> <span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#&gt; 3 2013 1 1 5 JFK MIA N619AA AA Miami … 25.8 -80.3 8</span></span> <span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#&gt; 4 2013 1 1 5 JFK BQN N804JB B6 &lt;NA&gt; NA NA NA</span></span> <span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#&gt; 5 2013 1 1 6 LGA ATL N668DN DL Hartsf… 33.6 -84.4 1026</span></span> <span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#&gt; # ℹ 336,771 more rows</span></span> <span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#&gt; # ℹ 3 more variables: tz &lt;dbl&gt;, dst &lt;chr&gt;, tzone &lt;chr&gt;</span></span> <span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a>flights2 <span class="sc">%&gt;%</span> <span class="fu">left_join</span>(airports, <span class="fu">c</span>(<span class="st">&quot;origin&quot;</span> <span class="ot">=</span> <span class="st">&quot;faa&quot;</span>))</span> <span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#&gt; # A tibble: 336,776 × 15</span></span> <span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="co">#&gt; year month day hour origin dest tailnum carrier name lat lon alt</span></span> <span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span> <span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a><span class="co">#&gt; 1 2013 1 1 5 EWR IAH N14228 UA Newark… 40.7 -74.2 18</span></span> <span id="cb4-17"><a href="#cb4-17" tabindex="-1"></a><span class="co">#&gt; 2 2013 1 1 5 LGA IAH N24211 UA La Gua… 40.8 -73.9 22</span></span> <span id="cb4-18"><a href="#cb4-18" tabindex="-1"></a><span class="co">#&gt; 3 2013 1 1 5 JFK MIA N619AA AA John F… 40.6 -73.8 13</span></span> <span id="cb4-19"><a href="#cb4-19" tabindex="-1"></a><span class="co">#&gt; 4 2013 1 1 5 JFK BQN N804JB B6 John F… 40.6 -73.8 13</span></span> <span id="cb4-20"><a href="#cb4-20" tabindex="-1"></a><span class="co">#&gt; 5 2013 1 1 6 LGA ATL N668DN DL La Gua… 40.8 -73.9 22</span></span> <span id="cb4-21"><a href="#cb4-21" tabindex="-1"></a><span class="co">#&gt; # ℹ 336,771 more rows</span></span> <span id="cb4-22"><a href="#cb4-22" tabindex="-1"></a><span class="co">#&gt; # ℹ 3 more variables: tz &lt;dbl&gt;, dst &lt;chr&gt;, tzone &lt;chr&gt;</span></span></code></pre></div></li> </ul> </div> <div id="types-of-join" class="section level3"> <h3>Types of join</h3> <p>There are four types of mutating join, which differ in their behaviour when a match is not found. We’ll illustrate each with a simple example:</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>df1 <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>), <span class="at">y =</span> <span class="dv">2</span><span class="sc">:</span><span class="dv">1</span>)</span> <span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a>df2 <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">3</span>, <span class="dv">1</span>), <span class="at">a =</span> <span class="dv">10</span>, <span class="at">b =</span> <span class="st">&quot;a&quot;</span>)</span></code></pre></div> <ul> <li><p><code>inner_join(x, y)</code> only includes observations that match in both <code>x</code> and <code>y</code>.</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>df1 <span class="sc">%&gt;%</span> <span class="fu">inner_join</span>(df2) <span class="sc">%&gt;%</span> knitr<span class="sc">::</span><span class="fu">kable</span>()</span> <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#&gt; Joining with `by = join_by(x)`</span></span></code></pre></div> <table> <thead> <tr class="header"> <th align="right">x</th> <th align="right">y</th> <th align="right">a</th> <th align="left">b</th> </tr> </thead> <tbody> <tr class="odd"> <td align="right">1</td> <td align="right">2</td> <td align="right">10</td> <td align="left">a</td> </tr> </tbody> </table></li> <li><p><code>left_join(x, y)</code> includes all observations in <code>x</code>, regardless of whether they match or not. This is the most commonly used join because it ensures that you don’t lose observations from your primary table.</p> <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>df1 <span class="sc">%&gt;%</span> <span class="fu">left_join</span>(df2)</span> <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co">#&gt; Joining with `by = join_by(x)`</span></span> <span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 × 4</span></span> <span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co">#&gt; x y a b </span></span> <span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;</span></span> <span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#&gt; 1 1 2 10 a </span></span> <span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#&gt; 2 2 1 NA &lt;NA&gt;</span></span></code></pre></div></li> <li><p><code>right_join(x, y)</code> includes all observations in <code>y</code>. It’s equivalent to <code>left_join(y, x)</code>, but the columns and rows will be ordered differently.</p> <div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>df1 <span class="sc">%&gt;%</span> <span class="fu">right_join</span>(df2)</span> <span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="co">#&gt; Joining with `by = join_by(x)`</span></span> <span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 × 4</span></span> <span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="co">#&gt; x y a b </span></span> <span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co">#&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;</span></span> <span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="co">#&gt; 1 1 2 10 a </span></span> <span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co">#&gt; 2 3 NA 10 a</span></span> <span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a>df2 <span class="sc">%&gt;%</span> <span class="fu">left_join</span>(df1)</span> <span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a><span class="co">#&gt; Joining with `by = join_by(x)`</span></span> <span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 × 4</span></span> <span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a><span class="co">#&gt; x a b y</span></span> <span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a><span class="co">#&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;int&gt;</span></span> <span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a><span class="co">#&gt; 1 3 10 a NA</span></span> <span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a><span class="co">#&gt; 2 1 10 a 2</span></span></code></pre></div></li> <li><p><code>full_join()</code> includes all observations from <code>x</code> and <code>y</code>.</p> <div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>df1 <span class="sc">%&gt;%</span> <span class="fu">full_join</span>(df2)</span> <span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a><span class="co">#&gt; Joining with `by = join_by(x)`</span></span> <span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 × 4</span></span> <span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="co">#&gt; x y a b </span></span> <span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;</span></span> <span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co">#&gt; 1 1 2 10 a </span></span> <span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="co">#&gt; 2 2 1 NA &lt;NA&gt; </span></span> <span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#&gt; 3 3 NA 10 a</span></span></code></pre></div></li> </ul> <p>The left, right and full joins are collectively know as <strong>outer joins</strong>. When a row doesn’t match in an outer join, the new variables are filled in with missing values.</p> </div> <div id="observations" class="section level3"> <h3>Observations</h3> <p>While mutating joins are primarily used to add new variables, they can also generate new observations. If a match is not unique, a join will add all possible combinations (the Cartesian product) of the matching observations:</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>df1 <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">2</span>), <span class="at">y =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>)</span> <span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a>df2 <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">2</span>), <span class="at">z =</span> <span class="fu">c</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;a&quot;</span>))</span> <span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a></span> <span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a>df1 <span class="sc">%&gt;%</span> <span class="fu">left_join</span>(df2)</span> <span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#&gt; Joining with `by = join_by(x)`</span></span> <span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co">#&gt; Warning in left_join(., df2): Detected an unexpected many-to-many relationship between `x` and `y`.</span></span> <span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#&gt; ℹ Row 1 of `x` matches multiple rows in `y`.</span></span> <span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co">#&gt; ℹ Row 1 of `y` matches multiple rows in `x`.</span></span> <span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a><span class="co">#&gt; ℹ If a many-to-many relationship is expected, set `relationship =</span></span> <span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="co">#&gt; &quot;many-to-many&quot;` to silence this warning.</span></span> <span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 × 3</span></span> <span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a><span class="co">#&gt; x y z </span></span> <span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a><span class="co">#&gt; &lt;dbl&gt; &lt;int&gt; &lt;chr&gt;</span></span> <span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a><span class="co">#&gt; 1 1 1 a </span></span> <span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a><span class="co">#&gt; 2 1 1 b </span></span> <span id="cb10-16"><a href="#cb10-16" tabindex="-1"></a><span class="co">#&gt; 3 1 2 a </span></span> <span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a><span class="co">#&gt; 4 1 2 b </span></span> <span id="cb10-18"><a href="#cb10-18" tabindex="-1"></a><span class="co">#&gt; 5 2 3 a</span></span></code></pre></div> </div> </div> <div id="filtering-joins" class="section level2"> <h2>Filtering joins</h2> <p>Filtering joins match observations in the same way as mutating joins, but affect the observations, not the variables. There are two types:</p> <ul> <li><code>semi_join(x, y)</code> <strong>keeps</strong> all observations in <code>x</code> that have a match in <code>y</code>.</li> <li><code>anti_join(x, y)</code> <strong>drops</strong> all observations in <code>x</code> that have a match in <code>y</code>.</li> </ul> <p>These are most useful for diagnosing join mismatches. For example, there are many flights in the nycflights13 dataset that don’t have a matching tail number in the planes table:</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><span class="fu">library</span>(<span class="st">&quot;nycflights13&quot;</span>)</span> <span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a>flights <span class="sc">%&gt;%</span> </span> <span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a> <span class="fu">anti_join</span>(planes, <span class="at">by =</span> <span class="st">&quot;tailnum&quot;</span>) <span class="sc">%&gt;%</span> </span> <span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a> <span class="fu">count</span>(tailnum, <span class="at">sort =</span> <span class="cn">TRUE</span>)</span> <span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="co">#&gt; # A tibble: 722 × 2</span></span> <span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a><span class="co">#&gt; tailnum n</span></span> <span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co">#&gt; &lt;chr&gt; &lt;int&gt;</span></span> <span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a><span class="co">#&gt; 1 &lt;NA&gt; 2512</span></span> <span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="co">#&gt; 2 N725MQ 575</span></span> <span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="co">#&gt; 3 N722MQ 513</span></span> <span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a><span class="co">#&gt; 4 N723MQ 507</span></span> <span id="cb11-12"><a href="#cb11-12" tabindex="-1"></a><span class="co">#&gt; 5 N713MQ 483</span></span> <span id="cb11-13"><a href="#cb11-13" tabindex="-1"></a><span class="co">#&gt; # ℹ 717 more rows</span></span></code></pre></div> <p>If you’re worried about what observations your joins will match, start with a <code>semi_join()</code> or <code>anti_join()</code>. <code>semi_join()</code> and <code>anti_join()</code> never duplicate; they only ever remove observations.</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>df1 <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">4</span>), <span class="at">y =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>)</span> <span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a>df2 <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">2</span>), <span class="at">z =</span> <span class="fu">c</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;b&quot;</span>, <span class="st">&quot;a&quot;</span>))</span> <span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a></span> <span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="co"># Four rows to start with:</span></span> <span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a>df1 <span class="sc">%&gt;%</span> <span class="fu">nrow</span>()</span> <span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#&gt; [1] 4</span></span> <span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co"># And we get four rows after the join</span></span> <span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a>df1 <span class="sc">%&gt;%</span> <span class="fu">inner_join</span>(df2, <span class="at">by =</span> <span class="st">&quot;x&quot;</span>) <span class="sc">%&gt;%</span> <span class="fu">nrow</span>()</span> <span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a><span class="co">#&gt; Warning in inner_join(., df2, by = &quot;x&quot;): Detected an unexpected many-to-many relationship between `x` and `y`.</span></span> <span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="co">#&gt; ℹ Row 1 of `x` matches multiple rows in `y`.</span></span> <span id="cb12-11"><a href="#cb12-11" tabindex="-1"></a><span class="co">#&gt; ℹ Row 1 of `y` matches multiple rows in `x`.</span></span> <span id="cb12-12"><a href="#cb12-12" tabindex="-1"></a><span class="co">#&gt; ℹ If a many-to-many relationship is expected, set `relationship =</span></span> <span id="cb12-13"><a href="#cb12-13" tabindex="-1"></a><span class="co">#&gt; &quot;many-to-many&quot;` to silence this warning.</span></span> <span id="cb12-14"><a href="#cb12-14" tabindex="-1"></a><span class="co">#&gt; [1] 4</span></span> <span id="cb12-15"><a href="#cb12-15" tabindex="-1"></a><span class="co"># But only two rows actually match</span></span> <span id="cb12-16"><a href="#cb12-16" tabindex="-1"></a>df1 <span class="sc">%&gt;%</span> <span class="fu">semi_join</span>(df2, <span class="at">by =</span> <span class="st">&quot;x&quot;</span>) <span class="sc">%&gt;%</span> <span class="fu">nrow</span>()</span> <span id="cb12-17"><a href="#cb12-17" tabindex="-1"></a><span class="co">#&gt; [1] 2</span></span></code></pre></div> </div> <div id="set-operations" class="section level2"> <h2>Set operations</h2> <p>The final type of two-table verb is set operations. These expect the <code>x</code> and <code>y</code> inputs to have the same variables, and treat the observations like sets:</p> <ul> <li><code>intersect(x, y)</code>: return only observations in both <code>x</code> and <code>y</code></li> <li><code>union(x, y)</code>: return unique observations in <code>x</code> and <code>y</code></li> <li><code>setdiff(x, y)</code>: return observations in <code>x</code>, but not in <code>y</code>.</li> </ul> <p>Given this simple data:</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>(df1 <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="at">y =</span> <span class="fu">c</span>(1L, 1L)))</span> <span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 × 2</span></span> <span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="co">#&gt; x y</span></span> <span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt;</span></span> <span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#&gt; 1 1 1</span></span> <span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co">#&gt; 2 2 1</span></span> <span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a>(df2 <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="at">y =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>))</span> <span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 × 2</span></span> <span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a><span class="co">#&gt; x y</span></span> <span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt;</span></span> <span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a><span class="co">#&gt; 1 1 1</span></span> <span id="cb13-12"><a href="#cb13-12" tabindex="-1"></a><span class="co">#&gt; 2 2 2</span></span></code></pre></div> <p>The four possibilities are:</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><span class="fu">intersect</span>(df1, df2)</span> <span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="co">#&gt; # A tibble: 1 × 2</span></span> <span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="co">#&gt; x y</span></span> <span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt;</span></span> <span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#&gt; 1 1 1</span></span> <span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="co"># Note that we get 3 rows, not 4</span></span> <span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="fu">union</span>(df1, df2)</span> <span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 × 2</span></span> <span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="co">#&gt; x y</span></span> <span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt;</span></span> <span id="cb14-11"><a href="#cb14-11" tabindex="-1"></a><span class="co">#&gt; 1 1 1</span></span> <span id="cb14-12"><a href="#cb14-12" tabindex="-1"></a><span class="co">#&gt; 2 2 1</span></span> <span id="cb14-13"><a href="#cb14-13" tabindex="-1"></a><span class="co">#&gt; 3 2 2</span></span> <span id="cb14-14"><a href="#cb14-14" tabindex="-1"></a><span class="fu">setdiff</span>(df1, df2)</span> <span id="cb14-15"><a href="#cb14-15" tabindex="-1"></a><span class="co">#&gt; # A tibble: 1 × 2</span></span> <span id="cb14-16"><a href="#cb14-16" tabindex="-1"></a><span class="co">#&gt; x y</span></span> <span id="cb14-17"><a href="#cb14-17" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt;</span></span> <span id="cb14-18"><a href="#cb14-18" tabindex="-1"></a><span class="co">#&gt; 1 2 1</span></span> <span id="cb14-19"><a href="#cb14-19" tabindex="-1"></a><span class="fu">setdiff</span>(df2, df1)</span> <span id="cb14-20"><a href="#cb14-20" tabindex="-1"></a><span class="co">#&gt; # A tibble: 1 × 2</span></span> <span id="cb14-21"><a href="#cb14-21" tabindex="-1"></a><span class="co">#&gt; x y</span></span> <span id="cb14-22"><a href="#cb14-22" tabindex="-1"></a><span class="co">#&gt; &lt;int&gt; &lt;int&gt;</span></span> <span id="cb14-23"><a href="#cb14-23" tabindex="-1"></a><span class="co">#&gt; 1 2 2</span></span></code></pre></div> </div> <div id="multiple-table-verbs" class="section level2"> <h2>Multiple-table verbs</h2> <p>dplyr does not provide any functions for working with three or more tables. Instead use <code>purrr::reduce()</code> or <code>Reduce()</code>, as described in <a href="http://adv-r.had.co.nz/Functionals.html#functionals-fp">Advanced R</a>, to iteratively combine the two-table verbs to handle as many tables as you need.</p> </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>