Search code examples
rigraph

Which point is closest to the mean of all points?


I have this graph in R in which each node has a random weight:

library(igraph)
library(tidyverse)
set.seed(123)

g <- sample_gnm(n = 20, m = 30, directed = FALSE)
V(g)$weight <- runif(20)

I am trying to find out which node has a weight closest to the mean of all nodes.

If this was not a network graph, it would be pretty easy to do. However, I am not sure how to accomplish this using a network graph object.

I tried to do this using the following way:

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())

The result looks like this:

# 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

Is this the correct way to do it?


Solution

  • Your implementation is correct, and it can be simplified to:

    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
    )
    
    > list(
    +   "node id" = closest_node,
    +   "node weight" = V(g)$weight[closest_node],
    +   "mean weight" = mean_weight
    + )
    $`node id`
    [1] 12
    
    $`node weight`
    [1] 0.4137243
    
    $`mean weight`
    [1] 0.3957675