1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
|
<!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>Rectangling</title>
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
}
});
</script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; }
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.at { color: #7d9029; }
code span.bn { color: #40a070; }
code span.bu { color: #008000; }
code span.cf { color: #007020; font-weight: bold; }
code span.ch { color: #4070a0; }
code span.cn { color: #880000; }
code span.co { color: #60a0b0; font-style: italic; }
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.do { color: #ba2121; font-style: italic; }
code span.dt { color: #902000; }
code span.dv { color: #40a070; }
code span.er { color: #ff0000; font-weight: bold; }
code span.ex { }
code span.fl { color: #40a070; }
code span.fu { color: #06287e; }
code span.im { color: #008000; font-weight: bold; }
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.kw { color: #007020; font-weight: bold; }
code span.op { color: #666666; }
code span.ot { color: #007020; }
code span.pp { color: #bc7a00; }
code span.sc { color: #4070a0; }
code span.ss { color: #bb6688; }
code span.st { color: #4070a0; }
code span.va { color: #19177c; }
code span.vs { color: #4070a0; }
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
var j = 0;
while (j < rules.length) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
j++;
continue;
}
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') {
j++;
continue;
}
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap;
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }
code > span.kw { color: #555; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #d14; }
code > span.fl { color: #d14; }
code > span.ch { color: #d14; }
code > span.st { color: #d14; }
code > span.co { color: #888888; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #900; font-weight: bold; }
code > span.er { color: #a61717; background-color: #e3d2d2; }
</style>
</head>
<body>
<h1 class="title toc-ignore">Rectangling</h1>
<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p>Rectangling is the art and craft of taking a deeply nested list
(often sourced from wild caught JSON or XML) and taming it into a tidy
data set of rows and columns. There are three functions from tidyr that
are particularly useful for rectangling:</p>
<ul>
<li><code>unnest_longer()</code> takes each element of a list-column and
makes a new row.</li>
<li><code>unnest_wider()</code> takes each element of a list-column and
makes a new column.</li>
<li><code>hoist()</code> is similar to <code>unnest_wider()</code> but
only plucks out selected components, and can reach down multiple
levels.</li>
</ul>
<p>(Alternative, for complex inputs where you need to rectangle a nested
list according to a specification, see the <a href="https://github.com/mgirlich/tibblify">tibblify</a> package.)</p>
<p>A very large number of data rectangling problems can be solved by
combining <code>jsonlite::read_json()</code> with these functions and a
splash of dplyr (largely eliminating prior approaches that combined
<code>mutate()</code> with multiple <code>purrr::map()</code>s). Note
that jsonlite has another important function called
<code>fromJSON()</code>. We don’t recommend it here because it performs
its own automatic simplification (<code>simplifyVector = TRUE</code>).
This often works well, particularly in simple cases, but we think you’re
better off doing the rectangling yourself so you know exactly what’s
happening and can more easily handle the most complicated nested
structures.</p>
<p>To illustrate these techniques, we’ll use the repurrrsive package,
which provides a number deeply nested lists originally mostly captured
from web APIs.</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>(tidyr)</span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="fu">library</span>(repurrrsive)</span></code></pre></div>
</div>
<div id="github-users" class="section level2">
<h2>GitHub users</h2>
<p>We’ll start with <code>gh_users</code>, a list which contains
information about six GitHub users. To begin, we put the
<code>gh_users</code> list into a data frame:</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>users <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="at">user =</span> gh_users)</span></code></pre></div>
<p>This seems a bit counter-intuitive: why is the first step in making a
list simpler to make it more complicated? But a data frame has a big
advantage: it bundles together multiple vectors so that everything is
tracked together in a single object.</p>
<p>Each <code>user</code> is a named list, where each element represents
a column.</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><span class="fu">names</span>(users<span class="sc">$</span>user[[<span class="dv">1</span>]])</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="co">#> [1] "login" "id" "avatar_url" </span></span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#> [4] "gravatar_id" "url" "html_url" </span></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#> [7] "followers_url" "following_url" "gists_url" </span></span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#> [10] "starred_url" "subscriptions_url" "organizations_url" </span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#> [13] "repos_url" "events_url" "received_events_url"</span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co">#> [16] "type" "site_admin" "name" </span></span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#> [19] "company" "blog" "location" </span></span>
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co">#> [22] "email" "hireable" "bio" </span></span>
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co">#> [25] "public_repos" "public_gists" "followers" </span></span>
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="co">#> [28] "following" "created_at" "updated_at"</span></span></code></pre></div>
<p>There are two ways to turn the list components into columns.
<code>unnest_wider()</code> takes every component and makes a new
column:</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>users <span class="sc">%>%</span> <span class="fu">unnest_wider</span>(user)</span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 30</span></span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#> login id avatar_url gravatar_id url html_url followers_url following_url</span></span>
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="co">#> <chr> <int> <chr> <chr> <chr> <chr> <chr> <chr> </span></span>
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#> 1 gabo… 6.60e5 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#> 2 jenn… 5.99e5 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#> 3 jtle… 1.57e6 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#> 4 juli… 1.25e7 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#> 5 leep… 3.51e6 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#> 6 masa… 8.36e6 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#> # ℹ 22 more variables: gists_url <chr>, starred_url <chr>,</span></span>
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a><span class="co">#> # subscriptions_url <chr>, organizations_url <chr>, repos_url <chr>,</span></span>
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#> # events_url <chr>, received_events_url <chr>, type <chr>, site_admin <lgl>,</span></span>
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="co">#> # name <chr>, company <chr>, blog <chr>, location <chr>, email <chr>,</span></span>
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="co">#> # hireable <lgl>, bio <chr>, public_repos <int>, public_gists <int>,</span></span>
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a><span class="co">#> # followers <int>, following <int>, created_at <chr>, updated_at <chr></span></span></code></pre></div>
<p>But in this case, there are many components and we don’t need most of
them so we can instead use <code>hoist()</code>. <code>hoist()</code>
allows us to pull out selected components using the same syntax as
<code>purrr::pluck()</code>:</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>users <span class="sc">%>%</span> <span class="fu">hoist</span>(user, </span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a> <span class="at">followers =</span> <span class="st">"followers"</span>, </span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a> <span class="at">login =</span> <span class="st">"login"</span>, </span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a> <span class="at">url =</span> <span class="st">"html_url"</span></span>
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a>)</span>
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 4</span></span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#> followers login url user </span></span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#> <int> <chr> <chr> <list> </span></span>
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#> 1 303 gaborcsardi https://github.com/gaborcsardi <named list [27]></span></span>
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co">#> 2 780 jennybc https://github.com/jennybc <named list [27]></span></span>
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co">#> 3 3958 jtleek https://github.com/jtleek <named list [27]></span></span>
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="co">#> 4 115 juliasilge https://github.com/juliasilge <named list [27]></span></span>
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a><span class="co">#> 5 213 leeper https://github.com/leeper <named list [27]></span></span>
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a><span class="co">#> 6 34 masalmon https://github.com/masalmon <named list [27]></span></span></code></pre></div>
<p><code>hoist()</code> removes the named components from the
<code>user</code> list-column, so you can think of it as moving
components out of the inner list into the top-level data frame.</p>
</div>
<div id="github-repos" class="section level2">
<h2>GitHub repos</h2>
<p>We start off <code>gh_repos</code> similarly, by putting it in a
tibble:</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>repos <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="at">repo =</span> gh_repos)</span>
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a>repos</span>
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#> # A tibble: 6 × 1</span></span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#> repo </span></span>
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="co">#> <list> </span></span>
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co">#> 1 <list [30]></span></span>
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co">#> 2 <list [30]></span></span>
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#> 3 <list [30]></span></span>
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="co">#> 4 <list [26]></span></span>
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#> 5 <list [30]></span></span>
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a><span class="co">#> 6 <list [30]></span></span></code></pre></div>
<p>This time the elements of <code>repos</code> are a list of
repositories that belong to that user. These are observations, so should
become new rows, so we use <code>unnest_longer()</code> rather than
<code>unnest_wider()</code>:</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>repos <span class="ot"><-</span> repos <span class="sc">%>%</span> <span class="fu">unnest_longer</span>(repo)</span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a>repos</span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co">#> # A tibble: 176 × 1</span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co">#> repo </span></span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#> <list> </span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#> 1 <named list [68]></span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#> 2 <named list [68]></span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co">#> 3 <named list [68]></span></span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co">#> 4 <named list [68]></span></span>
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="co">#> 5 <named list [68]></span></span>
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="co">#> 6 <named list [68]></span></span>
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a><span class="co">#> # ℹ 170 more rows</span></span></code></pre></div>
<p>Then we can use <code>unnest_wider()</code> or
<code>hoist()</code>:</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>repos <span class="sc">%>%</span> <span class="fu">hoist</span>(repo, </span>
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> <span class="at">login =</span> <span class="fu">c</span>(<span class="st">"owner"</span>, <span class="st">"login"</span>), </span>
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a> <span class="at">name =</span> <span class="st">"name"</span>,</span>
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a> <span class="at">homepage =</span> <span class="st">"homepage"</span>,</span>
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a> <span class="at">watchers =</span> <span class="st">"watchers_count"</span></span>
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a>)</span>
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co">#> # A tibble: 176 × 5</span></span>
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="co">#> login name homepage watchers repo </span></span>
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a><span class="co">#> <chr> <chr> <chr> <int> <list> </span></span>
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="co">#> 1 gaborcsardi after <NA> 5 <named list [65]></span></span>
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a><span class="co">#> 2 gaborcsardi argufy <NA> 19 <named list [65]></span></span>
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a><span class="co">#> 3 gaborcsardi ask <NA> 5 <named list [65]></span></span>
<span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a><span class="co">#> 4 gaborcsardi baseimports <NA> 0 <named list [65]></span></span>
<span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a><span class="co">#> 5 gaborcsardi citest <NA> 0 <named list [65]></span></span>
<span id="cb8-15"><a href="#cb8-15" tabindex="-1"></a><span class="co">#> 6 gaborcsardi clisymbols "" 18 <named list [65]></span></span>
<span id="cb8-16"><a href="#cb8-16" tabindex="-1"></a><span class="co">#> # ℹ 170 more rows</span></span></code></pre></div>
<p>Note the use of <code>c("owner", "login")</code>: this allows us to
reach two levels deep inside of a list. An alternative approach would be
to pull out just <code>owner</code> and then put each element of it in a
column:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>repos <span class="sc">%>%</span> </span>
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> <span class="fu">hoist</span>(repo, <span class="at">owner =</span> <span class="st">"owner"</span>) <span class="sc">%>%</span> </span>
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> <span class="fu">unnest_wider</span>(owner)</span>
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="co">#> # A tibble: 176 × 18</span></span>
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#> login id avatar_url gravatar_id url html_url followers_url following_url</span></span>
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co">#> <chr> <int> <chr> <chr> <chr> <chr> <chr> <chr> </span></span>
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="co">#> 1 gabo… 660288 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#> 2 gabo… 660288 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co">#> 3 gabo… 660288 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a><span class="co">#> 4 gabo… 660288 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a><span class="co">#> 5 gabo… 660288 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a><span class="co">#> 6 gabo… 660288 https://a… "" http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a><span class="co">#> # ℹ 170 more rows</span></span>
<span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a><span class="co">#> # ℹ 10 more variables: gists_url <chr>, starred_url <chr>,</span></span>
<span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a><span class="co">#> # subscriptions_url <chr>, organizations_url <chr>, repos_url <chr>,</span></span>
<span id="cb9-16"><a href="#cb9-16" tabindex="-1"></a><span class="co">#> # events_url <chr>, received_events_url <chr>, type <chr>, site_admin <lgl>,</span></span>
<span id="cb9-17"><a href="#cb9-17" tabindex="-1"></a><span class="co">#> # repo <list></span></span></code></pre></div>
</div>
<div id="game-of-thrones-characters" class="section level2">
<h2>Game of Thrones characters</h2>
<p><code>got_chars</code> has a similar structure to
<code>gh_users</code>: it’s a list of named lists, where each element of
the inner list describes some attribute of a GoT character. We start in
the same way, first by creating a data frame and then by unnesting each
component into a column:</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>chars <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="at">char =</span> got_chars)</span>
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a>chars</span>
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="co">#> # A tibble: 30 × 1</span></span>
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="co">#> char </span></span>
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#> <list> </span></span>
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co">#> 1 <named list [18]></span></span>
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#> 2 <named list [18]></span></span>
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co">#> 3 <named list [18]></span></span>
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a><span class="co">#> 4 <named list [18]></span></span>
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="co">#> 5 <named list [18]></span></span>
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co">#> 6 <named list [18]></span></span>
<span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a><span class="co">#> # ℹ 24 more rows</span></span>
<span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a></span>
<span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a>chars2 <span class="ot"><-</span> chars <span class="sc">%>%</span> <span class="fu">unnest_wider</span>(char)</span>
<span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a>chars2</span>
<span id="cb10-16"><a href="#cb10-16" tabindex="-1"></a><span class="co">#> # A tibble: 30 × 18</span></span>
<span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a><span class="co">#> url id name gender culture born died alive titles aliases father</span></span>
<span id="cb10-18"><a href="#cb10-18" tabindex="-1"></a><span class="co">#> <chr> <int> <chr> <chr> <chr> <chr> <chr> <lgl> <list> <list> <chr> </span></span>
<span id="cb10-19"><a href="#cb10-19" tabindex="-1"></a><span class="co">#> 1 https://ww… 1022 Theo… Male "Ironb… "In … "" TRUE <chr> <chr> "" </span></span>
<span id="cb10-20"><a href="#cb10-20" tabindex="-1"></a><span class="co">#> 2 https://ww… 1052 Tyri… Male "" "In … "" TRUE <chr> <chr> "" </span></span>
<span id="cb10-21"><a href="#cb10-21" tabindex="-1"></a><span class="co">#> 3 https://ww… 1074 Vict… Male "Ironb… "In … "" TRUE <chr> <chr> "" </span></span>
<span id="cb10-22"><a href="#cb10-22" tabindex="-1"></a><span class="co">#> 4 https://ww… 1109 Will Male "" "" "In … FALSE <chr> <chr> "" </span></span>
<span id="cb10-23"><a href="#cb10-23" tabindex="-1"></a><span class="co">#> 5 https://ww… 1166 Areo… Male "Norvo… "In … "" TRUE <chr> <chr> "" </span></span>
<span id="cb10-24"><a href="#cb10-24" tabindex="-1"></a><span class="co">#> 6 https://ww… 1267 Chett Male "" "At … "In … FALSE <chr> <chr> "" </span></span>
<span id="cb10-25"><a href="#cb10-25" tabindex="-1"></a><span class="co">#> # ℹ 24 more rows</span></span>
<span id="cb10-26"><a href="#cb10-26" tabindex="-1"></a><span class="co">#> # ℹ 7 more variables: mother <chr>, spouse <chr>, allegiances <list>,</span></span>
<span id="cb10-27"><a href="#cb10-27" tabindex="-1"></a><span class="co">#> # books <list>, povBooks <list>, tvSeries <list>, playedBy <list></span></span></code></pre></div>
<p>This is more complex than <code>gh_users</code> because some
component of <code>char</code> are themselves a list, giving us a
collection of list-columns:</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>chars2 <span class="sc">%>%</span> <span class="fu">select_if</span>(is.list)</span>
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="co">#> # A tibble: 30 × 7</span></span>
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co">#> titles aliases allegiances books povBooks tvSeries playedBy </span></span>
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="co">#> <list> <list> <list> <list> <list> <list> <list> </span></span>
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="co">#> 1 <chr [2]> <chr [4]> <chr [1]> <chr [3]> <chr [2]> <chr [6]> <chr [1]></span></span>
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a><span class="co">#> 2 <chr [2]> <chr [11]> <chr [1]> <chr [2]> <chr [4]> <chr [6]> <chr [1]></span></span>
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co">#> 3 <chr [2]> <chr [1]> <chr [1]> <chr [3]> <chr [2]> <chr [1]> <chr [1]></span></span>
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a><span class="co">#> 4 <chr [1]> <chr [1]> <NULL> <chr [1]> <chr [1]> <chr [1]> <chr [1]></span></span>
<span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="co">#> 5 <chr [1]> <chr [1]> <chr [1]> <chr [3]> <chr [2]> <chr [2]> <chr [1]></span></span>
<span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="co">#> 6 <chr [1]> <chr [1]> <NULL> <chr [2]> <chr [1]> <chr [1]> <chr [1]></span></span>
<span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a><span class="co">#> # ℹ 24 more rows</span></span></code></pre></div>
<p>What you do next will depend on the purposes of the analysis. Maybe
you want a row for every book and TV series that the character appears
in:</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>chars2 <span class="sc">%>%</span> </span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a> <span class="fu">select</span>(name, books, tvSeries) <span class="sc">%>%</span> </span>
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="fu">c</span>(books, tvSeries), <span class="at">names_to =</span> <span class="st">"media"</span>, <span class="at">values_to =</span> <span class="st">"value"</span>) <span class="sc">%>%</span> </span>
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a> <span class="fu">unnest_longer</span>(value)</span>
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="co">#> # A tibble: 179 × 3</span></span>
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#> name media value </span></span>
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#> <chr> <chr> <chr> </span></span>
<span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="co">#> 1 Theon Greyjoy books A Game of Thrones</span></span>
<span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a><span class="co">#> 2 Theon Greyjoy books A Storm of Swords</span></span>
<span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="co">#> 3 Theon Greyjoy books A Feast for Crows</span></span>
<span id="cb12-11"><a href="#cb12-11" tabindex="-1"></a><span class="co">#> 4 Theon Greyjoy tvSeries Season 1 </span></span>
<span id="cb12-12"><a href="#cb12-12" tabindex="-1"></a><span class="co">#> 5 Theon Greyjoy tvSeries Season 2 </span></span>
<span id="cb12-13"><a href="#cb12-13" tabindex="-1"></a><span class="co">#> 6 Theon Greyjoy tvSeries Season 3 </span></span>
<span id="cb12-14"><a href="#cb12-14" tabindex="-1"></a><span class="co">#> # ℹ 173 more rows</span></span></code></pre></div>
<p>Or maybe you want to build a table that lets you match title to
name:</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>chars2 <span class="sc">%>%</span> </span>
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> <span class="fu">select</span>(name, <span class="at">title =</span> titles) <span class="sc">%>%</span> </span>
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a> <span class="fu">unnest_longer</span>(title)</span>
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="co">#> # A tibble: 59 × 2</span></span>
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#> name title </span></span>
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co">#> <chr> <chr> </span></span>
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a><span class="co">#> 1 Theon Greyjoy Prince of Winterfell </span></span>
<span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a><span class="co">#> 2 Theon Greyjoy Lord of the Iron Islands (by law of the green lands)</span></span>
<span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a><span class="co">#> 3 Tyrion Lannister Acting Hand of the King (former) </span></span>
<span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a><span class="co">#> 4 Tyrion Lannister Master of Coin (former) </span></span>
<span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a><span class="co">#> 5 Victarion Greyjoy Lord Captain of the Iron Fleet </span></span>
<span id="cb13-12"><a href="#cb13-12" tabindex="-1"></a><span class="co">#> 6 Victarion Greyjoy Master of the Iron Victory </span></span>
<span id="cb13-13"><a href="#cb13-13" tabindex="-1"></a><span class="co">#> # ℹ 53 more rows</span></span></code></pre></div>
<p>(Note that the empty titles (<code>""</code>) are due to an
infelicity in the input <code>got_chars</code>: ideally people without
titles would have a title vector of length 0, not a title vector of
length 1 containing an empty string.)</p>
</div>
<div id="geocoding-with-google" class="section level2">
<h2>Geocoding with google</h2>
<p>Next we’ll tackle a more complex form of data that comes from
Google’s geocoding service, stored in the repurssive package</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>repurrrsive<span class="sc">::</span>gmaps_cities</span>
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="co">#> # A tibble: 5 × 2</span></span>
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="co">#> city json </span></span>
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#> <chr> <list> </span></span>
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#> 1 Houston <named list [2]></span></span>
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="co">#> 2 Washington <named list [2]></span></span>
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#> 3 New York <named list [2]></span></span>
<span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="co">#> 4 Chicago <named list [2]></span></span>
<span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="co">#> 5 Arlington <named list [2]></span></span></code></pre></div>
<p><code>json</code> is a list-column of named lists, so it makes sense
to start with <code>unnest_wider()</code>:</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>repurrrsive<span class="sc">::</span>gmaps_cities <span class="sc">%>%</span></span>
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> <span class="fu">unnest_wider</span>(json)</span>
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="co">#> # A tibble: 5 × 3</span></span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="co">#> city results status</span></span>
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a><span class="co">#> <chr> <list> <chr> </span></span>
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="co">#> 1 Houston <list [1]> OK </span></span>
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a><span class="co">#> 2 Washington <list [2]> OK </span></span>
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a><span class="co">#> 3 New York <list [1]> OK </span></span>
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a><span class="co">#> 4 Chicago <list [1]> OK </span></span>
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a><span class="co">#> 5 Arlington <list [2]> OK</span></span></code></pre></div>
<p>Notice that <code>results</code> is a list of lists. Most of the
cities have 1 element (representing a unique match from the geocoding
API), but Washington and Arlington have two. We can pull these out into
separate rows with <code>unnest_longer()</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>repurrrsive<span class="sc">::</span>gmaps_cities <span class="sc">%>%</span></span>
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> <span class="fu">unnest_wider</span>(json) <span class="sc">%>%</span> </span>
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> <span class="fu">unnest_longer</span>(results)</span>
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a><span class="co">#> # A tibble: 7 × 3</span></span>
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="co">#> city results status</span></span>
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="co">#> <chr> <list> <chr> </span></span>
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a><span class="co">#> 1 Houston <named list [5]> OK </span></span>
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="co">#> 2 Washington <named list [5]> OK </span></span>
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a><span class="co">#> 3 Washington <named list [5]> OK </span></span>
<span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a><span class="co">#> 4 New York <named list [5]> OK </span></span>
<span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a><span class="co">#> 5 Chicago <named list [5]> OK </span></span>
<span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a><span class="co">#> 6 Arlington <named list [5]> OK </span></span>
<span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a><span class="co">#> # ℹ 1 more row</span></span></code></pre></div>
<p>Now these all have the same components, as revealed by
<code>unnest_wider()</code>:</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>repurrrsive<span class="sc">::</span>gmaps_cities <span class="sc">%>%</span></span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a> <span class="fu">unnest_wider</span>(json) <span class="sc">%>%</span> </span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a> <span class="fu">unnest_longer</span>(results) <span class="sc">%>%</span> </span>
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a> <span class="fu">unnest_wider</span>(results)</span>
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="co">#> # A tibble: 7 × 7</span></span>
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co">#> city address_components formatted_address geometry place_id types status</span></span>
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a><span class="co">#> <chr> <list> <chr> <list> <chr> <list> <chr> </span></span>
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a><span class="co">#> 1 Hous… <list [4]> Houston, TX, USA <named list> ChIJAYW… <list> OK </span></span>
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a><span class="co">#> 2 Wash… <list [2]> Washington, USA <named list> ChIJ-bD… <list> OK </span></span>
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a><span class="co">#> 3 Wash… <list [4]> Washington, DC, … <named list> ChIJW-T… <list> OK </span></span>
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="co">#> 4 New … <list [3]> New York, NY, USA <named list> ChIJOwg… <list> OK </span></span>
<span id="cb17-12"><a href="#cb17-12" tabindex="-1"></a><span class="co">#> 5 Chic… <list [4]> Chicago, IL, USA <named list> ChIJ7cv… <list> OK </span></span>
<span id="cb17-13"><a href="#cb17-13" tabindex="-1"></a><span class="co">#> 6 Arli… <list [4]> Arlington, TX, U… <named list> ChIJ05g… <list> OK </span></span>
<span id="cb17-14"><a href="#cb17-14" tabindex="-1"></a><span class="co">#> # ℹ 1 more row</span></span></code></pre></div>
<p>We can find the latitude and longitude by unnesting
<code>geometry</code>:</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a>repurrrsive<span class="sc">::</span>gmaps_cities <span class="sc">%>%</span></span>
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a> <span class="fu">unnest_wider</span>(json) <span class="sc">%>%</span> </span>
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a> <span class="fu">unnest_longer</span>(results) <span class="sc">%>%</span> </span>
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a> <span class="fu">unnest_wider</span>(results) <span class="sc">%>%</span> </span>
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a> <span class="fu">unnest_wider</span>(geometry)</span>
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a><span class="co">#> # A tibble: 7 × 10</span></span>
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="co">#> city address_components formatted_address bounds location </span></span>
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a><span class="co">#> <chr> <list> <chr> <list> <list> </span></span>
<span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a><span class="co">#> 1 Houston <list [4]> Houston, TX, USA <named list> <named list></span></span>
<span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a><span class="co">#> 2 Washington <list [2]> Washington, USA <named list> <named list></span></span>
<span id="cb18-11"><a href="#cb18-11" tabindex="-1"></a><span class="co">#> 3 Washington <list [4]> Washington, DC, USA <named list> <named list></span></span>
<span id="cb18-12"><a href="#cb18-12" tabindex="-1"></a><span class="co">#> 4 New York <list [3]> New York, NY, USA <named list> <named list></span></span>
<span id="cb18-13"><a href="#cb18-13" tabindex="-1"></a><span class="co">#> 5 Chicago <list [4]> Chicago, IL, USA <named list> <named list></span></span>
<span id="cb18-14"><a href="#cb18-14" tabindex="-1"></a><span class="co">#> 6 Arlington <list [4]> Arlington, TX, USA <named list> <named list></span></span>
<span id="cb18-15"><a href="#cb18-15" tabindex="-1"></a><span class="co">#> # ℹ 1 more row</span></span>
<span id="cb18-16"><a href="#cb18-16" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: location_type <chr>, viewport <list>, place_id <chr>,</span></span>
<span id="cb18-17"><a href="#cb18-17" tabindex="-1"></a><span class="co">#> # types <list>, status <chr></span></span></code></pre></div>
<p>And then location:</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>repurrrsive<span class="sc">::</span>gmaps_cities <span class="sc">%>%</span></span>
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a> <span class="fu">unnest_wider</span>(json) <span class="sc">%>%</span></span>
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a> <span class="fu">unnest_longer</span>(results) <span class="sc">%>%</span></span>
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a> <span class="fu">unnest_wider</span>(results) <span class="sc">%>%</span></span>
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a> <span class="fu">unnest_wider</span>(geometry) <span class="sc">%>%</span></span>
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a> <span class="fu">unnest_wider</span>(location)</span>
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a><span class="co">#> # A tibble: 7 × 11</span></span>
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a><span class="co">#> city address_components formatted_address bounds lat lng</span></span>
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a><span class="co">#> <chr> <list> <chr> <list> <dbl> <dbl></span></span>
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a><span class="co">#> 1 Houston <list [4]> Houston, TX, USA <named list> 29.8 -95.4</span></span>
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a><span class="co">#> 2 Washington <list [2]> Washington, USA <named list> 47.8 -121. </span></span>
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a><span class="co">#> 3 Washington <list [4]> Washington, DC, USA <named list> 38.9 -77.0</span></span>
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a><span class="co">#> 4 New York <list [3]> New York, NY, USA <named list> 40.7 -74.0</span></span>
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a><span class="co">#> 5 Chicago <list [4]> Chicago, IL, USA <named list> 41.9 -87.6</span></span>
<span id="cb19-15"><a href="#cb19-15" tabindex="-1"></a><span class="co">#> 6 Arlington <list [4]> Arlington, TX, USA <named list> 32.7 -97.1</span></span>
<span id="cb19-16"><a href="#cb19-16" tabindex="-1"></a><span class="co">#> # ℹ 1 more row</span></span>
<span id="cb19-17"><a href="#cb19-17" tabindex="-1"></a><span class="co">#> # ℹ 5 more variables: location_type <chr>, viewport <list>, place_id <chr>,</span></span>
<span id="cb19-18"><a href="#cb19-18" tabindex="-1"></a><span class="co">#> # types <list>, status <chr></span></span></code></pre></div>
<p>We could also just look at the first address for each city:</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>repurrrsive<span class="sc">::</span>gmaps_cities <span class="sc">%>%</span></span>
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a> <span class="fu">unnest_wider</span>(json) <span class="sc">%>%</span></span>
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a> <span class="fu">hoist</span>(results, <span class="at">first_result =</span> <span class="dv">1</span>) <span class="sc">%>%</span></span>
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a> <span class="fu">unnest_wider</span>(first_result) <span class="sc">%>%</span></span>
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a> <span class="fu">unnest_wider</span>(geometry) <span class="sc">%>%</span></span>
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a> <span class="fu">unnest_wider</span>(location)</span>
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a><span class="co">#> # A tibble: 5 × 12</span></span>
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a><span class="co">#> city address_components formatted_address bounds lat lng</span></span>
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a><span class="co">#> <chr> <list> <chr> <list> <dbl> <dbl></span></span>
<span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a><span class="co">#> 1 Houston <list [4]> Houston, TX, USA <named list [2]> 29.8 -95.4</span></span>
<span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a><span class="co">#> 2 Washington <list [2]> Washington, USA <named list [2]> 47.8 -121. </span></span>
<span id="cb20-12"><a href="#cb20-12" tabindex="-1"></a><span class="co">#> 3 New York <list [3]> New York, NY, USA <named list [2]> 40.7 -74.0</span></span>
<span id="cb20-13"><a href="#cb20-13" tabindex="-1"></a><span class="co">#> 4 Chicago <list [4]> Chicago, IL, USA <named list [2]> 41.9 -87.6</span></span>
<span id="cb20-14"><a href="#cb20-14" tabindex="-1"></a><span class="co">#> 5 Arlington <list [4]> Arlington, TX, USA <named list [2]> 32.7 -97.1</span></span>
<span id="cb20-15"><a href="#cb20-15" tabindex="-1"></a><span class="co">#> # ℹ 6 more variables: location_type <chr>, viewport <list>, place_id <chr>,</span></span>
<span id="cb20-16"><a href="#cb20-16" tabindex="-1"></a><span class="co">#> # types <list>, results <list>, status <chr></span></span></code></pre></div>
<p>Or use <code>hoist()</code> to dive deeply to get directly to
<code>lat</code> and <code>lng</code>:</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>repurrrsive<span class="sc">::</span>gmaps_cities <span class="sc">%>%</span></span>
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a> <span class="fu">hoist</span>(json,</span>
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a> <span class="at">lat =</span> <span class="fu">list</span>(<span class="st">"results"</span>, <span class="dv">1</span>, <span class="st">"geometry"</span>, <span class="st">"location"</span>, <span class="st">"lat"</span>),</span>
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a> <span class="at">lng =</span> <span class="fu">list</span>(<span class="st">"results"</span>, <span class="dv">1</span>, <span class="st">"geometry"</span>, <span class="st">"location"</span>, <span class="st">"lng"</span>)</span>
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a> )</span>
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a><span class="co">#> # A tibble: 5 × 4</span></span>
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="co">#> city lat lng json </span></span>
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="co">#> <chr> <dbl> <dbl> <list> </span></span>
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="co">#> 1 Houston 29.8 -95.4 <named list [2]></span></span>
<span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a><span class="co">#> 2 Washington 47.8 -121. <named list [2]></span></span>
<span id="cb21-11"><a href="#cb21-11" tabindex="-1"></a><span class="co">#> 3 New York 40.7 -74.0 <named list [2]></span></span>
<span id="cb21-12"><a href="#cb21-12" tabindex="-1"></a><span class="co">#> 4 Chicago 41.9 -87.6 <named list [2]></span></span>
<span id="cb21-13"><a href="#cb21-13" tabindex="-1"></a><span class="co">#> 5 Arlington 32.7 -97.1 <named list [2]></span></span></code></pre></div>
</div>
<div id="sharla-gelfands-discography" class="section level2">
<h2>Sharla Gelfand’s discography</h2>
<p>We’ll finish off with the most complex list, from <a href="https://sharla.party/post/discog-purrr/">Sharla Gelfand’s</a>
discography. We’ll start the usual way: putting the list into a single
column data frame, and then widening so each component is a column. I
also parse the <code>date_added</code> column into a real date-time<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a>discs <span class="ot"><-</span> <span class="fu">tibble</span>(<span class="at">disc =</span> discog) <span class="sc">%>%</span> </span>
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a> <span class="fu">unnest_wider</span>(disc) <span class="sc">%>%</span> </span>
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_added =</span> <span class="fu">as.POSIXct</span>(<span class="fu">strptime</span>(date_added, <span class="st">"%Y-%m-%dT%H:%M:%S"</span>))) </span>
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a>discs</span>
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="co">#> # A tibble: 155 × 5</span></span>
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a><span class="co">#> instance_id date_added basic_information id rating</span></span>
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a><span class="co">#> <int> <dttm> <list> <int> <int></span></span>
<span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a><span class="co">#> 1 354823933 2019-02-16 17:48:59 <named list [11]> 7496378 0</span></span>
<span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a><span class="co">#> 2 354092601 2019-02-13 14:13:11 <named list [11]> 4490852 0</span></span>
<span id="cb22-10"><a href="#cb22-10" tabindex="-1"></a><span class="co">#> 3 354091476 2019-02-13 14:07:23 <named list [11]> 9827276 0</span></span>
<span id="cb22-11"><a href="#cb22-11" tabindex="-1"></a><span class="co">#> 4 351244906 2019-02-02 11:39:58 <named list [11]> 9769203 0</span></span>
<span id="cb22-12"><a href="#cb22-12" tabindex="-1"></a><span class="co">#> 5 351244801 2019-02-02 11:39:37 <named list [11]> 7237138 0</span></span>
<span id="cb22-13"><a href="#cb22-13" tabindex="-1"></a><span class="co">#> 6 351052065 2019-02-01 20:40:53 <named list [11]> 13117042 0</span></span>
<span id="cb22-14"><a href="#cb22-14" tabindex="-1"></a><span class="co">#> # ℹ 149 more rows</span></span></code></pre></div>
<p>At this level, we see information about when each disc was added to
Sharla’s discography, not any information about the disc itself. To do
that we need to widen the <code>basic_information</code> column:</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a>discs <span class="sc">%>%</span> <span class="fu">unnest_wider</span>(basic_information)</span>
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="co">#> Error in `unnest_wider()`:</span></span>
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a><span class="co">#> ! Can't duplicate names between the affected columns and the original</span></span>
<span id="cb23-4"><a href="#cb23-4" tabindex="-1"></a><span class="co">#> data.</span></span>
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a><span class="co">#> ✖ These names are duplicated:</span></span>
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="co">#> ℹ `id`, from `basic_information`.</span></span>
<span id="cb23-7"><a href="#cb23-7" tabindex="-1"></a><span class="co">#> ℹ Use `names_sep` to disambiguate using the column name.</span></span>
<span id="cb23-8"><a href="#cb23-8" tabindex="-1"></a><span class="co">#> ℹ Or use `names_repair` to specify a repair strategy.</span></span></code></pre></div>
<p>Unfortunately that fails because there’s an <code>id</code> column
inside <code>basic_information</code>. We can quickly see what’s going
on by setting <code>names_repair = "unique"</code>:</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a>discs <span class="sc">%>%</span> <span class="fu">unnest_wider</span>(basic_information, <span class="at">names_repair =</span> <span class="st">"unique"</span>)</span>
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="co">#> New names:</span></span>
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a><span class="co">#> • `id` -> `id...7`</span></span>
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a><span class="co">#> • `id` -> `id...14`</span></span>
<span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a><span class="co">#> # A tibble: 155 × 15</span></span>
<span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a><span class="co">#> instance_id date_added labels year master_url artists id...7 thumb</span></span>
<span id="cb24-7"><a href="#cb24-7" tabindex="-1"></a><span class="co">#> <int> <dttm> <list> <int> <chr> <list> <int> <chr></span></span>
<span id="cb24-8"><a href="#cb24-8" tabindex="-1"></a><span class="co">#> 1 354823933 2019-02-16 17:48:59 <list> 2015 <NA> <list> 7.50e6 http…</span></span>
<span id="cb24-9"><a href="#cb24-9" tabindex="-1"></a><span class="co">#> 2 354092601 2019-02-13 14:13:11 <list> 2013 https://api… <list> 4.49e6 http…</span></span>
<span id="cb24-10"><a href="#cb24-10" tabindex="-1"></a><span class="co">#> 3 354091476 2019-02-13 14:07:23 <list> 2017 https://api… <list> 9.83e6 http…</span></span>
<span id="cb24-11"><a href="#cb24-11" tabindex="-1"></a><span class="co">#> 4 351244906 2019-02-02 11:39:58 <list> 2017 https://api… <list> 9.77e6 http…</span></span>
<span id="cb24-12"><a href="#cb24-12" tabindex="-1"></a><span class="co">#> 5 351244801 2019-02-02 11:39:37 <list> 2015 https://api… <list> 7.24e6 http…</span></span>
<span id="cb24-13"><a href="#cb24-13" tabindex="-1"></a><span class="co">#> 6 351052065 2019-02-01 20:40:53 <list> 2019 https://api… <list> 1.31e7 http…</span></span>
<span id="cb24-14"><a href="#cb24-14" tabindex="-1"></a><span class="co">#> # ℹ 149 more rows</span></span>
<span id="cb24-15"><a href="#cb24-15" tabindex="-1"></a><span class="co">#> # ℹ 7 more variables: title <chr>, formats <list>, cover_image <chr>,</span></span>
<span id="cb24-16"><a href="#cb24-16" tabindex="-1"></a><span class="co">#> # resource_url <chr>, master_id <int>, id...14 <int>, rating <int></span></span></code></pre></div>
<p>The problem is that <code>basic_information</code> repeats the
<code>id</code> column that’s also stored at the top-level, so we can
just drop that:</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a>discs <span class="sc">%>%</span> </span>
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a> <span class="fu">select</span>(<span class="sc">!</span>id) <span class="sc">%>%</span> </span>
<span id="cb25-3"><a href="#cb25-3" tabindex="-1"></a> <span class="fu">unnest_wider</span>(basic_information)</span>
<span id="cb25-4"><a href="#cb25-4" tabindex="-1"></a><span class="co">#> # A tibble: 155 × 14</span></span>
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a><span class="co">#> instance_id date_added labels year master_url artists id thumb</span></span>
<span id="cb25-6"><a href="#cb25-6" tabindex="-1"></a><span class="co">#> <int> <dttm> <list> <int> <chr> <list> <int> <chr></span></span>
<span id="cb25-7"><a href="#cb25-7" tabindex="-1"></a><span class="co">#> 1 354823933 2019-02-16 17:48:59 <list> 2015 <NA> <list> 7.50e6 http…</span></span>
<span id="cb25-8"><a href="#cb25-8" tabindex="-1"></a><span class="co">#> 2 354092601 2019-02-13 14:13:11 <list> 2013 https://api… <list> 4.49e6 http…</span></span>
<span id="cb25-9"><a href="#cb25-9" tabindex="-1"></a><span class="co">#> 3 354091476 2019-02-13 14:07:23 <list> 2017 https://api… <list> 9.83e6 http…</span></span>
<span id="cb25-10"><a href="#cb25-10" tabindex="-1"></a><span class="co">#> 4 351244906 2019-02-02 11:39:58 <list> 2017 https://api… <list> 9.77e6 http…</span></span>
<span id="cb25-11"><a href="#cb25-11" tabindex="-1"></a><span class="co">#> 5 351244801 2019-02-02 11:39:37 <list> 2015 https://api… <list> 7.24e6 http…</span></span>
<span id="cb25-12"><a href="#cb25-12" tabindex="-1"></a><span class="co">#> 6 351052065 2019-02-01 20:40:53 <list> 2019 https://api… <list> 1.31e7 http…</span></span>
<span id="cb25-13"><a href="#cb25-13" tabindex="-1"></a><span class="co">#> # ℹ 149 more rows</span></span>
<span id="cb25-14"><a href="#cb25-14" tabindex="-1"></a><span class="co">#> # ℹ 6 more variables: title <chr>, formats <list>, cover_image <chr>,</span></span>
<span id="cb25-15"><a href="#cb25-15" tabindex="-1"></a><span class="co">#> # resource_url <chr>, master_id <int>, rating <int></span></span></code></pre></div>
<p>Alternatively, we could use <code>hoist()</code>:</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a>discs <span class="sc">%>%</span> </span>
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a> <span class="fu">hoist</span>(basic_information,</span>
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"title"</span>,</span>
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a> <span class="at">year =</span> <span class="st">"year"</span>,</span>
<span id="cb26-5"><a href="#cb26-5" tabindex="-1"></a> <span class="at">label =</span> <span class="fu">list</span>(<span class="st">"labels"</span>, <span class="dv">1</span>, <span class="st">"name"</span>),</span>
<span id="cb26-6"><a href="#cb26-6" tabindex="-1"></a> <span class="at">artist =</span> <span class="fu">list</span>(<span class="st">"artists"</span>, <span class="dv">1</span>, <span class="st">"name"</span>)</span>
<span id="cb26-7"><a href="#cb26-7" tabindex="-1"></a> )</span>
<span id="cb26-8"><a href="#cb26-8" tabindex="-1"></a><span class="co">#> # A tibble: 155 × 9</span></span>
<span id="cb26-9"><a href="#cb26-9" tabindex="-1"></a><span class="co">#> instance_id date_added title year label artist basic_information</span></span>
<span id="cb26-10"><a href="#cb26-10" tabindex="-1"></a><span class="co">#> <int> <dttm> <chr> <int> <chr> <chr> <list> </span></span>
<span id="cb26-11"><a href="#cb26-11" tabindex="-1"></a><span class="co">#> 1 354823933 2019-02-16 17:48:59 Demo 2015 Tobi… Mollot <named list [9]> </span></span>
<span id="cb26-12"><a href="#cb26-12" tabindex="-1"></a><span class="co">#> 2 354092601 2019-02-13 14:13:11 Observan… 2013 La V… Una B… <named list [9]> </span></span>
<span id="cb26-13"><a href="#cb26-13" tabindex="-1"></a><span class="co">#> 3 354091476 2019-02-13 14:07:23 I 2017 La V… S.H.I… <named list [9]> </span></span>
<span id="cb26-14"><a href="#cb26-14" tabindex="-1"></a><span class="co">#> 4 351244906 2019-02-02 11:39:58 Oído Abs… 2017 La V… Rata … <named list [9]> </span></span>
<span id="cb26-15"><a href="#cb26-15" tabindex="-1"></a><span class="co">#> 5 351244801 2019-02-02 11:39:37 A Cat's … 2015 Kato… Ivy (… <named list [9]> </span></span>
<span id="cb26-16"><a href="#cb26-16" tabindex="-1"></a><span class="co">#> 6 351052065 2019-02-01 20:40:53 Tashme 2019 High… Tashme <named list [9]> </span></span>
<span id="cb26-17"><a href="#cb26-17" tabindex="-1"></a><span class="co">#> # ℹ 149 more rows</span></span>
<span id="cb26-18"><a href="#cb26-18" tabindex="-1"></a><span class="co">#> # ℹ 2 more variables: id <int>, rating <int></span></span></code></pre></div>
<p>Here I quickly extract the name of the first label and artist by
indexing deeply into the nested list.</p>
<p>A more systematic approach would be to create separate tables for
artist and label:</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a>discs <span class="sc">%>%</span> </span>
<span id="cb27-2"><a href="#cb27-2" tabindex="-1"></a> <span class="fu">hoist</span>(basic_information, <span class="at">artist =</span> <span class="st">"artists"</span>) <span class="sc">%>%</span> </span>
<span id="cb27-3"><a href="#cb27-3" tabindex="-1"></a> <span class="fu">select</span>(<span class="at">disc_id =</span> id, artist) <span class="sc">%>%</span> </span>
<span id="cb27-4"><a href="#cb27-4" tabindex="-1"></a> <span class="fu">unnest_longer</span>(artist) <span class="sc">%>%</span> </span>
<span id="cb27-5"><a href="#cb27-5" tabindex="-1"></a> <span class="fu">unnest_wider</span>(artist)</span>
<span id="cb27-6"><a href="#cb27-6" tabindex="-1"></a><span class="co">#> # A tibble: 167 × 8</span></span>
<span id="cb27-7"><a href="#cb27-7" tabindex="-1"></a><span class="co">#> disc_id join name anv tracks role resource_url id</span></span>
<span id="cb27-8"><a href="#cb27-8" tabindex="-1"></a><span class="co">#> <int> <chr> <chr> <chr> <chr> <chr> <chr> <int></span></span>
<span id="cb27-9"><a href="#cb27-9" tabindex="-1"></a><span class="co">#> 1 7496378 "" Mollot "" "" "" https://api… 4.62e6</span></span>
<span id="cb27-10"><a href="#cb27-10" tabindex="-1"></a><span class="co">#> 2 4490852 "" Una Bèstia Incontrolable "" "" "" https://api… 3.19e6</span></span>
<span id="cb27-11"><a href="#cb27-11" tabindex="-1"></a><span class="co">#> 3 9827276 "" S.H.I.T. (3) "" "" "" https://api… 2.77e6</span></span>
<span id="cb27-12"><a href="#cb27-12" tabindex="-1"></a><span class="co">#> 4 9769203 "" Rata Negra "" "" "" https://api… 4.28e6</span></span>
<span id="cb27-13"><a href="#cb27-13" tabindex="-1"></a><span class="co">#> 5 7237138 "" Ivy (18) "" "" "" https://api… 3.60e6</span></span>
<span id="cb27-14"><a href="#cb27-14" tabindex="-1"></a><span class="co">#> 6 13117042 "" Tashme "" "" "" https://api… 5.21e6</span></span>
<span id="cb27-15"><a href="#cb27-15" tabindex="-1"></a><span class="co">#> # ℹ 161 more rows</span></span>
<span id="cb27-16"><a href="#cb27-16" tabindex="-1"></a></span>
<span id="cb27-17"><a href="#cb27-17" tabindex="-1"></a>discs <span class="sc">%>%</span> </span>
<span id="cb27-18"><a href="#cb27-18" tabindex="-1"></a> <span class="fu">hoist</span>(basic_information, <span class="at">format =</span> <span class="st">"formats"</span>) <span class="sc">%>%</span> </span>
<span id="cb27-19"><a href="#cb27-19" tabindex="-1"></a> <span class="fu">select</span>(<span class="at">disc_id =</span> id, format) <span class="sc">%>%</span> </span>
<span id="cb27-20"><a href="#cb27-20" tabindex="-1"></a> <span class="fu">unnest_longer</span>(format) <span class="sc">%>%</span> </span>
<span id="cb27-21"><a href="#cb27-21" tabindex="-1"></a> <span class="fu">unnest_wider</span>(format) <span class="sc">%>%</span> </span>
<span id="cb27-22"><a href="#cb27-22" tabindex="-1"></a> <span class="fu">unnest_longer</span>(descriptions)</span>
<span id="cb27-23"><a href="#cb27-23" tabindex="-1"></a><span class="co">#> # A tibble: 258 × 5</span></span>
<span id="cb27-24"><a href="#cb27-24" tabindex="-1"></a><span class="co">#> disc_id descriptions text name qty </span></span>
<span id="cb27-25"><a href="#cb27-25" tabindex="-1"></a><span class="co">#> <int> <chr> <chr> <chr> <chr></span></span>
<span id="cb27-26"><a href="#cb27-26" tabindex="-1"></a><span class="co">#> 1 7496378 "Numbered" Black Cassette 1 </span></span>
<span id="cb27-27"><a href="#cb27-27" tabindex="-1"></a><span class="co">#> 2 4490852 "LP" <NA> Vinyl 1 </span></span>
<span id="cb27-28"><a href="#cb27-28" tabindex="-1"></a><span class="co">#> 3 9827276 "7\"" <NA> Vinyl 1 </span></span>
<span id="cb27-29"><a href="#cb27-29" tabindex="-1"></a><span class="co">#> 4 9827276 "45 RPM" <NA> Vinyl 1 </span></span>
<span id="cb27-30"><a href="#cb27-30" tabindex="-1"></a><span class="co">#> 5 9827276 "EP" <NA> Vinyl 1 </span></span>
<span id="cb27-31"><a href="#cb27-31" tabindex="-1"></a><span class="co">#> 6 9769203 "LP" <NA> Vinyl 1 </span></span>
<span id="cb27-32"><a href="#cb27-32" tabindex="-1"></a><span class="co">#> # ℹ 252 more rows</span></span></code></pre></div>
<p>Then you could join these back on to the original dataset as
needed.</p>
</div>
<div class="footnotes footnotes-end-of-document">
<hr />
<ol>
<li id="fn1"><p>I’d normally use <code>readr::parse_datetime()</code> or
<code>lubridate::ymd_hms()</code>, but I can’t here because it’s a
vignette and I don’t want to add a dependency to tidyr just to simplify
one example.<a href="#fnref1" class="footnote-back">↩︎</a></p></li>
</ol>
</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>
|