Learn R Programming

bartMan (version 0.1.1)

getChildren: Generate Child and Parent Node Relationships

Description

Populates `childLeft`, `childRight`, and `parent` columns in the dataset to establish parent-child relationships between nodes based on tree structure.

Usage

getChildren(data)

Value

The modified data frame with `childLeft`, `childRight`, and `parent` columns added, detailing the tree's parent-child node relationships.

Arguments

data

A data frame with tree structure, including `iteration`, `treeNum`, `node`, and `depth` columns, along with a `terminal` indicator.

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")
# Add node number sequntially
tree_data_example$node <- with(tree_data_example,
                               ave(seq_along(iteration),
                               list(iteration, treeNum),
                               FUN = seq_along))
# get children
getChildren(data = tree_data_example)

Run the code above in your browser using DataLab