使用 R 中的 tidygraph 包,给定一棵树,我想计算树中每个节点的每个直接子节点的值的平均值、总和、方差......。
我的直觉是使用map_bfs_back_dbl
或相关并尝试修改帮助示例,但被卡住了
library(tidygraph)
# Collect values from children
create_tree(40, children = 3, directed = TRUE) %>%
mutate(value = round(runif(40)*100)) %>%
mutate(child_acc = map_bfs_back_dbl(node_is_root(), .f = function(node, path, ...) {
if (nrow(path) == 0) .N()$value[node]
else {
sum(unlist(path$result[path$parent == node]))
}
}))
对于上述内容,我想要value
树中每个父级的所有直接、第一级子级的平均值。
更新:: 我试过这种方法(计算子属性的方差):
library(tidygraph)
create_tree(40, children = 3, directed = TRUE) %>%
mutate(parent = bfs_parent(),
value = round(runif(40)*100)) %>%
group_by(parent) %>%
mutate(var = var(value))
这是该死的:
# Node Data: 40 x 3 (active)
# Groups: parent [14]
parent value var
* <int> <dbl> <dbl>
1 NA 2.00 NA
2 1 13.0 1393
3 1 63.0 1393
4 1 86.0 1393
5 2 27.0 890
6 2 76.0 890
# ... with 34 more rows
我想看到的是这样的:
# Node Data: 40 x 3 (active)
# Groups: parent [14]
parent value var child_var
* <int> <dbl> <dbl> <dbl>
1 NA 2.00 NA 1393
2 1 13.0 1393 890
3 1 63.0 1393 (etc)
4 1 86.0 1393
5 2 27.0 890
6 2 76.0 890
# ... with 34 more rows
它将(第一个)“var”值移动到由“父”值标识的节点。帮助?建议?
编辑:这就是我最终要做的事情:
tree <- create_tree(40, children = 3, directed = TRUE) %>%
mutate(parent = bfs_parent(),
value = round(runif(40) * 100),
name = row_number()) %>%
activate(nodes) %>%
left_join(
tree %>%
group_by(parent) %>%
mutate(var = var(value)) %>% activate(nodes) %>% as_tibble() %>%
group_by(parent) %>% summarize(child_stat = first(var)),
by=c("name" = "parent")
)
感觉不是很整洁,但似乎工作。开放优化。