Skip to contents

Calculate distance between two nodes in a tree.

Usage

distance_to_target(x, ...)

Arguments

x

The tree to calculate distances within.

from

The index to calculate distance "from".

to

The index to calculate distance "to".

Value

Distance between nodes as a numeric scalar. If the nodes are not nested, returns NA. If to is above from in the tree, distance will be negative.

Examples

tree() |>
  add_node() |>
  add_node(parent = 1L) |>
  add_node(parent = 2L) |>
  distance_to_target(1, 3)
#> [1] 2