我在 R 中有这个图,其中每个节点都有一个随机权重:
library(igraph)
library(tidyverse)
set.seed(123)
g <- sample_gnm(n = 20, m = 30, directed = FALSE)
V(g)$weight <- runif(20)
我试图找出哪个节点的权重最接近所有节点的平均值。
如果这不是网络图,那会很容易做到。但是,我不确定如何使用网络图对象来完成此任务。
我尝试使用以下方式执行此操作:
tibble(
node_id = 1:20,
weight = V(g)$weight,
mean_weight_of_all_nodes = mean(V(g)$weight),
abs_distance_from_mean = abs(weight - mean_weight_of_all_nodes)
) %>%
arrange(abs_distance_from_mean) %>%
mutate(rank = row_number())
结果如下所示:
# A tibble: 20 × 5
node_id weight mean_weight_of_all_nodes abs_distance_from_mean rank
<int> <dbl> <dbl> <dbl> <int>
1 12 0.414 0.396 0.0180 1
2 11 0.415 0.396 0.0188 2
3 13 0.369 0.396 0.0269 3
4 17 0.466 0.396 0.0702 4
5 8 0.318 0.396 0.0776 5
6 5 0.478 0.396 0.0820 6
7 18 0.266 0.396 0.130 7
8 16 0.233 0.396 0.163 8
9 9 0.232 0.396 0.164 9
10 7 0.216 0.396 0.179 10
11 14 0.152 0.396 0.243 11
12 10 0.143 0.396 0.253 12
13 15 0.139 0.396 0.257 13
14 2 0.691 0.396 0.295 14
15 20 0.0458 0.396 0.350 15
16 6 0.758 0.396 0.363 16
17 4 0.0246 0.396 0.371 17
18 3 0.795 0.396 0.400 18
19 19 0.858 0.396 0.462 19
20 1 0.902 0.396 0.507 20
这是正确的做法吗?
您的实现是正确的,可以简化为:
library(igraph)
# same graph from the question
set.seed(123)
g <- sample_gnm(n = 20, m = 30, directed = FALSE)
V(g)$weight <- runif(20)
mean_weight <- mean(V(g)$weight)
# find the node with weight closest to the mean
closest_node <- which.min(abs(V(g)$weight - mean_weight))
list(
"node id" = closest_node,
"node weight" = V(g)$weight[closest_node],
"mean weight" = mean_weight
)