Learn R Programming

bartMan (version 0.1.1)

node_depth: Calculate Node Depths in a Tree Data Frame

Description

Computes the depth of each node in a given tree data frame, assuming a binary tree structure. Requires the tree data frame to contain a logical column `terminal` indicating terminal nodes.

Usage

node_depth(tree)

Value

A vector of depths corresponding to each node in the tree.

Arguments

tree

A data frame representing a tree, must contain a `terminal` column.

Examples

Run this code
data("tree_data_example")
# Create Terminal Column
tree_data_example <- transform(tree_data_example, terminal = ifelse(is.na(var), TRUE, FALSE))
# Get depths
depthList <- lapply(split(tree_data_example, ~treeNum + iteration),
                    function(x) cbind(x, depth = node_depth(x)-1))
# Turn into data frame
tree_data_example <- dplyr::bind_rows(depthList, .id = "list_id")

Run the code above in your browser using DataLab