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(
  data,
  id_col,
  child_col,
  level_to = "level",
  parent_to = "parent",
  ancestors_to = NULL
)

Arguments

data

A data frame.

id_col

A column that uniquely identifies each observation.

child_col

Column containing the children of an observation. This must be a list where each element is either NULL or a data frame with the same columns as data.

level_to

A string ("level" by default) specifying the new column to store the level of an observation. Use NULL if you don't need this information.

parent_to

A string ("parent" by default) specifying the new column storing the parent id of an observation. Use NULL if you don't need this information.

ancestors_to

A string (NULL by default) specifying the new column storing the ids of its ancestors. Use NULL if you don't need this information.

Value

A 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")
        )
      )
    )
  )
)

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

tibblify documentation built on Nov. 16, 2022, 5:07 p.m.