Get attribute values from the children of a node.
children( attribute, node = NULL, scale = NULL, symbol = NULL, link = NULL, filter_fun = NULL, continue = TRUE )
attribute | Any node attribute (as a character) |
---|---|
node | The MTG node |
scale | An integer vector for filtering the |
symbol | A character vector for filtering the children by the name of their |
link | A character vector for filtering the |
filter_fun | Any filtering function taking a node as input, e.g. |
continue | If a child is not of the right |
The attribute values from the children of the node
This function is mainly intended to be used with mutate_mtg()
. In this case,
the node
argument can be left empty (or you can put node = node
equivalently).
#> Warning: the condition has length > 1 and only the first element will be used#> Warning: the condition has length > 1 and only the first element will be used#> Warning: the condition has length > 1 and only the first element will be used#> Warning: the condition has length > 1 and only the first element will be used#> node_7 #> 12# Using node 4 as reference now: node_4 = extract_node(MTG, "node_4") # node_4 has two children, returns two values: children("Length", node = node_4)#> node_5 node_6 #> 10 6# To get the names of those children: children("name", node = node_4)#> node_5 node_6 #> "node_5" "node_6"# The width is not available for one child ("node_6"): children("Width", node = node_4)#> node_5 node_6 #> 6 NA# We can filter by scale if we need to return the values for some scales only: children("Width", node = node_4, symbol = "Leaf")#> node_5 node_7 #> 6 7# Here we get the value of node_7 also, because its parent "node_6" is not of scale # "Leaf", so it was filtered out. It you need the values for one scale, but not # making a recursive search from one scale to another until finding the required scale, # you can put the `continue` argument to `FALSE`: children("Width", node = node_4, symbol = "Leaf", continue = FALSE)#> node_5 #> 6# To get the values of the children of each node: mutate_mtg(MTG, children_width = children("Width")) print(MTG, "Width", "children_width")#> levelName Width children_width #> 1 node_1 NA #> 2 °--node_2 NA #> 3 °--node_3 NA 1 #> 4 °--node_4 1 6, NA #> 5 ¦--node_5 6 #> 6 °--node_6 NA 7 #> 7 °--node_7 7# And using only the bodes with symbol "Leaf": mutate_mtg(MTG, children_width2 = children("Width", symbol = "Leaf", continue = FALSE)) print(MTG, "Width", "children_width2")#> levelName Width children_width2 #> 1 node_1 NA NA #> 2 °--node_2 NA NA #> 3 °--node_3 NA NA #> 4 °--node_4 1 6 #> 5 ¦--node_5 6 NA #> 6 °--node_6 NA 7 #> 7 °--node_7 7 NA