| nest_tree | R Documentation |
Recursively nest data frame rows based on parent-child relationships, defined by an id column and a parent column. Children become sub-tibbles of their parent rows. This structure is intended for representing tree-like data, such as organizational charts, file systems, category trees, or any other hierarchical relationships.
nest_tree(x, id_col, parent_col, children_to)
x |
( |
id_col |
( |
parent_col |
( |
children_to |
( |
A tree-like, recursively nested data frame.
df <- tibble::tibble(
id = 1:5,
x = letters[1:5],
parent = c(NA, NA, 1L, 2L, 4L)
)
df
# Only the root elements are in the top-level tibble.
out <- nest_tree(df, id, parent, "children")
out
# The children of each element are stored in the "children" column.
out$children
# "d" (id 4) is a child of "b" (id 2), and "e" (id 5) is a child of "d"
# (id 4).
out$children[[2]]$children
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.