unnest_tree: Unnest a recursive data frame

View source: R/unnest_tree.R

unnest_treeR Documentation

Unnest a recursive data frame

Description

Unnest a recursive data frame

Usage

unnest_tree(
  x,
  id_col,
  child_col,
  level_to = "level",
  parent_to = "parent",
  ancestors_to = NULL
)

Arguments

x

(data.frame) The data frame to unnest.

id_col

(character(1), integer(1), or symbol) The column that uniquely identifies each observation.

child_col

(character(1), integer(1), or symbol) The column that contains the children of an observation. This column must be a list where each element is either NULL or a data frame with the same columns as x.

level_to

(character(1)) The column name ("level" by default) in which to store the level of an observation. Use NULL if you don't need this information.

parent_to

(character(1)) The column name ("parent" by default) in which to store the parent id of an observation. Use NULL if you don't need this information.

ancestors_to

(character(1)) The column name (NULL by default) in which to store the ids of the ancestors of a deeply nested child. Use NULL if you don't need this information.

Value

A "flat" data frame.

Examples

df <- tibble(
  id = 1L,
  name = "a",
  children = list(
    tibble(
      id = 11:12,
      name = c("b", "c"),
      children = list(
        NULL,
        tibble(
          id = 121:122,
          name = c("d", "e")
        )
      )
    )
  )
)
df

unnest_tree(
  df,
  id_col = "id",
  child_col = "children",
  level_to = "level",
  parent_to = "parent",
  ancestors_to = "ancestors"
)

tibblify documentation built on May 9, 2026, 5:07 p.m.