File: rectangle.html

package info (click to toggle)
r-cran-tidyr 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,720 kB
  • sloc: cpp: 268; sh: 9; makefile: 2
file content (879 lines) | stat: -rw-r--r-- 77,021 bytes parent folder | download
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">&lt;-</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">#&gt;  [1] &quot;login&quot;               &quot;id&quot;                  &quot;avatar_url&quot;         </span></span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#&gt;  [4] &quot;gravatar_id&quot;         &quot;url&quot;                 &quot;html_url&quot;           </span></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#&gt;  [7] &quot;followers_url&quot;       &quot;following_url&quot;       &quot;gists_url&quot;          </span></span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#&gt; [10] &quot;starred_url&quot;         &quot;subscriptions_url&quot;   &quot;organizations_url&quot;  </span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#&gt; [13] &quot;repos_url&quot;           &quot;events_url&quot;          &quot;received_events_url&quot;</span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co">#&gt; [16] &quot;type&quot;                &quot;site_admin&quot;          &quot;name&quot;               </span></span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#&gt; [19] &quot;company&quot;             &quot;blog&quot;                &quot;location&quot;           </span></span>
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co">#&gt; [22] &quot;email&quot;               &quot;hireable&quot;            &quot;bio&quot;                </span></span>
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co">#&gt; [25] &quot;public_repos&quot;        &quot;public_gists&quot;        &quot;followers&quot;          </span></span>
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="co">#&gt; [28] &quot;following&quot;           &quot;created_at&quot;          &quot;updated_at&quot;</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">%&gt;%</span> <span class="fu">unnest_wider</span>(user)</span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="co">#&gt; # A tibble: 6 × 30</span></span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#&gt;   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">#&gt;   &lt;chr&gt;  &lt;int&gt; &lt;chr&gt;      &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt;    &lt;chr&gt;         &lt;chr&gt;        </span></span>
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#&gt; 1 gabo… 6.60e5 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#&gt; 2 jenn… 5.99e5 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#&gt; 3 jtle… 1.57e6 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#&gt; 4 juli… 1.25e7 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#&gt; 5 leep… 3.51e6 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#&gt; 6 masa… 8.36e6 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#&gt; # ℹ 22 more variables: gists_url &lt;chr&gt;, starred_url &lt;chr&gt;,</span></span>
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a><span class="co">#&gt; #   subscriptions_url &lt;chr&gt;, organizations_url &lt;chr&gt;, repos_url &lt;chr&gt;,</span></span>
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#&gt; #   events_url &lt;chr&gt;, received_events_url &lt;chr&gt;, type &lt;chr&gt;, site_admin &lt;lgl&gt;,</span></span>
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="co">#&gt; #   name &lt;chr&gt;, company &lt;chr&gt;, blog &lt;chr&gt;, location &lt;chr&gt;, email &lt;chr&gt;,</span></span>
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="co">#&gt; #   hireable &lt;lgl&gt;, bio &lt;chr&gt;, public_repos &lt;int&gt;, public_gists &lt;int&gt;,</span></span>
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a><span class="co">#&gt; #   followers &lt;int&gt;, following &lt;int&gt;, created_at &lt;chr&gt;, updated_at &lt;chr&gt;</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">%&gt;%</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">&quot;followers&quot;</span>, </span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a>  <span class="at">login =</span> <span class="st">&quot;login&quot;</span>, </span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a>  <span class="at">url =</span> <span class="st">&quot;html_url&quot;</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">#&gt; # A tibble: 6 × 4</span></span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#&gt;   followers login       url                            user             </span></span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#&gt;       &lt;int&gt; &lt;chr&gt;       &lt;chr&gt;                          &lt;list&gt;           </span></span>
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#&gt; 1       303 gaborcsardi https://github.com/gaborcsardi &lt;named list [27]&gt;</span></span>
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co">#&gt; 2       780 jennybc     https://github.com/jennybc     &lt;named list [27]&gt;</span></span>
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co">#&gt; 3      3958 jtleek      https://github.com/jtleek      &lt;named list [27]&gt;</span></span>
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="co">#&gt; 4       115 juliasilge  https://github.com/juliasilge  &lt;named list [27]&gt;</span></span>
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a><span class="co">#&gt; 5       213 leeper      https://github.com/leeper      &lt;named list [27]&gt;</span></span>
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a><span class="co">#&gt; 6        34 masalmon    https://github.com/masalmon    &lt;named list [27]&gt;</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">&lt;-</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">#&gt; # A tibble: 6 × 1</span></span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#&gt;   repo       </span></span>
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="co">#&gt;   &lt;list&gt;     </span></span>
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co">#&gt; 1 &lt;list [30]&gt;</span></span>
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co">#&gt; 2 &lt;list [30]&gt;</span></span>
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#&gt; 3 &lt;list [30]&gt;</span></span>
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="co">#&gt; 4 &lt;list [26]&gt;</span></span>
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#&gt; 5 &lt;list [30]&gt;</span></span>
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a><span class="co">#&gt; 6 &lt;list [30]&gt;</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">&lt;-</span> repos <span class="sc">%&gt;%</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">#&gt; # A tibble: 176 × 1</span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co">#&gt;   repo             </span></span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#&gt;   &lt;list&gt;           </span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#&gt; 1 &lt;named list [68]&gt;</span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#&gt; 2 &lt;named list [68]&gt;</span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co">#&gt; 3 &lt;named list [68]&gt;</span></span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co">#&gt; 4 &lt;named list [68]&gt;</span></span>
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="co">#&gt; 5 &lt;named list [68]&gt;</span></span>
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="co">#&gt; 6 &lt;named list [68]&gt;</span></span>
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a><span class="co">#&gt; # ℹ 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">%&gt;%</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">&quot;owner&quot;</span>, <span class="st">&quot;login&quot;</span>), </span>
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a>  <span class="at">name =</span> <span class="st">&quot;name&quot;</span>,</span>
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a>  <span class="at">homepage =</span> <span class="st">&quot;homepage&quot;</span>,</span>
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a>  <span class="at">watchers =</span> <span class="st">&quot;watchers_count&quot;</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">#&gt; # A tibble: 176 × 5</span></span>
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="co">#&gt;   login       name        homepage watchers repo             </span></span>
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;       &lt;chr&gt;       &lt;chr&gt;       &lt;int&gt; &lt;list&gt;           </span></span>
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="co">#&gt; 1 gaborcsardi after       &lt;NA&gt;            5 &lt;named list [65]&gt;</span></span>
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a><span class="co">#&gt; 2 gaborcsardi argufy      &lt;NA&gt;           19 &lt;named list [65]&gt;</span></span>
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a><span class="co">#&gt; 3 gaborcsardi ask         &lt;NA&gt;            5 &lt;named list [65]&gt;</span></span>
<span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a><span class="co">#&gt; 4 gaborcsardi baseimports &lt;NA&gt;            0 &lt;named list [65]&gt;</span></span>
<span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a><span class="co">#&gt; 5 gaborcsardi citest      &lt;NA&gt;            0 &lt;named list [65]&gt;</span></span>
<span id="cb8-15"><a href="#cb8-15" tabindex="-1"></a><span class="co">#&gt; 6 gaborcsardi clisymbols  &quot;&quot;             18 &lt;named list [65]&gt;</span></span>
<span id="cb8-16"><a href="#cb8-16" tabindex="-1"></a><span class="co">#&gt; # ℹ 170 more rows</span></span></code></pre></div>
<p>Note the use of <code>c(&quot;owner&quot;, &quot;login&quot;)</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">%&gt;%</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">&quot;owner&quot;</span>) <span class="sc">%&gt;%</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">#&gt; # A tibble: 176 × 18</span></span>
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#&gt;   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">#&gt;   &lt;chr&gt;  &lt;int&gt; &lt;chr&gt;      &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt;    &lt;chr&gt;         &lt;chr&gt;        </span></span>
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="co">#&gt; 1 gabo… 660288 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#&gt; 2 gabo… 660288 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co">#&gt; 3 gabo… 660288 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a><span class="co">#&gt; 4 gabo… 660288 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a><span class="co">#&gt; 5 gabo… 660288 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a><span class="co">#&gt; 6 gabo… 660288 https://a… &quot;&quot;          http… https:/… https://api.… https://api.…</span></span>
<span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a><span class="co">#&gt; # ℹ 170 more rows</span></span>
<span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a><span class="co">#&gt; # ℹ 10 more variables: gists_url &lt;chr&gt;, starred_url &lt;chr&gt;,</span></span>
<span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a><span class="co">#&gt; #   subscriptions_url &lt;chr&gt;, organizations_url &lt;chr&gt;, repos_url &lt;chr&gt;,</span></span>
<span id="cb9-16"><a href="#cb9-16" tabindex="-1"></a><span class="co">#&gt; #   events_url &lt;chr&gt;, received_events_url &lt;chr&gt;, type &lt;chr&gt;, site_admin &lt;lgl&gt;,</span></span>
<span id="cb9-17"><a href="#cb9-17" tabindex="-1"></a><span class="co">#&gt; #   repo &lt;list&gt;</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">&lt;-</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">#&gt; # A tibble: 30 × 1</span></span>
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="co">#&gt;   char             </span></span>
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#&gt;   &lt;list&gt;           </span></span>
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co">#&gt; 1 &lt;named list [18]&gt;</span></span>
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#&gt; 2 &lt;named list [18]&gt;</span></span>
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co">#&gt; 3 &lt;named list [18]&gt;</span></span>
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a><span class="co">#&gt; 4 &lt;named list [18]&gt;</span></span>
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="co">#&gt; 5 &lt;named list [18]&gt;</span></span>
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co">#&gt; 6 &lt;named list [18]&gt;</span></span>
<span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a><span class="co">#&gt; # ℹ 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">&lt;-</span> chars <span class="sc">%&gt;%</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">#&gt; # A tibble: 30 × 18</span></span>
<span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a><span class="co">#&gt;   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">#&gt;   &lt;chr&gt;       &lt;int&gt; &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;   &lt;chr&gt; &lt;chr&gt; &lt;lgl&gt; &lt;list&gt; &lt;list&gt;  &lt;chr&gt; </span></span>
<span id="cb10-19"><a href="#cb10-19" tabindex="-1"></a><span class="co">#&gt; 1 https://ww…  1022 Theo… Male   &quot;Ironb… &quot;In … &quot;&quot;    TRUE  &lt;chr&gt;  &lt;chr&gt;   &quot;&quot;    </span></span>
<span id="cb10-20"><a href="#cb10-20" tabindex="-1"></a><span class="co">#&gt; 2 https://ww…  1052 Tyri… Male   &quot;&quot;      &quot;In … &quot;&quot;    TRUE  &lt;chr&gt;  &lt;chr&gt;   &quot;&quot;    </span></span>
<span id="cb10-21"><a href="#cb10-21" tabindex="-1"></a><span class="co">#&gt; 3 https://ww…  1074 Vict… Male   &quot;Ironb… &quot;In … &quot;&quot;    TRUE  &lt;chr&gt;  &lt;chr&gt;   &quot;&quot;    </span></span>
<span id="cb10-22"><a href="#cb10-22" tabindex="-1"></a><span class="co">#&gt; 4 https://ww…  1109 Will  Male   &quot;&quot;      &quot;&quot;    &quot;In … FALSE &lt;chr&gt;  &lt;chr&gt;   &quot;&quot;    </span></span>
<span id="cb10-23"><a href="#cb10-23" tabindex="-1"></a><span class="co">#&gt; 5 https://ww…  1166 Areo… Male   &quot;Norvo… &quot;In … &quot;&quot;    TRUE  &lt;chr&gt;  &lt;chr&gt;   &quot;&quot;    </span></span>
<span id="cb10-24"><a href="#cb10-24" tabindex="-1"></a><span class="co">#&gt; 6 https://ww…  1267 Chett Male   &quot;&quot;      &quot;At … &quot;In … FALSE &lt;chr&gt;  &lt;chr&gt;   &quot;&quot;    </span></span>
<span id="cb10-25"><a href="#cb10-25" tabindex="-1"></a><span class="co">#&gt; # ℹ 24 more rows</span></span>
<span id="cb10-26"><a href="#cb10-26" tabindex="-1"></a><span class="co">#&gt; # ℹ 7 more variables: mother &lt;chr&gt;, spouse &lt;chr&gt;, allegiances &lt;list&gt;,</span></span>
<span id="cb10-27"><a href="#cb10-27" tabindex="-1"></a><span class="co">#&gt; #   books &lt;list&gt;, povBooks &lt;list&gt;, tvSeries &lt;list&gt;, playedBy &lt;list&gt;</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">%&gt;%</span> <span class="fu">select_if</span>(is.list)</span>
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="co">#&gt; # A tibble: 30 × 7</span></span>
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co">#&gt;   titles    aliases    allegiances books     povBooks  tvSeries  playedBy </span></span>
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="co">#&gt;   &lt;list&gt;    &lt;list&gt;     &lt;list&gt;      &lt;list&gt;    &lt;list&gt;    &lt;list&gt;    &lt;list&gt;   </span></span>
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="co">#&gt; 1 &lt;chr [2]&gt; &lt;chr [4]&gt;  &lt;chr [1]&gt;   &lt;chr [3]&gt; &lt;chr [2]&gt; &lt;chr [6]&gt; &lt;chr [1]&gt;</span></span>
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a><span class="co">#&gt; 2 &lt;chr [2]&gt; &lt;chr [11]&gt; &lt;chr [1]&gt;   &lt;chr [2]&gt; &lt;chr [4]&gt; &lt;chr [6]&gt; &lt;chr [1]&gt;</span></span>
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co">#&gt; 3 &lt;chr [2]&gt; &lt;chr [1]&gt;  &lt;chr [1]&gt;   &lt;chr [3]&gt; &lt;chr [2]&gt; &lt;chr [1]&gt; &lt;chr [1]&gt;</span></span>
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a><span class="co">#&gt; 4 &lt;chr [1]&gt; &lt;chr [1]&gt;  &lt;NULL&gt;      &lt;chr [1]&gt; &lt;chr [1]&gt; &lt;chr [1]&gt; &lt;chr [1]&gt;</span></span>
<span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="co">#&gt; 5 &lt;chr [1]&gt; &lt;chr [1]&gt;  &lt;chr [1]&gt;   &lt;chr [3]&gt; &lt;chr [2]&gt; &lt;chr [2]&gt; &lt;chr [1]&gt;</span></span>
<span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="co">#&gt; 6 &lt;chr [1]&gt; &lt;chr [1]&gt;  &lt;NULL&gt;      &lt;chr [2]&gt; &lt;chr [1]&gt; &lt;chr [1]&gt; &lt;chr [1]&gt;</span></span>
<span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a><span class="co">#&gt; # ℹ 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">%&gt;%</span> </span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a>  <span class="fu">select</span>(name, books, tvSeries) <span class="sc">%&gt;%</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">&quot;media&quot;</span>, <span class="at">values_to =</span> <span class="st">&quot;value&quot;</span>) <span class="sc">%&gt;%</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">#&gt; # A tibble: 179 × 3</span></span>
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#&gt;   name          media    value            </span></span>
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;         &lt;chr&gt;    &lt;chr&gt;            </span></span>
<span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="co">#&gt; 1 Theon Greyjoy books    A Game of Thrones</span></span>
<span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a><span class="co">#&gt; 2 Theon Greyjoy books    A Storm of Swords</span></span>
<span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="co">#&gt; 3 Theon Greyjoy books    A Feast for Crows</span></span>
<span id="cb12-11"><a href="#cb12-11" tabindex="-1"></a><span class="co">#&gt; 4 Theon Greyjoy tvSeries Season 1         </span></span>
<span id="cb12-12"><a href="#cb12-12" tabindex="-1"></a><span class="co">#&gt; 5 Theon Greyjoy tvSeries Season 2         </span></span>
<span id="cb12-13"><a href="#cb12-13" tabindex="-1"></a><span class="co">#&gt; 6 Theon Greyjoy tvSeries Season 3         </span></span>
<span id="cb12-14"><a href="#cb12-14" tabindex="-1"></a><span class="co">#&gt; # ℹ 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">%&gt;%</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">%&gt;%</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">#&gt; # A tibble: 59 × 2</span></span>
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#&gt;   name              title                                               </span></span>
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;             &lt;chr&gt;                                               </span></span>
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a><span class="co">#&gt; 1 Theon Greyjoy     Prince of Winterfell                                </span></span>
<span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a><span class="co">#&gt; 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">#&gt; 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">#&gt; 4 Tyrion Lannister  Master of Coin (former)                             </span></span>
<span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a><span class="co">#&gt; 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">#&gt; 6 Victarion Greyjoy Master of the Iron Victory                          </span></span>
<span id="cb13-13"><a href="#cb13-13" tabindex="-1"></a><span class="co">#&gt; # ℹ 53 more rows</span></span></code></pre></div>
<p>(Note that the empty titles (<code>&quot;&quot;</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">#&gt; # A tibble: 5 × 2</span></span>
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="co">#&gt;   city       json            </span></span>
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;      &lt;list&gt;          </span></span>
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#&gt; 1 Houston    &lt;named list [2]&gt;</span></span>
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="co">#&gt; 2 Washington &lt;named list [2]&gt;</span></span>
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#&gt; 3 New York   &lt;named list [2]&gt;</span></span>
<span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="co">#&gt; 4 Chicago    &lt;named list [2]&gt;</span></span>
<span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="co">#&gt; 5 Arlington  &lt;named list [2]&gt;</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">%&gt;%</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">#&gt; # A tibble: 5 × 3</span></span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="co">#&gt;   city       results    status</span></span>
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;      &lt;list&gt;     &lt;chr&gt; </span></span>
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="co">#&gt; 1 Houston    &lt;list [1]&gt; OK    </span></span>
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a><span class="co">#&gt; 2 Washington &lt;list [2]&gt; OK    </span></span>
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a><span class="co">#&gt; 3 New York   &lt;list [1]&gt; OK    </span></span>
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a><span class="co">#&gt; 4 Chicago    &lt;list [1]&gt; OK    </span></span>
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a><span class="co">#&gt; 5 Arlington  &lt;list [2]&gt; 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">%&gt;%</span></span>
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(json) <span class="sc">%&gt;%</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">#&gt; # A tibble: 7 × 3</span></span>
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="co">#&gt;   city       results          status</span></span>
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;      &lt;list&gt;           &lt;chr&gt; </span></span>
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a><span class="co">#&gt; 1 Houston    &lt;named list [5]&gt; OK    </span></span>
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="co">#&gt; 2 Washington &lt;named list [5]&gt; OK    </span></span>
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a><span class="co">#&gt; 3 Washington &lt;named list [5]&gt; OK    </span></span>
<span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a><span class="co">#&gt; 4 New York   &lt;named list [5]&gt; OK    </span></span>
<span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a><span class="co">#&gt; 5 Chicago    &lt;named list [5]&gt; OK    </span></span>
<span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a><span class="co">#&gt; 6 Arlington  &lt;named list [5]&gt; OK    </span></span>
<span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a><span class="co">#&gt; # ℹ 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">%&gt;%</span></span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(json) <span class="sc">%&gt;%</span> </span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a>  <span class="fu">unnest_longer</span>(results) <span class="sc">%&gt;%</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">#&gt; # A tibble: 7 × 7</span></span>
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co">#&gt;   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">#&gt;   &lt;chr&gt; &lt;list&gt;             &lt;chr&gt;             &lt;list&gt;       &lt;chr&gt;    &lt;list&gt; &lt;chr&gt; </span></span>
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a><span class="co">#&gt; 1 Hous… &lt;list [4]&gt;         Houston, TX, USA  &lt;named list&gt; ChIJAYW… &lt;list&gt; OK    </span></span>
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a><span class="co">#&gt; 2 Wash… &lt;list [2]&gt;         Washington, USA   &lt;named list&gt; ChIJ-bD… &lt;list&gt; OK    </span></span>
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a><span class="co">#&gt; 3 Wash… &lt;list [4]&gt;         Washington, DC, … &lt;named list&gt; ChIJW-T… &lt;list&gt; OK    </span></span>
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="co">#&gt; 4 New … &lt;list [3]&gt;         New York, NY, USA &lt;named list&gt; ChIJOwg… &lt;list&gt; OK    </span></span>
<span id="cb17-12"><a href="#cb17-12" tabindex="-1"></a><span class="co">#&gt; 5 Chic… &lt;list [4]&gt;         Chicago, IL, USA  &lt;named list&gt; ChIJ7cv… &lt;list&gt; OK    </span></span>
<span id="cb17-13"><a href="#cb17-13" tabindex="-1"></a><span class="co">#&gt; 6 Arli… &lt;list [4]&gt;         Arlington, TX, U… &lt;named list&gt; ChIJ05g… &lt;list&gt; OK    </span></span>
<span id="cb17-14"><a href="#cb17-14" tabindex="-1"></a><span class="co">#&gt; # ℹ 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">%&gt;%</span></span>
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(json) <span class="sc">%&gt;%</span> </span>
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a>  <span class="fu">unnest_longer</span>(results) <span class="sc">%&gt;%</span> </span>
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(results) <span class="sc">%&gt;%</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">#&gt; # A tibble: 7 × 10</span></span>
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="co">#&gt;   city       address_components formatted_address   bounds       location    </span></span>
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;      &lt;list&gt;             &lt;chr&gt;               &lt;list&gt;       &lt;list&gt;      </span></span>
<span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a><span class="co">#&gt; 1 Houston    &lt;list [4]&gt;         Houston, TX, USA    &lt;named list&gt; &lt;named list&gt;</span></span>
<span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a><span class="co">#&gt; 2 Washington &lt;list [2]&gt;         Washington, USA     &lt;named list&gt; &lt;named list&gt;</span></span>
<span id="cb18-11"><a href="#cb18-11" tabindex="-1"></a><span class="co">#&gt; 3 Washington &lt;list [4]&gt;         Washington, DC, USA &lt;named list&gt; &lt;named list&gt;</span></span>
<span id="cb18-12"><a href="#cb18-12" tabindex="-1"></a><span class="co">#&gt; 4 New York   &lt;list [3]&gt;         New York, NY, USA   &lt;named list&gt; &lt;named list&gt;</span></span>
<span id="cb18-13"><a href="#cb18-13" tabindex="-1"></a><span class="co">#&gt; 5 Chicago    &lt;list [4]&gt;         Chicago, IL, USA    &lt;named list&gt; &lt;named list&gt;</span></span>
<span id="cb18-14"><a href="#cb18-14" tabindex="-1"></a><span class="co">#&gt; 6 Arlington  &lt;list [4]&gt;         Arlington, TX, USA  &lt;named list&gt; &lt;named list&gt;</span></span>
<span id="cb18-15"><a href="#cb18-15" tabindex="-1"></a><span class="co">#&gt; # ℹ 1 more row</span></span>
<span id="cb18-16"><a href="#cb18-16" tabindex="-1"></a><span class="co">#&gt; # ℹ 5 more variables: location_type &lt;chr&gt;, viewport &lt;list&gt;, place_id &lt;chr&gt;,</span></span>
<span id="cb18-17"><a href="#cb18-17" tabindex="-1"></a><span class="co">#&gt; #   types &lt;list&gt;, status &lt;chr&gt;</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">%&gt;%</span></span>
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(json) <span class="sc">%&gt;%</span></span>
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a>  <span class="fu">unnest_longer</span>(results) <span class="sc">%&gt;%</span></span>
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(results) <span class="sc">%&gt;%</span></span>
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(geometry) <span class="sc">%&gt;%</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">#&gt; # A tibble: 7 × 11</span></span>
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a><span class="co">#&gt;   city       address_components formatted_address   bounds         lat    lng</span></span>
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;      &lt;list&gt;             &lt;chr&gt;               &lt;list&gt;       &lt;dbl&gt;  &lt;dbl&gt;</span></span>
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a><span class="co">#&gt; 1 Houston    &lt;list [4]&gt;         Houston, TX, USA    &lt;named list&gt;  29.8  -95.4</span></span>
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a><span class="co">#&gt; 2 Washington &lt;list [2]&gt;         Washington, USA     &lt;named list&gt;  47.8 -121. </span></span>
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a><span class="co">#&gt; 3 Washington &lt;list [4]&gt;         Washington, DC, USA &lt;named list&gt;  38.9  -77.0</span></span>
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a><span class="co">#&gt; 4 New York   &lt;list [3]&gt;         New York, NY, USA   &lt;named list&gt;  40.7  -74.0</span></span>
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a><span class="co">#&gt; 5 Chicago    &lt;list [4]&gt;         Chicago, IL, USA    &lt;named list&gt;  41.9  -87.6</span></span>
<span id="cb19-15"><a href="#cb19-15" tabindex="-1"></a><span class="co">#&gt; 6 Arlington  &lt;list [4]&gt;         Arlington, TX, USA  &lt;named list&gt;  32.7  -97.1</span></span>
<span id="cb19-16"><a href="#cb19-16" tabindex="-1"></a><span class="co">#&gt; # ℹ 1 more row</span></span>
<span id="cb19-17"><a href="#cb19-17" tabindex="-1"></a><span class="co">#&gt; # ℹ 5 more variables: location_type &lt;chr&gt;, viewport &lt;list&gt;, place_id &lt;chr&gt;,</span></span>
<span id="cb19-18"><a href="#cb19-18" tabindex="-1"></a><span class="co">#&gt; #   types &lt;list&gt;, status &lt;chr&gt;</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">%&gt;%</span></span>
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(json) <span class="sc">%&gt;%</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">%&gt;%</span></span>
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(first_result) <span class="sc">%&gt;%</span></span>
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(geometry) <span class="sc">%&gt;%</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">#&gt; # A tibble: 5 × 12</span></span>
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a><span class="co">#&gt;   city       address_components formatted_address  bounds             lat    lng</span></span>
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;      &lt;list&gt;             &lt;chr&gt;              &lt;list&gt;           &lt;dbl&gt;  &lt;dbl&gt;</span></span>
<span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a><span class="co">#&gt; 1 Houston    &lt;list [4]&gt;         Houston, TX, USA   &lt;named list [2]&gt;  29.8  -95.4</span></span>
<span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a><span class="co">#&gt; 2 Washington &lt;list [2]&gt;         Washington, USA    &lt;named list [2]&gt;  47.8 -121. </span></span>
<span id="cb20-12"><a href="#cb20-12" tabindex="-1"></a><span class="co">#&gt; 3 New York   &lt;list [3]&gt;         New York, NY, USA  &lt;named list [2]&gt;  40.7  -74.0</span></span>
<span id="cb20-13"><a href="#cb20-13" tabindex="-1"></a><span class="co">#&gt; 4 Chicago    &lt;list [4]&gt;         Chicago, IL, USA   &lt;named list [2]&gt;  41.9  -87.6</span></span>
<span id="cb20-14"><a href="#cb20-14" tabindex="-1"></a><span class="co">#&gt; 5 Arlington  &lt;list [4]&gt;         Arlington, TX, USA &lt;named list [2]&gt;  32.7  -97.1</span></span>
<span id="cb20-15"><a href="#cb20-15" tabindex="-1"></a><span class="co">#&gt; # ℹ 6 more variables: location_type &lt;chr&gt;, viewport &lt;list&gt;, place_id &lt;chr&gt;,</span></span>
<span id="cb20-16"><a href="#cb20-16" tabindex="-1"></a><span class="co">#&gt; #   types &lt;list&gt;, results &lt;list&gt;, status &lt;chr&gt;</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">%&gt;%</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">&quot;results&quot;</span>, <span class="dv">1</span>, <span class="st">&quot;geometry&quot;</span>, <span class="st">&quot;location&quot;</span>, <span class="st">&quot;lat&quot;</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">&quot;results&quot;</span>, <span class="dv">1</span>, <span class="st">&quot;geometry&quot;</span>, <span class="st">&quot;location&quot;</span>, <span class="st">&quot;lng&quot;</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">#&gt; # A tibble: 5 × 4</span></span>
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="co">#&gt;   city         lat    lng json            </span></span>
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;      &lt;dbl&gt;  &lt;dbl&gt; &lt;list&gt;          </span></span>
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="co">#&gt; 1 Houston     29.8  -95.4 &lt;named list [2]&gt;</span></span>
<span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a><span class="co">#&gt; 2 Washington  47.8 -121.  &lt;named list [2]&gt;</span></span>
<span id="cb21-11"><a href="#cb21-11" tabindex="-1"></a><span class="co">#&gt; 3 New York    40.7  -74.0 &lt;named list [2]&gt;</span></span>
<span id="cb21-12"><a href="#cb21-12" tabindex="-1"></a><span class="co">#&gt; 4 Chicago     41.9  -87.6 &lt;named list [2]&gt;</span></span>
<span id="cb21-13"><a href="#cb21-13" tabindex="-1"></a><span class="co">#&gt; 5 Arlington   32.7  -97.1 &lt;named list [2]&gt;</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">&lt;-</span> <span class="fu">tibble</span>(<span class="at">disc =</span> discog) <span class="sc">%&gt;%</span> </span>
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(disc) <span class="sc">%&gt;%</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">&quot;%Y-%m-%dT%H:%M:%S&quot;</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">#&gt; # A tibble: 155 × 5</span></span>
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a><span class="co">#&gt;   instance_id date_added          basic_information       id rating</span></span>
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a><span class="co">#&gt;         &lt;int&gt; &lt;dttm&gt;              &lt;list&gt;               &lt;int&gt;  &lt;int&gt;</span></span>
<span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a><span class="co">#&gt; 1   354823933 2019-02-16 17:48:59 &lt;named list [11]&gt;  7496378      0</span></span>
<span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a><span class="co">#&gt; 2   354092601 2019-02-13 14:13:11 &lt;named list [11]&gt;  4490852      0</span></span>
<span id="cb22-10"><a href="#cb22-10" tabindex="-1"></a><span class="co">#&gt; 3   354091476 2019-02-13 14:07:23 &lt;named list [11]&gt;  9827276      0</span></span>
<span id="cb22-11"><a href="#cb22-11" tabindex="-1"></a><span class="co">#&gt; 4   351244906 2019-02-02 11:39:58 &lt;named list [11]&gt;  9769203      0</span></span>
<span id="cb22-12"><a href="#cb22-12" tabindex="-1"></a><span class="co">#&gt; 5   351244801 2019-02-02 11:39:37 &lt;named list [11]&gt;  7237138      0</span></span>
<span id="cb22-13"><a href="#cb22-13" tabindex="-1"></a><span class="co">#&gt; 6   351052065 2019-02-01 20:40:53 &lt;named list [11]&gt; 13117042      0</span></span>
<span id="cb22-14"><a href="#cb22-14" tabindex="-1"></a><span class="co">#&gt; # ℹ 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">%&gt;%</span> <span class="fu">unnest_wider</span>(basic_information)</span>
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="co">#&gt; Error in `unnest_wider()`:</span></span>
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a><span class="co">#&gt; ! Can&#39;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">#&gt;   data.</span></span>
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a><span class="co">#&gt; ✖ These names are duplicated:</span></span>
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="co">#&gt;   ℹ `id`, from `basic_information`.</span></span>
<span id="cb23-7"><a href="#cb23-7" tabindex="-1"></a><span class="co">#&gt; ℹ 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">#&gt; ℹ 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 = &quot;unique&quot;</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">%&gt;%</span> <span class="fu">unnest_wider</span>(basic_information, <span class="at">names_repair =</span> <span class="st">&quot;unique&quot;</span>)</span>
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="co">#&gt; New names:</span></span>
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a><span class="co">#&gt; • `id` -&gt; `id...7`</span></span>
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a><span class="co">#&gt; • `id` -&gt; `id...14`</span></span>
<span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a><span class="co">#&gt; # A tibble: 155 × 15</span></span>
<span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a><span class="co">#&gt;   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">#&gt;         &lt;int&gt; &lt;dttm&gt;              &lt;list&gt; &lt;int&gt; &lt;chr&gt;        &lt;list&gt;   &lt;int&gt; &lt;chr&gt;</span></span>
<span id="cb24-8"><a href="#cb24-8" tabindex="-1"></a><span class="co">#&gt; 1   354823933 2019-02-16 17:48:59 &lt;list&gt;  2015 &lt;NA&gt;         &lt;list&gt;  7.50e6 http…</span></span>
<span id="cb24-9"><a href="#cb24-9" tabindex="-1"></a><span class="co">#&gt; 2   354092601 2019-02-13 14:13:11 &lt;list&gt;  2013 https://api… &lt;list&gt;  4.49e6 http…</span></span>
<span id="cb24-10"><a href="#cb24-10" tabindex="-1"></a><span class="co">#&gt; 3   354091476 2019-02-13 14:07:23 &lt;list&gt;  2017 https://api… &lt;list&gt;  9.83e6 http…</span></span>
<span id="cb24-11"><a href="#cb24-11" tabindex="-1"></a><span class="co">#&gt; 4   351244906 2019-02-02 11:39:58 &lt;list&gt;  2017 https://api… &lt;list&gt;  9.77e6 http…</span></span>
<span id="cb24-12"><a href="#cb24-12" tabindex="-1"></a><span class="co">#&gt; 5   351244801 2019-02-02 11:39:37 &lt;list&gt;  2015 https://api… &lt;list&gt;  7.24e6 http…</span></span>
<span id="cb24-13"><a href="#cb24-13" tabindex="-1"></a><span class="co">#&gt; 6   351052065 2019-02-01 20:40:53 &lt;list&gt;  2019 https://api… &lt;list&gt;  1.31e7 http…</span></span>
<span id="cb24-14"><a href="#cb24-14" tabindex="-1"></a><span class="co">#&gt; # ℹ 149 more rows</span></span>
<span id="cb24-15"><a href="#cb24-15" tabindex="-1"></a><span class="co">#&gt; # ℹ 7 more variables: title &lt;chr&gt;, formats &lt;list&gt;, cover_image &lt;chr&gt;,</span></span>
<span id="cb24-16"><a href="#cb24-16" tabindex="-1"></a><span class="co">#&gt; #   resource_url &lt;chr&gt;, master_id &lt;int&gt;, id...14 &lt;int&gt;, rating &lt;int&gt;</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">%&gt;%</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">%&gt;%</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">#&gt; # A tibble: 155 × 14</span></span>
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a><span class="co">#&gt;   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">#&gt;         &lt;int&gt; &lt;dttm&gt;              &lt;list&gt; &lt;int&gt; &lt;chr&gt;        &lt;list&gt;   &lt;int&gt; &lt;chr&gt;</span></span>
<span id="cb25-7"><a href="#cb25-7" tabindex="-1"></a><span class="co">#&gt; 1   354823933 2019-02-16 17:48:59 &lt;list&gt;  2015 &lt;NA&gt;         &lt;list&gt;  7.50e6 http…</span></span>
<span id="cb25-8"><a href="#cb25-8" tabindex="-1"></a><span class="co">#&gt; 2   354092601 2019-02-13 14:13:11 &lt;list&gt;  2013 https://api… &lt;list&gt;  4.49e6 http…</span></span>
<span id="cb25-9"><a href="#cb25-9" tabindex="-1"></a><span class="co">#&gt; 3   354091476 2019-02-13 14:07:23 &lt;list&gt;  2017 https://api… &lt;list&gt;  9.83e6 http…</span></span>
<span id="cb25-10"><a href="#cb25-10" tabindex="-1"></a><span class="co">#&gt; 4   351244906 2019-02-02 11:39:58 &lt;list&gt;  2017 https://api… &lt;list&gt;  9.77e6 http…</span></span>
<span id="cb25-11"><a href="#cb25-11" tabindex="-1"></a><span class="co">#&gt; 5   351244801 2019-02-02 11:39:37 &lt;list&gt;  2015 https://api… &lt;list&gt;  7.24e6 http…</span></span>
<span id="cb25-12"><a href="#cb25-12" tabindex="-1"></a><span class="co">#&gt; 6   351052065 2019-02-01 20:40:53 &lt;list&gt;  2019 https://api… &lt;list&gt;  1.31e7 http…</span></span>
<span id="cb25-13"><a href="#cb25-13" tabindex="-1"></a><span class="co">#&gt; # ℹ 149 more rows</span></span>
<span id="cb25-14"><a href="#cb25-14" tabindex="-1"></a><span class="co">#&gt; # ℹ 6 more variables: title &lt;chr&gt;, formats &lt;list&gt;, cover_image &lt;chr&gt;,</span></span>
<span id="cb25-15"><a href="#cb25-15" tabindex="-1"></a><span class="co">#&gt; #   resource_url &lt;chr&gt;, master_id &lt;int&gt;, rating &lt;int&gt;</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">%&gt;%</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">&quot;title&quot;</span>,</span>
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a>    <span class="at">year =</span> <span class="st">&quot;year&quot;</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">&quot;labels&quot;</span>, <span class="dv">1</span>, <span class="st">&quot;name&quot;</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">&quot;artists&quot;</span>, <span class="dv">1</span>, <span class="st">&quot;name&quot;</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">#&gt; # A tibble: 155 × 9</span></span>
<span id="cb26-9"><a href="#cb26-9" tabindex="-1"></a><span class="co">#&gt;   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">#&gt;         &lt;int&gt; &lt;dttm&gt;              &lt;chr&gt;     &lt;int&gt; &lt;chr&gt; &lt;chr&gt;  &lt;list&gt;           </span></span>
<span id="cb26-11"><a href="#cb26-11" tabindex="-1"></a><span class="co">#&gt; 1   354823933 2019-02-16 17:48:59 Demo       2015 Tobi… Mollot &lt;named list [9]&gt; </span></span>
<span id="cb26-12"><a href="#cb26-12" tabindex="-1"></a><span class="co">#&gt; 2   354092601 2019-02-13 14:13:11 Observan…  2013 La V… Una B… &lt;named list [9]&gt; </span></span>
<span id="cb26-13"><a href="#cb26-13" tabindex="-1"></a><span class="co">#&gt; 3   354091476 2019-02-13 14:07:23 I          2017 La V… S.H.I… &lt;named list [9]&gt; </span></span>
<span id="cb26-14"><a href="#cb26-14" tabindex="-1"></a><span class="co">#&gt; 4   351244906 2019-02-02 11:39:58 Oído Abs…  2017 La V… Rata … &lt;named list [9]&gt; </span></span>
<span id="cb26-15"><a href="#cb26-15" tabindex="-1"></a><span class="co">#&gt; 5   351244801 2019-02-02 11:39:37 A Cat&#39;s …  2015 Kato… Ivy (… &lt;named list [9]&gt; </span></span>
<span id="cb26-16"><a href="#cb26-16" tabindex="-1"></a><span class="co">#&gt; 6   351052065 2019-02-01 20:40:53 Tashme     2019 High… Tashme &lt;named list [9]&gt; </span></span>
<span id="cb26-17"><a href="#cb26-17" tabindex="-1"></a><span class="co">#&gt; # ℹ 149 more rows</span></span>
<span id="cb26-18"><a href="#cb26-18" tabindex="-1"></a><span class="co">#&gt; # ℹ 2 more variables: id &lt;int&gt;, rating &lt;int&gt;</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">%&gt;%</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">&quot;artists&quot;</span>) <span class="sc">%&gt;%</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">%&gt;%</span> </span>
<span id="cb27-4"><a href="#cb27-4" tabindex="-1"></a>  <span class="fu">unnest_longer</span>(artist) <span class="sc">%&gt;%</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">#&gt; # A tibble: 167 × 8</span></span>
<span id="cb27-7"><a href="#cb27-7" tabindex="-1"></a><span class="co">#&gt;    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">#&gt;      &lt;int&gt; &lt;chr&gt; &lt;chr&gt;                    &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt; &lt;chr&gt;         &lt;int&gt;</span></span>
<span id="cb27-9"><a href="#cb27-9" tabindex="-1"></a><span class="co">#&gt; 1  7496378 &quot;&quot;    Mollot                   &quot;&quot;    &quot;&quot;     &quot;&quot;    https://api… 4.62e6</span></span>
<span id="cb27-10"><a href="#cb27-10" tabindex="-1"></a><span class="co">#&gt; 2  4490852 &quot;&quot;    Una Bèstia Incontrolable &quot;&quot;    &quot;&quot;     &quot;&quot;    https://api… 3.19e6</span></span>
<span id="cb27-11"><a href="#cb27-11" tabindex="-1"></a><span class="co">#&gt; 3  9827276 &quot;&quot;    S.H.I.T. (3)             &quot;&quot;    &quot;&quot;     &quot;&quot;    https://api… 2.77e6</span></span>
<span id="cb27-12"><a href="#cb27-12" tabindex="-1"></a><span class="co">#&gt; 4  9769203 &quot;&quot;    Rata Negra               &quot;&quot;    &quot;&quot;     &quot;&quot;    https://api… 4.28e6</span></span>
<span id="cb27-13"><a href="#cb27-13" tabindex="-1"></a><span class="co">#&gt; 5  7237138 &quot;&quot;    Ivy (18)                 &quot;&quot;    &quot;&quot;     &quot;&quot;    https://api… 3.60e6</span></span>
<span id="cb27-14"><a href="#cb27-14" tabindex="-1"></a><span class="co">#&gt; 6 13117042 &quot;&quot;    Tashme                   &quot;&quot;    &quot;&quot;     &quot;&quot;    https://api… 5.21e6</span></span>
<span id="cb27-15"><a href="#cb27-15" tabindex="-1"></a><span class="co">#&gt; # ℹ 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">%&gt;%</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">&quot;formats&quot;</span>) <span class="sc">%&gt;%</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">%&gt;%</span> </span>
<span id="cb27-20"><a href="#cb27-20" tabindex="-1"></a>  <span class="fu">unnest_longer</span>(format) <span class="sc">%&gt;%</span> </span>
<span id="cb27-21"><a href="#cb27-21" tabindex="-1"></a>  <span class="fu">unnest_wider</span>(format) <span class="sc">%&gt;%</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">#&gt; # A tibble: 258 × 5</span></span>
<span id="cb27-24"><a href="#cb27-24" tabindex="-1"></a><span class="co">#&gt;   disc_id descriptions text  name     qty  </span></span>
<span id="cb27-25"><a href="#cb27-25" tabindex="-1"></a><span class="co">#&gt;     &lt;int&gt; &lt;chr&gt;        &lt;chr&gt; &lt;chr&gt;    &lt;chr&gt;</span></span>
<span id="cb27-26"><a href="#cb27-26" tabindex="-1"></a><span class="co">#&gt; 1 7496378 &quot;Numbered&quot;   Black Cassette 1    </span></span>
<span id="cb27-27"><a href="#cb27-27" tabindex="-1"></a><span class="co">#&gt; 2 4490852 &quot;LP&quot;         &lt;NA&gt;  Vinyl    1    </span></span>
<span id="cb27-28"><a href="#cb27-28" tabindex="-1"></a><span class="co">#&gt; 3 9827276 &quot;7\&quot;&quot;        &lt;NA&gt;  Vinyl    1    </span></span>
<span id="cb27-29"><a href="#cb27-29" tabindex="-1"></a><span class="co">#&gt; 4 9827276 &quot;45 RPM&quot;     &lt;NA&gt;  Vinyl    1    </span></span>
<span id="cb27-30"><a href="#cb27-30" tabindex="-1"></a><span class="co">#&gt; 5 9827276 &quot;EP&quot;         &lt;NA&gt;  Vinyl    1    </span></span>
<span id="cb27-31"><a href="#cb27-31" tabindex="-1"></a><span class="co">#&gt; 6 9769203 &quot;LP&quot;         &lt;NA&gt;  Vinyl    1    </span></span>
<span id="cb27-32"><a href="#cb27-32" tabindex="-1"></a><span class="co">#&gt; # ℹ 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>