0

I'm trying to create an igraph network object by using the graph_from_dataframe. My target is to map out interactions in a large group. For this, I have a dataframe of vertices (i.e. list of the actors (approx 200) with their name and different attributes) and a dataframe of interactions (i.e. and edge list with two columns, 'initiator' and 'supporter').

Not all actors of the group are involved in the interaction, so there are isolate nodes. However, when I run the command, I get an igraph error:

"Some vertex names in edge list are not listed in vertex data frame".

I have double checked that my CSV vertices data includes all the names which are included in my CSV edge list's both columns. However, because not all are involved, they are not present in the edge listing. I'm perplexed by the error because shouldn't the message should be vice versa – some names in the vertex list are not listed in the edge list?

library(igraph)

links <- read.csv2("edgelist.csv")
vertices <- read.csv2("vertices.csv")
network <- graph_from_data_frame(d=links,vertices = vertices,directed = TRUE)

Error in graph_from_data_frame(d = links, vertices = vertices, :
Some vertex names in edge list are not listed in vertex data frame

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
voppikode
  • 27
  • 6

1 Answers1

0

You can extend the vertices table with dummies:

library(tidyverse)
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:dplyr':
#> 
#>     as_data_frame, groups, union
#> The following objects are masked from 'package:purrr':
#> 
#>     compose, simplify
#> The following object is masked from 'package:tidyr':
#> 
#>     crossing
#> The following object is masked from 'package:tibble':
#> 
#>     as_data_frame
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

vertices <- tribble(
  ~name, ~ alias,
  1, "A",
  2, "B",
  3, "C"
)
vertices
#> # A tibble: 3 × 2
#>    name alias
#>   <dbl> <chr>
#> 1     1 A    
#> 2     2 B    
#> 3     3 C

links <- tribble(
  ~from,  ~to,
  1, 2,
  1, 3,
  1, 4,
  2, 5
)
links
#> # A tibble: 4 × 2
#>    from    to
#>   <dbl> <dbl>
#> 1     1     2
#> 2     1     3
#> 3     1     4
#> 4     2     5

other_vertices <-
  c(links$from, links$to) %>%
    unique() %>%
    setdiff(vertices$name) %>%
    tibble(name = .)
other_vertices
#> # A tibble: 2 × 1
#>    name
#>   <dbl>
#> 1     4
#> 2     5

vertices  %>%
  full_join(other_vertices) %>%
  graph_from_data_frame(d=links,vertices = ., directed = TRUE)
#> Joining, by = "name"
#> IGRAPH 9e3ec0f DN-- 5 4 -- 
#> + attr: name (v/c), alias (v/c)
#> + edges from 9e3ec0f (vertex names):
#> [1] 1->2 1->3 1->4 2->5

Created on 2021-09-18 by the reprex package (v2.0.1)

danlooo
  • 10,067
  • 2
  • 8
  • 22
  • Thanks danloo, however the issue was solved by including the isolates in the edge list as self-loops and changing all vertex names to numbers. This was somewhat surprising because I didn't find any mention about this (number IDs) in the documentation. Now that I have successfully imported the data, my next task is to somehow create a weighted graph object from it, as I only have two columns, _from_ and _to_ in my edge list. – voppikode Sep 19 '21 at 11:52
  • @voppikode please ask only one question per thread. Fell free to open a new one in case you need to – danlooo Sep 19 '21 at 15:57