Skip to content

Instantly share code, notes, and snippets.

@thisisnic
Created November 29, 2018 09:01
Show Gist options
  • Save thisisnic/769baad1aee6ef62495531d291ce6320 to your computer and use it in GitHub Desktop.
Save thisisnic/769baad1aee6ef62495531d291ce6320 to your computer and use it in GitHub Desktop.
When working with list-columns you can use the parameters to tidyr::unnest() to specifty whether to keep or drop other list-columns.

Unnest - unpacking list columns

library(dplyr)
library(tidyr)

# Use this to show list columns
glimpse(starwars)

# Unnest 'films' column, drop other list column
by_film <- unnest(starwars, films) 
head(by_film)
glimpse(by_film)

# Unnest 'films' column but keep all other list columns
by_film_keep_others <- unnest(starwars, films, .drop = FALSE) # keeps all other list columns
head(by_film_keep_others)
glimpse(by_film_keep_others)

# Unnest 'films' column but keep specified list columns
keep_vehicles <- unnest(starwars, films, .preserve = "vehicles") # keeps specific list columns
head(keep_vehicles)
glimpse(keep_vehicles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment