# Collect values from the 2 closest layers of children in a dfs search
create_tree(40, children = 3, directed = TRUE) %>%
mutate(value = round(runif(40)*100)) %>%
mutate(child_acc = map_dfs_back(node_is_root(), .f = function(node, path, dist, ...) {
if (nrow(path) == 0) .N()$value[node]
else {
unlist(path$result[path$dist - dist <= 2])
}
}))
Run the code above in your browser using DataLab