spread_list: Spread list column into new columns

View source: R/other_functions.R

spread_listR Documentation

Spread list column into new columns

Description

Spread an existing list column into new columns on a data.frame. Note that every element on every observation must have a name for the function to do its work. Original column will be automatically suppressed but you can set the replace argument to avoid it.

Usage

spread_list(df, col, str = NULL, replace = TRUE)

Arguments

df

Dataframe

col

Variable name.

str

Character. Start column names with. If set to NULL, original name of column will be used.

replace

Boolean. Replace original values (delete column)

Value

data.frame. Result of un-nesting named or un-named list columns.

Examples

df <- dplyr::starwars
# Un-named list columns
spread_list(df, films, replace = FALSE) %>%
  dplyr::select(name, dplyr::starts_with("films")) %>%
  head(8)
# Named (and un-nammed) list columns
df <- dplyr::tibble(id = 1:3, platform = list(
  list("fb" = 1, "ig" = 2),
  list("fb" = 3),
  list()
))
spread_list(df, platform, str = "ptf_")

lares documentation built on Sept. 13, 2024, 1:08 a.m.