3.3 矩形数据

我目前接触这类数据或者说需求比较少,我们仅仅简单介绍。大家感兴趣的话,可通过vignette("rectangle")自行学习。

df <- tibble(
  character = c("Toothless", "Dory"),
  metadata = list(
    list(
      species = "dragon",
      color = "black",
      films = c(
        "How to Train Your Dragon",
        "How to Train Your Dragon 2",
        "How to Train Your Dragon: The Hidden World"
       )
    ),
    list(
      species = "blue tang",
      color = "blue",
      films = c("Finding Nemo", "Finding Dory")
    )
  )
)
df
#> # A tibble: 2 x 2
#>   character metadata        
#>   <chr>     <list>          
#> 1 Toothless <named list [3]>
#> 2 Dory      <named list [3]>

df %>% unnest_wider(metadata)
#> # A tibble: 2 x 4
#>   character species   color films    
#>   <chr>     <chr>     <chr> <list>   
#> 1 Toothless dragon    black <chr [3]>
#> 2 Dory      blue tang blue  <chr [2]>

df %>% hoist(metadata,
  "species",
  first_film = list("films", 1L),
  third_film = list("films", 3L)
)
#> # A tibble: 2 x 5
#>   character species   first_film        third_film                  metadata    
#>   <chr>     <chr>     <chr>             <chr>                       <list>      
#> 1 Toothless dragon    How to Train You~ How to Train Your Dragon: ~ <named list~
#> 2 Dory      blue tang Finding Nemo      <NA>                        <named list~

df %>%
  unnest_wider(metadata) %>%
  unnest_longer(films)
#> # A tibble: 5 x 4
#>   character species   color films                                     
#>   <chr>     <chr>     <chr> <chr>                                     
#> 1 Toothless dragon    black How to Train Your Dragon                  
#> 2 Toothless dragon    black How to Train Your Dragon 2                
#> 3 Toothless dragon    black How to Train Your Dragon: The Hidden World
#> 4 Dory      blue tang blue  Finding Nemo                              
#> 5 Dory      blue tang blue  Finding Dory
library(tidyr)
library(dplyr)
library(repurrrsive)
users <- tibble(user = gh_users)
users
#> # A tibble: 6 x 1
#>   user             
#>   <list>           
#> 1 <named list [30]>
#> 2 <named list [30]>
#> 3 <named list [30]>
#> 4 <named list [30]>
#> 5 <named list [30]>
#> 6 <named list [30]>
users %>% unnest_wider(user)
#> # A tibble: 6 x 30
#>   login     id avatar_url gravatar_id url   html_url followers_url following_url
#>   <chr>  <int> <chr>      <chr>       <chr> <chr>    <chr>         <chr>        
#> 1 gabo~ 6.60e5 https://a~ ""          http~ https:/~ https://api.~ https://api.~
#> 2 jenn~ 5.99e5 https://a~ ""          http~ https:/~ https://api.~ https://api.~
#> 3 jtle~ 1.57e6 https://a~ ""          http~ https:/~ https://api.~ https://api.~
#> 4 juli~ 1.25e7 https://a~ ""          http~ https:/~ https://api.~ https://api.~
#> 5 leep~ 3.51e6 https://a~ ""          http~ https:/~ https://api.~ https://api.~
#> 6 masa~ 8.36e6 https://a~ ""          http~ https:/~ https://api.~ https://api.~
#> # ... with 22 more variables: gists_url <chr>, starred_url <chr>,
#> #   subscriptions_url <chr>, organizations_url <chr>, repos_url <chr>,
#> #   events_url <chr>, received_events_url <chr>, type <chr>, site_admin <lgl>,
#> #   name <chr>, company <chr>, blog <chr>, location <chr>, email <chr>,
#> #   public_repos <int>, public_gists <int>, followers <int>, following <int>,
#> #   created_at <chr>, updated_at <chr>, bio <chr>, hireable <lgl>