I have a data frame with more than 20 columns of actor names and I would like to make the adjacency matrix for the relationship among them, for the sake of simplicity let's say my data frame is as follows:
actor 1 | actor 2 | actor 3 | actor 4 |
---|---|---|---|
Mary | Anna | bob | jack |
Lucy | Sara | bob | Alice |
Nicolas | rob | NA | NA |
Angela | Mary | Anna | NA |
I would like an adjacency matrix in which I have an edge between Mary and Anna and bob and jack and also with Angela( basically, who ever is that is some how related to anyone)
I have already tried the following code but I will only get an edge between Mary and Anna(it put 0 for Mary and bob and bob and jack) and it seems like it only make the edge between first two columns.
ig <- graph_from_data_frame(data)
matrix <- get.adjacency (ig)
How can I get my desired adjacency matrix?
CodePudding user response:
I think you were missing an edge list; that graph_from_data_frame would need.
library(tidyverse)
library(igraph)
(start <- tibble::tribble(
~actor.1, ~actor.2, ~actor.3, ~actor.4,
"Mary", "Anna", "bob", "jack",
"Lucy", "Sara", "bob", "Alice",
"Nicolas", "rob", NA, NA,
"Angela", "Mary", "Anna", NA
) |> mutate(rel=row_number()))
(longlist <- pivot_longer(start,
cols=-rel) |> na.omit()|>
mutate(value=factor(value)) |> select(-name))
(edgelist <- left_join(longlist,
longlist,
by=join_by(rel == rel),
multiple="all") |> filter(value.x!=value.y) |>
relocate(rel,.after=everything()))
ig <- graph_from_data_frame(edgelist)
matrix <- get.adjacency (ig)