Skip to content

Instantly share code, notes, and snippets.

@batpigandme
Last active March 5, 2023 17:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save batpigandme/048c6eecd04d4d1f32d7b97e5a8b437b to your computer and use it in GitHub Desktop.
Save batpigandme/048c6eecd04d4d1f32d7b97e5a8b437b to your computer and use it in GitHub Desktop.
Example of using `tidyr::hoist()`.
library(tidyverse)

family <- list(
  list(
    "name" = "Bob",
    "age" = 46,
    "mother" = NA,
    "father" = "Big Bob",
    "siblings" = list(NA),
    "children" = list("Tina", "Gene", "Louise"),
    "glasses" = FALSE,
    "top_color" = "#e6e6e6",
    "bottom_color" = "#797979"
  ),
  list(
    "name" = "Linda",
    "age" = 45,
    "mother" = "Gloria",
    "father" = "Al",
    "siblings" = list("Gayle"),
    "children" = list("Tina", "Gene", "Louise"),
    "glasses" = TRUE,
    "top_color" = "#e05448",
    "bottom_color" = "#3365ab"
  ),
  list(
    "name" = "Tina",
    "age" = 13,
    "mother" = "Linda",
    "father" = "Bob",
    "siblings" = list("Gene", "Louise"),
    "children" = list(NA),
    "glasses" = TRUE,
    "top_color" = "#98baee",
    "bottom_color" = "#1c4871"
  ),
  list(
    "name" = "Gene",
    "age" = 11,
    "mother" = "Linda",
    "father" = "Bob",
    "siblings" = list("Tina", "Louise"),
    "children" = list(NA),
    "glasses" = FALSE,
    "top_color" = "#fcde60",
    "bottom_color" = "#828fba"
  ),
  list(
    "name" = "Louise",
    "age" = 9,
    "mother" = "Linda",
    "father" = "Bob",
    "siblings" = list("Tina", "Gene"),
    "children" = list(NA),
    "glasses" = FALSE,
    "top_color" = "#f874ac",
    "bottom_color" = "#98a447"
  )
)

belchers <- tibble(info = family)

belchers
#> # A tibble: 5 x 1
#>   info            
#>   <list>          
#> 1 <named list [9]>
#> 2 <named list [9]>
#> 3 <named list [9]>
#> 4 <named list [9]>
#> 5 <named list [9]>

names(belchers$info[[1]])
#> [1] "name"         "age"          "mother"       "father"      
#> [5] "siblings"     "children"     "glasses"      "top_color"   
#> [9] "bottom_color"

belchers %>% unnest_wider(info)
#> # A tibble: 5 x 9
#>   name    age mother father siblings children glasses top_color
#>   <chr> <dbl> <chr>  <chr>  <list>   <list>   <lgl>   <chr>    
#> 1 Bob      46 <NA>   Big B… <list [… <list [… FALSE   #e6e6e6  
#> 2 Linda    45 Gloria Al     <list [… <list [… TRUE    #e05448  
#> 3 Tina     13 Linda  Bob    <list [… <list [… TRUE    #98baee  
#> 4 Gene     11 Linda  Bob    <list [… <list [… FALSE   #fcde60  
#> 5 Loui…     9 Linda  Bob    <list [… <list [… FALSE   #f874ac  
#> # … with 1 more variable: bottom_color <chr>

# From the belchers data frame in the listcol info,
# I want the element “name” in a column I’ll call name,
# the element “age” in a column I’ll call age,
# and the element “father” in a column I’ll call dad.
# I'll also get the first element of the nested list
# "children" and call it firstborn.
belchers %>% hoist(info,
  name = "name",
  age = "age",
  dad = "father",
  firstborn = list("children", 1L)
)
#> # A tibble: 5 x 5
#>   name     age dad     firstborn info            
#>   <chr>  <dbl> <chr>   <chr>     <list>          
#> 1 Bob       46 Big Bob Tina      <named list [6]>
#> 2 Linda     45 Al      Tina      <named list [6]>
#> 3 Tina      13 Bob     <NA>      <named list [6]>
#> 4 Gene      11 Bob     <NA>      <named list [6]>
#> 5 Louise     9 Bob     <NA>      <named list [6]>

Created on 2019-09-30 by the reprex package (v0.3.0)

library(tidyverse)
family <- list(
list(
"name" = "Bob",
"age" = 46,
"mother" = NA,
"father" = "Big Bob",
"siblings" = list(NA),
"children" = list("Tina", "Gene", "Louise"),
"glasses" = FALSE,
"top_color" = "#e6e6e6",
"bottom_color" = "#797979"
),
list(
"name" = "Linda",
"age" = 45,
"mother" = "Gloria",
"father" = "Al",
"siblings" = list("Gayle"),
"children" = list("Tina", "Gene", "Louise"),
"glasses" = TRUE,
"top_color" = "#e05448",
"bottom_color" = "#3365ab"
),
list(
"name" = "Tina",
"age" = 13,
"mother" = "Linda",
"father" = "Bob",
"siblings" = list("Gene", "Louise"),
"children" = list(NA),
"glasses" = TRUE,
"top_color" = "#98baee",
"bottom_color" = "#1c4871"
),
list(
"name" = "Gene",
"age" = 11,
"mother" = "Linda",
"father" = "Bob",
"siblings" = list("Tina", "Louise"),
"children" = list(NA),
"glasses" = FALSE,
"top_color" = "#fcde60",
"bottom_color" = "#828fba"
),
list(
"name" = "Louise",
"age" = 9,
"mother" = "Linda",
"father" = "Bob",
"siblings" = list("Tina", "Gene"),
"children" = list(NA),
"glasses" = FALSE,
"top_color" = "#f874ac",
"bottom_color" = "#98a447"
)
)
belchers <- tibble(info = family)
belchers
names(belchers$info[[1]])
belchers %>% unnest_wider(info)
# From the belchers data frame in the listcol info,
# I want the element “name” in a column I’ll call name,
# the element “age” in a column I’ll call age,
# and the element “father” in a column I’ll call dad.
# I'll also get the first element of the nested list
# "children" and call it firstborn.
belchers %>% hoist(info,
name = "name",
age = "age",
dad = "father",
firstborn = list("children", 1L)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment