powered by
Convert a data frame to a tree
nest_tree(data, id_col, parent_col, children_to)
A tree like data frame.
A data frame.
Id column. The values must be unique and non-missing.
Parent column. Each value must either be missing (for the root elements) or appear in the id_col column.
id_col
Name of the column the children should be put.
df <- tibble::tibble( id = 1:5, x = letters[1:5], parent = c(NA, NA, 1L, 2L, 4L) ) out <- nest_tree(df, id, parent, "children") out out$children out$children[[2]]$children
Run the code above in your browser using DataLab