Session Ready
Exercise

Transforming the similarity matrix

For programming with similarity matrices—especially to leverage tidyverse packages like dplyr and ggplot2—you can convert them to a data frame with one entry per row.

There are many ways to do this, but the situation is complicated by the fact that for large networks, it is better to store the adjacency matrix as a sparse matrix to save memory, and different tools are needed.

Here we take the approach of converting them to be graphs using graph_from_adjacency_matrix(). Next we convert this to a data.frame using igraph's as_data_frame(), and finally convert that to a tidyverse tibble using as_tibble(). You need to be slightly careful here since dplyr also has a function named as_data_frame(), which is an alias for as_tibble(). This is fairly convoluted code, but it works.

The similarity matrix S is in the workspace.

Instructions 1/3
undefined XP
  • 1

    Convert the similarity matrix S to an "undirected" graph and save it to h.

    • 2
      • Convert the similarity graph h into a similarity data.frame sim_df.
        • Call igraph's as_data_frame(). To make you you have the function from the correct package, qualify the name with pkgname::functionname() syntax.
        • Pass it the graph, and set what to "edges".
    • 3

      Use as_tibble() to convert the (base-R) data frame to a tidyverse data frame.