More work on O'Reilly book
* Make width narrower * Convert deps to table * Strip chapter status
This commit is contained in:
		@@ -1,29 +1,5 @@
 | 
			
		||||
<section data-type="chapter" id="chp-rectangling">
 | 
			
		||||
<h1><span id="sec-rectangling" class="quarto-section-identifier d-none d-lg-block"><span class="chapter-title">Data rectangling</span></span></h1><div data-type="note"><div class="callout-body d-flex">
 | 
			
		||||
<div class="callout-icon-container">
 | 
			
		||||
<i class="callout-icon"/>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
</div><h1>
 | 
			
		||||
Base R
 | 
			
		||||
</h1><p>You are reading the work-in-progress second edition of R for Data Science. This chapter should be readable but is currently undergoing final polishing. You can find the complete first edition at <a href="https://r4ds.had.co.nz" class="uri">https://r4ds.had.co.nz</a>.</p>
 | 
			
		||||
 | 
			
		||||
<p>It’s possible to put a list in a column of a <code>data.frame</code>, but it’s a lot fiddlier because <code><a href="https://rdrr.io/r/base/data.frame.html">data.frame()</a></code> treats a list as a list of columns:</p><div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">data.frame(x = list(1:3, 3:5))
 | 
			
		||||
#>   x.1.3 x.3.5
 | 
			
		||||
#> 1     1     3
 | 
			
		||||
#> 2     2     4
 | 
			
		||||
#> 3     3     5</pre>
 | 
			
		||||
</div><p>You can force <code><a href="https://rdrr.io/r/base/data.frame.html">data.frame()</a></code> to treat a list as a list of rows by wrapping it in list <code><a href="https://rdrr.io/r/base/AsIs.html">I()</a></code>, but the result doesn’t print particularly well:</p><div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">data.frame(
 | 
			
		||||
  x = I(list(1:2, 3:5)), 
 | 
			
		||||
  y = c("1, 2", "3, 4, 5")
 | 
			
		||||
)
 | 
			
		||||
#>         x       y
 | 
			
		||||
#> 1    1, 2    1, 2
 | 
			
		||||
#> 2 3, 4, 5 3, 4, 5</pre>
 | 
			
		||||
</div><p>It’s easier to use list-columns with tibbles because <code><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble()</a></code> treats lists like either vectors and the print method has been designed with lists in mind.</p></div>
 | 
			
		||||
 | 
			
		||||
<h1><span id="sec-rectangling" class="quarto-section-identifier d-none d-lg-block"><span class="chapter-title">Data rectangling</span></span></h1><p>::: status callout-note You are reading the work-in-progress second edition of R for Data Science. This chapter should be readable but is currently undergoing final polishing. You can find the complete first edition at <a href="https://r4ds.had.co.nz" class="uri">https://r4ds.had.co.nz</a>. :::</p>
 | 
			
		||||
<section id="introduction" data-type="sect1">
 | 
			
		||||
<h1>
 | 
			
		||||
Introduction</h1>
 | 
			
		||||
@@ -198,9 +174,7 @@ df
 | 
			
		||||
<p>Similarly, if you <code><a href="https://rdrr.io/r/utils/View.html">View()</a></code> a data frame in RStudio, you’ll get the standard tabular view, which doesn’t allow you to selectively expand list columns. To explore those fields you’ll need to <code><a href="https://dplyr.tidyverse.org/reference/pull.html">pull()</a></code> and view, e.g. <code>df |> pull(z) |> View()</code>.</p>
 | 
			
		||||
<div data-type="note"><h1>
 | 
			
		||||
Base R
 | 
			
		||||
</h1><p>You are reading the work-in-progress second edition of R for Data Science. This chapter should be readable but is currently undergoing final polishing. You can find the complete first edition at <a href="https://r4ds.had.co.nz" class="uri">https://r4ds.had.co.nz</a>.</p>
 | 
			
		||||
 | 
			
		||||
<p>It’s possible to put a list in a column of a <code>data.frame</code>, but it’s a lot fiddlier because <code><a href="https://rdrr.io/r/base/data.frame.html">data.frame()</a></code> treats a list as a list of columns:</p><div class="cell">
 | 
			
		||||
</h1><p>It’s possible to put a list in a column of a <code>data.frame</code>, but it’s a lot fiddlier because <code><a href="https://rdrr.io/r/base/data.frame.html">data.frame()</a></code> treats a list as a list of columns:</p><div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">data.frame(x = list(1:3, 3:5))
 | 
			
		||||
#>   x.1.3 x.3.5
 | 
			
		||||
#> 1     1     3
 | 
			
		||||
@@ -486,15 +460,15 @@ repos
 | 
			
		||||
  unnest_longer(json) |> 
 | 
			
		||||
  unnest_wider(json) 
 | 
			
		||||
#> # A tibble: 176 × 68
 | 
			
		||||
#>        id name  full_…¹ owner        private html_…² descr…³ fork  url   forks…⁴
 | 
			
		||||
#>     <int> <chr> <chr>   <list>       <lgl>   <chr>   <chr>   <lgl> <chr> <chr>  
 | 
			
		||||
#> 1  6.12e7 after gaborc… <named list> FALSE   https:… Run Co… FALSE http… https:…
 | 
			
		||||
#> 2  4.05e7 argu… gaborc… <named list> FALSE   https:… Declar… FALSE http… https:…
 | 
			
		||||
#> 3  3.64e7 ask   gaborc… <named list> FALSE   https:… Friend… FALSE http… https:…
 | 
			
		||||
#> 4  3.49e7 base… gaborc… <named list> FALSE   https:… Do we … FALSE http… https:…
 | 
			
		||||
#> 5  6.16e7 cite… gaborc… <named list> FALSE   https:… Test R… TRUE  http… https:…
 | 
			
		||||
#> 6  3.39e7 clis… gaborc… <named list> FALSE   https:… Unicod… FALSE http… https:…
 | 
			
		||||
#> # … with 170 more rows, 58 more variables: keys_url <chr>,
 | 
			
		||||
#>         id name      full_…¹ owner        private html_…² descr…³ fork  url  
 | 
			
		||||
#>      <int> <chr>     <chr>   <list>       <lgl>   <chr>   <chr>   <lgl> <chr>
 | 
			
		||||
#> 1 61160198 after     gaborc… <named list> FALSE   https:… Run Co… FALSE http…
 | 
			
		||||
#> 2 40500181 argufy    gaborc… <named list> FALSE   https:… Declar… FALSE http…
 | 
			
		||||
#> 3 36442442 ask       gaborc… <named list> FALSE   https:… Friend… FALSE http…
 | 
			
		||||
#> 4 34924886 baseimpo… gaborc… <named list> FALSE   https:… Do we … FALSE http…
 | 
			
		||||
#> 5 61620661 citest    gaborc… <named list> FALSE   https:… Test R… TRUE  http…
 | 
			
		||||
#> 6 33907457 clisymbo… gaborc… <named list> FALSE   https:… Unicod… FALSE http…
 | 
			
		||||
#> # … with 170 more rows, 59 more variables: forks_url <chr>, keys_url <chr>,
 | 
			
		||||
#> #   collaborators_url <chr>, teams_url <chr>, hooks_url <chr>,
 | 
			
		||||
#> #   issue_events_url <chr>, events_url <chr>, assignees_url <chr>,
 | 
			
		||||
#> #   branches_url <chr>, tags_url <chr>, blobs_url <chr>, git_tags_url <chr>,
 | 
			
		||||
@@ -539,14 +513,14 @@ repos
 | 
			
		||||
  unnest_wider(json) |> 
 | 
			
		||||
  select(id, full_name, owner, description)
 | 
			
		||||
#> # A tibble: 176 × 4
 | 
			
		||||
#>         id full_name               owner             description                
 | 
			
		||||
#>      <int> <chr>                   <list>            <chr>                      
 | 
			
		||||
#> 1 61160198 gaborcsardi/after       <named list [17]> Run Code in the Background 
 | 
			
		||||
#> 2 40500181 gaborcsardi/argufy      <named list [17]> Declarative function argum…
 | 
			
		||||
#> 3 36442442 gaborcsardi/ask         <named list [17]> Friendly CLI interaction i…
 | 
			
		||||
#> 4 34924886 gaborcsardi/baseimports <named list [17]> Do we get warnings for und…
 | 
			
		||||
#> 5 61620661 gaborcsardi/citest      <named list [17]> Test R package and repo fo…
 | 
			
		||||
#> 6 33907457 gaborcsardi/clisymbols  <named list [17]> Unicode symbols for CLI ap…
 | 
			
		||||
#>         id full_name               owner             description             
 | 
			
		||||
#>      <int> <chr>                   <list>            <chr>                   
 | 
			
		||||
#> 1 61160198 gaborcsardi/after       <named list [17]> Run Code in the Backgro…
 | 
			
		||||
#> 2 40500181 gaborcsardi/argufy      <named list [17]> Declarative function ar…
 | 
			
		||||
#> 3 36442442 gaborcsardi/ask         <named list [17]> Friendly CLI interactio…
 | 
			
		||||
#> 4 34924886 gaborcsardi/baseimports <named list [17]> Do we get warnings for …
 | 
			
		||||
#> 5 61620661 gaborcsardi/citest      <named list [17]> Test R package and repo…
 | 
			
		||||
#> 6 33907457 gaborcsardi/clisymbols  <named list [17]> Unicode symbols for CLI…
 | 
			
		||||
#> # … with 170 more rows</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>You can use this to work back to understand how <code>gh_repos</code> was strucured: each child was a GitHub user containing a list of up to 30 GitHub repositories that they created.</p>
 | 
			
		||||
@@ -572,21 +546,21 @@ repos
 | 
			
		||||
  select(id, full_name, owner, description) |> 
 | 
			
		||||
  unnest_wider(owner, names_sep = "_")
 | 
			
		||||
#> # A tibble: 176 × 20
 | 
			
		||||
#>       id full_…¹ owner…² owner…³ owner…⁴ owner…⁵ owner…⁶ owner…⁷ owner…⁸ owner…⁹
 | 
			
		||||
#>    <int> <chr>   <chr>     <int> <chr>   <chr>   <chr>   <chr>   <chr>   <chr>  
 | 
			
		||||
#> 1 6.12e7 gaborc… gaborc…  660288 https:… ""      https:… https:… https:… https:…
 | 
			
		||||
#> 2 4.05e7 gaborc… gaborc…  660288 https:… ""      https:… https:… https:… https:…
 | 
			
		||||
#> 3 3.64e7 gaborc… gaborc…  660288 https:… ""      https:… https:… https:… https:…
 | 
			
		||||
#> 4 3.49e7 gaborc… gaborc…  660288 https:… ""      https:… https:… https:… https:…
 | 
			
		||||
#> 5 6.16e7 gaborc… gaborc…  660288 https:… ""      https:… https:… https:… https:…
 | 
			
		||||
#> 6 3.39e7 gaborc… gaborc…  660288 https:… ""      https:… https:… https:… https:…
 | 
			
		||||
#> # … with 170 more rows, 10 more variables: owner_gists_url <chr>,
 | 
			
		||||
#> #   owner_starred_url <chr>, owner_subscriptions_url <chr>,
 | 
			
		||||
#> #   owner_organizations_url <chr>, owner_repos_url <chr>,
 | 
			
		||||
#> #   owner_events_url <chr>, owner_received_events_url <chr>, owner_type <chr>,
 | 
			
		||||
#> #   owner_site_admin <lgl>, description <chr>, and abbreviated variable names
 | 
			
		||||
#> #   ¹full_name, ²owner_login, ³owner_id, ⁴owner_avatar_url, ⁵owner_gravatar_id,
 | 
			
		||||
#> #   ⁶owner_url, ⁷owner_html_url, ⁸owner_followers_url, ⁹owner_following_url</pre>
 | 
			
		||||
#>         id full_name  owner…¹ owner…² owner…³ owner…⁴ owner…⁵ owner…⁶ owner…⁷
 | 
			
		||||
#>      <int> <chr>      <chr>     <int> <chr>   <chr>   <chr>   <chr>   <chr>  
 | 
			
		||||
#> 1 61160198 gaborcsar… gaborc…  660288 https:… ""      https:… https:… https:…
 | 
			
		||||
#> 2 40500181 gaborcsar… gaborc…  660288 https:… ""      https:… https:… https:…
 | 
			
		||||
#> 3 36442442 gaborcsar… gaborc…  660288 https:… ""      https:… https:… https:…
 | 
			
		||||
#> 4 34924886 gaborcsar… gaborc…  660288 https:… ""      https:… https:… https:…
 | 
			
		||||
#> 5 61620661 gaborcsar… gaborc…  660288 https:… ""      https:… https:… https:…
 | 
			
		||||
#> 6 33907457 gaborcsar… gaborc…  660288 https:… ""      https:… https:… https:…
 | 
			
		||||
#> # … with 170 more rows, 11 more variables: owner_following_url <chr>,
 | 
			
		||||
#> #   owner_gists_url <chr>, owner_starred_url <chr>,
 | 
			
		||||
#> #   owner_subscriptions_url <chr>, owner_organizations_url <chr>,
 | 
			
		||||
#> #   owner_repos_url <chr>, owner_events_url <chr>,
 | 
			
		||||
#> #   owner_received_events_url <chr>, owner_type <chr>,
 | 
			
		||||
#> #   owner_site_admin <lgl>, description <chr>, and abbreviated variable
 | 
			
		||||
#> #   names ¹owner_login, ²owner_id, ³owner_avatar_url, ⁴owner_gravatar_id, …</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>This gives another wide dataset, but you can see that <code>owner</code> appears to contain a lot of additional data about the person who “owns” the repository.</p>
 | 
			
		||||
</section>
 | 
			
		||||
@@ -614,14 +588,14 @@ chars
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">chars |> 
 | 
			
		||||
  unnest_wider(json)
 | 
			
		||||
#> # A tibble: 30 × 18
 | 
			
		||||
#>   url            id name  gender culture born  died  alive titles aliases father
 | 
			
		||||
#>   <chr>       <int> <chr> <chr>  <chr>   <chr> <chr> <lgl> <list> <list>  <chr> 
 | 
			
		||||
#> 1 https://ww…  1022 Theo… Male   "Ironb… "In … ""    TRUE  <chr>  <chr>   ""    
 | 
			
		||||
#> 2 https://ww…  1052 Tyri… Male   ""      "In … ""    TRUE  <chr>  <chr>   ""    
 | 
			
		||||
#> 3 https://ww…  1074 Vict… Male   "Ironb… "In … ""    TRUE  <chr>  <chr>   ""    
 | 
			
		||||
#> 4 https://ww…  1109 Will  Male   ""      ""    "In … FALSE <chr>  <chr>   ""    
 | 
			
		||||
#> 5 https://ww…  1166 Areo… Male   "Norvo… "In … ""    TRUE  <chr>  <chr>   ""    
 | 
			
		||||
#> 6 https://ww…  1267 Chett Male   ""      "At … "In … FALSE <chr>  <chr>   ""    
 | 
			
		||||
#>   url         id name  gender culture born  died  alive titles aliases father
 | 
			
		||||
#>   <chr>    <int> <chr> <chr>  <chr>   <chr> <chr> <lgl> <list> <list>  <chr> 
 | 
			
		||||
#> 1 https:/…  1022 Theo… Male   "Ironb… "In … ""    TRUE  <chr>  <chr>   ""    
 | 
			
		||||
#> 2 https:/…  1052 Tyri… Male   ""      "In … ""    TRUE  <chr>  <chr>   ""    
 | 
			
		||||
#> 3 https:/…  1074 Vict… Male   "Ironb… "In … ""    TRUE  <chr>  <chr>   ""    
 | 
			
		||||
#> 4 https:/…  1109 Will  Male   ""      ""    "In … FALSE <chr>  <chr>   ""    
 | 
			
		||||
#> 5 https:/…  1166 Areo… Male   "Norvo… "In … ""    TRUE  <chr>  <chr>   ""    
 | 
			
		||||
#> 6 https:/…  1267 Chett Male   ""      "At … "In … FALSE <chr>  <chr>   ""    
 | 
			
		||||
#> # … with 24 more rows, and 7 more variables: mother <chr>, spouse <chr>,
 | 
			
		||||
#> #   allegiances <list>, books <list>, povBooks <list>, tvSeries <list>,
 | 
			
		||||
#> #   playedBy <list></pre>
 | 
			
		||||
@@ -633,14 +607,14 @@ chars
 | 
			
		||||
  select(id, name, gender, culture, born, died, alive)
 | 
			
		||||
characters
 | 
			
		||||
#> # A tibble: 30 × 7
 | 
			
		||||
#>      id name              gender culture    born                     died  alive
 | 
			
		||||
#>   <int> <chr>             <chr>  <chr>      <chr>                    <chr> <lgl>
 | 
			
		||||
#> 1  1022 Theon Greyjoy     Male   "Ironborn" "In 278 AC or 279 AC, a… ""    TRUE 
 | 
			
		||||
#> 2  1052 Tyrion Lannister  Male   ""         "In 273 AC, at Casterly… ""    TRUE 
 | 
			
		||||
#> 3  1074 Victarion Greyjoy Male   "Ironborn" "In 268 AC or before, a… ""    TRUE 
 | 
			
		||||
#> 4  1109 Will              Male   ""         ""                       "In … FALSE
 | 
			
		||||
#> 5  1166 Areo Hotah        Male   "Norvoshi" "In 257 AC or before, a… ""    TRUE 
 | 
			
		||||
#> 6  1267 Chett             Male   ""         "At Hag's Mire"          "In … FALSE
 | 
			
		||||
#>      id name              gender culture    born                  died  alive
 | 
			
		||||
#>   <int> <chr>             <chr>  <chr>      <chr>                 <chr> <lgl>
 | 
			
		||||
#> 1  1022 Theon Greyjoy     Male   "Ironborn" "In 278 AC or 279 AC… ""    TRUE 
 | 
			
		||||
#> 2  1052 Tyrion Lannister  Male   ""         "In 273 AC, at Caste… ""    TRUE 
 | 
			
		||||
#> 3  1074 Victarion Greyjoy Male   "Ironborn" "In 268 AC or before… ""    TRUE 
 | 
			
		||||
#> 4  1109 Will              Male   ""         ""                    "In … FALSE
 | 
			
		||||
#> 5  1166 Areo Hotah        Male   "Norvoshi" "In 257 AC or before… ""    TRUE 
 | 
			
		||||
#> 6  1267 Chett             Male   ""         "At Hag's Mire"       "In … FALSE
 | 
			
		||||
#> # … with 24 more rows</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>There are also many list-columns:</p>
 | 
			
		||||
@@ -649,15 +623,15 @@ characters
 | 
			
		||||
  unnest_wider(json) |> 
 | 
			
		||||
  select(id, where(is.list))
 | 
			
		||||
#> # A tibble: 30 × 8
 | 
			
		||||
#>      id titles    aliases    allegiances books     povBooks  tvSeries  playedBy 
 | 
			
		||||
#>   <int> <list>    <list>     <list>      <list>    <list>    <list>    <list>   
 | 
			
		||||
#> 1  1022 <chr [3]> <chr [4]>  <chr [1]>   <chr [3]> <chr [2]> <chr [6]> <chr [1]>
 | 
			
		||||
#> 2  1052 <chr [2]> <chr [11]> <chr [1]>   <chr [2]> <chr [4]> <chr [6]> <chr [1]>
 | 
			
		||||
#> 3  1074 <chr [2]> <chr [1]>  <chr [1]>   <chr [3]> <chr [2]> <chr [1]> <chr [1]>
 | 
			
		||||
#> 4  1109 <chr [1]> <chr [1]>  <NULL>      <chr [1]> <chr [1]> <chr [1]> <chr [1]>
 | 
			
		||||
#> 5  1166 <chr [1]> <chr [1]>  <chr [1]>   <chr [3]> <chr [2]> <chr [2]> <chr [1]>
 | 
			
		||||
#> 6  1267 <chr [1]> <chr [1]>  <NULL>      <chr [2]> <chr [1]> <chr [1]> <chr [1]>
 | 
			
		||||
#> # … with 24 more rows</pre>
 | 
			
		||||
#>      id titles    aliases    allegiances books     povBooks  tvSeries playe…¹
 | 
			
		||||
#>   <int> <list>    <list>     <list>      <list>    <list>    <list>   <list> 
 | 
			
		||||
#> 1  1022 <chr [3]> <chr [4]>  <chr [1]>   <chr [3]> <chr [2]> <chr>    <chr>  
 | 
			
		||||
#> 2  1052 <chr [2]> <chr [11]> <chr [1]>   <chr [2]> <chr [4]> <chr>    <chr>  
 | 
			
		||||
#> 3  1074 <chr [2]> <chr [1]>  <chr [1]>   <chr [3]> <chr [2]> <chr>    <chr>  
 | 
			
		||||
#> 4  1109 <chr [1]> <chr [1]>  <NULL>      <chr [1]> <chr [1]> <chr>    <chr>  
 | 
			
		||||
#> 5  1166 <chr [1]> <chr [1]>  <chr [1]>   <chr [3]> <chr [2]> <chr>    <chr>  
 | 
			
		||||
#> 6  1267 <chr [1]> <chr [1]>  <NULL>      <chr [2]> <chr [1]> <chr>    <chr>  
 | 
			
		||||
#> # … with 24 more rows, and abbreviated variable name ¹playedBy</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Lets explore the <code>titles</code> column. It’s an unnamed list-column, so we’ll unnest it into rows:</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
@@ -713,14 +687,14 @@ characters |>
 | 
			
		||||
  select(id, name) |> 
 | 
			
		||||
  inner_join(titles, by = "id", multiple = "all")
 | 
			
		||||
#> # A tibble: 53 × 3
 | 
			
		||||
#>      id name              title                                               
 | 
			
		||||
#>   <int> <chr>             <chr>                                               
 | 
			
		||||
#> 1  1022 Theon Greyjoy     Prince of Winterfell                                
 | 
			
		||||
#> 2  1022 Theon Greyjoy     Captain of Sea Bitch                                
 | 
			
		||||
#> 3  1022 Theon Greyjoy     Lord of the Iron Islands (by law of the green lands)
 | 
			
		||||
#> 4  1052 Tyrion Lannister  Acting Hand of the King (former)                    
 | 
			
		||||
#> 5  1052 Tyrion Lannister  Master of Coin (former)                             
 | 
			
		||||
#> 6  1074 Victarion Greyjoy Lord Captain of the Iron Fleet                      
 | 
			
		||||
#>      id name              title                                              
 | 
			
		||||
#>   <int> <chr>             <chr>                                              
 | 
			
		||||
#> 1  1022 Theon Greyjoy     Prince of Winterfell                               
 | 
			
		||||
#> 2  1022 Theon Greyjoy     Captain of Sea Bitch                               
 | 
			
		||||
#> 3  1022 Theon Greyjoy     Lord of the Iron Islands (by law of the green land…
 | 
			
		||||
#> 4  1052 Tyrion Lannister  Acting Hand of the King (former)                   
 | 
			
		||||
#> 5  1052 Tyrion Lannister  Master of Coin (former)                            
 | 
			
		||||
#> 6  1074 Victarion Greyjoy Lord Captain of the Iron Fleet                     
 | 
			
		||||
#> # … with 47 more rows</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>You could imagine creating a table like this for each of the list-columns, then using joins to combine them with the character data as you need it.</p>
 | 
			
		||||
@@ -855,15 +829,15 @@ Deeply nested</h2>
 | 
			
		||||
  unnest_wider(results)
 | 
			
		||||
locations
 | 
			
		||||
#> # A tibble: 7 × 6
 | 
			
		||||
#>   city       address_components formatted_address   geometry     place_id types 
 | 
			
		||||
#>   <chr>      <list>             <chr>               <list>       <chr>    <list>
 | 
			
		||||
#> 1 Houston    <list [4]>         Houston, TX, USA    <named list> ChIJAYW… <list>
 | 
			
		||||
#> 2 Washington <list [2]>         Washington, USA     <named list> ChIJ-bD… <list>
 | 
			
		||||
#> 3 Washington <list [4]>         Washington, DC, USA <named list> ChIJW-T… <list>
 | 
			
		||||
#> 4 New York   <list [3]>         New York, NY, USA   <named list> ChIJOwg… <list>
 | 
			
		||||
#> 5 Chicago    <list [4]>         Chicago, IL, USA    <named list> ChIJ7cv… <list>
 | 
			
		||||
#> 6 Arlington  <list [4]>         Arlington, TX, USA  <named list> ChIJ05g… <list>
 | 
			
		||||
#> # … with 1 more row</pre>
 | 
			
		||||
#>   city       address_components formatted_address geometry     place…¹ types 
 | 
			
		||||
#>   <chr>      <list>             <chr>             <list>       <chr>   <list>
 | 
			
		||||
#> 1 Houston    <list [4]>         Houston, TX, USA  <named list> ChIJAY… <list>
 | 
			
		||||
#> 2 Washington <list [2]>         Washington, USA   <named list> ChIJ-b… <list>
 | 
			
		||||
#> 3 Washington <list [4]>         Washington, DC, … <named list> ChIJW-… <list>
 | 
			
		||||
#> 4 New York   <list [3]>         New York, NY, USA <named list> ChIJOw… <list>
 | 
			
		||||
#> 5 Chicago    <list [4]>         Chicago, IL, USA  <named list> ChIJ7c… <list>
 | 
			
		||||
#> 6 Arlington  <list [4]>         Arlington, TX, U… <named list> ChIJ05… <list>
 | 
			
		||||
#> # … with 1 more row, and abbreviated variable name ¹place_id</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Now we can see why two cities got two results: Washington matched both Washington state and Washington, DC, and Arlington matched Arlington, Virginia and Arlington, Texas.</p>
 | 
			
		||||
<p>There are few different places we could go from here. We might want to determine the exact location of the match, which is stored in the <code>geometry</code> list-column:</p>
 | 
			
		||||
@@ -872,14 +846,14 @@ locations
 | 
			
		||||
  select(city, formatted_address, geometry) |> 
 | 
			
		||||
  unnest_wider(geometry)
 | 
			
		||||
#> # A tibble: 7 × 6
 | 
			
		||||
#>   city       formatted_address   bounds       location     locati…¹ viewport    
 | 
			
		||||
#>   <chr>      <chr>               <list>       <list>       <chr>    <list>      
 | 
			
		||||
#> 1 Houston    Houston, TX, USA    <named list> <named list> APPROXI… <named list>
 | 
			
		||||
#> 2 Washington Washington, USA     <named list> <named list> APPROXI… <named list>
 | 
			
		||||
#> 3 Washington Washington, DC, USA <named list> <named list> APPROXI… <named list>
 | 
			
		||||
#> 4 New York   New York, NY, USA   <named list> <named list> APPROXI… <named list>
 | 
			
		||||
#> 5 Chicago    Chicago, IL, USA    <named list> <named list> APPROXI… <named list>
 | 
			
		||||
#> 6 Arlington  Arlington, TX, USA  <named list> <named list> APPROXI… <named list>
 | 
			
		||||
#>   city       formatted_address bounds       location     locat…¹ viewport    
 | 
			
		||||
#>   <chr>      <chr>             <list>       <list>       <chr>   <list>      
 | 
			
		||||
#> 1 Houston    Houston, TX, USA  <named list> <named list> APPROX… <named list>
 | 
			
		||||
#> 2 Washington Washington, USA   <named list> <named list> APPROX… <named list>
 | 
			
		||||
#> 3 Washington Washington, DC, … <named list> <named list> APPROX… <named list>
 | 
			
		||||
#> 4 New York   New York, NY, USA <named list> <named list> APPROX… <named list>
 | 
			
		||||
#> 5 Chicago    Chicago, IL, USA  <named list> <named list> APPROX… <named list>
 | 
			
		||||
#> 6 Arlington  Arlington, TX, U… <named list> <named list> APPROX… <named list>
 | 
			
		||||
#> # … with 1 more row, and abbreviated variable name ¹location_type</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>That gives us new <code>bounds</code> (a rectangular region) and <code>location</code> (a point). We can unnest <code>location</code> to see the latitude (<code>lat</code>) and longitude (<code>lng</code>):</p>
 | 
			
		||||
@@ -889,14 +863,14 @@ locations
 | 
			
		||||
  unnest_wider(geometry) |> 
 | 
			
		||||
  unnest_wider(location)
 | 
			
		||||
#> # A tibble: 7 × 7
 | 
			
		||||
#>   city       formatted_address   bounds         lat    lng locati…¹ viewport    
 | 
			
		||||
#>   <chr>      <chr>               <list>       <dbl>  <dbl> <chr>    <list>      
 | 
			
		||||
#> 1 Houston    Houston, TX, USA    <named list>  29.8  -95.4 APPROXI… <named list>
 | 
			
		||||
#> 2 Washington Washington, USA     <named list>  47.8 -121.  APPROXI… <named list>
 | 
			
		||||
#> 3 Washington Washington, DC, USA <named list>  38.9  -77.0 APPROXI… <named list>
 | 
			
		||||
#> 4 New York   New York, NY, USA   <named list>  40.7  -74.0 APPROXI… <named list>
 | 
			
		||||
#> 5 Chicago    Chicago, IL, USA    <named list>  41.9  -87.6 APPROXI… <named list>
 | 
			
		||||
#> 6 Arlington  Arlington, TX, USA  <named list>  32.7  -97.1 APPROXI… <named list>
 | 
			
		||||
#>   city       formatted_address bounds         lat    lng locat…¹ viewport    
 | 
			
		||||
#>   <chr>      <chr>             <list>       <dbl>  <dbl> <chr>   <list>      
 | 
			
		||||
#> 1 Houston    Houston, TX, USA  <named list>  29.8  -95.4 APPROX… <named list>
 | 
			
		||||
#> 2 Washington Washington, USA   <named list>  47.8 -121.  APPROX… <named list>
 | 
			
		||||
#> 3 Washington Washington, DC, … <named list>  38.9  -77.0 APPROX… <named list>
 | 
			
		||||
#> 4 New York   New York, NY, USA <named list>  40.7  -74.0 APPROX… <named list>
 | 
			
		||||
#> 5 Chicago    Chicago, IL, USA  <named list>  41.9  -87.6 APPROX… <named list>
 | 
			
		||||
#> 6 Arlington  Arlington, TX, U… <named list>  32.7  -97.1 APPROX… <named list>
 | 
			
		||||
#> # … with 1 more row, and abbreviated variable name ¹location_type</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Extracting the bounds requires a few more steps:</p>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user