I understand that one could create an igraph graph directly from dataframe(s). I have tried to do this, but somehow failed so far.
I have a DF "myvertices" with the following schema (id and name are unique):
id, name, feature_a, feature_b, feature_c
And another DF "myedges" with the following schema:
id, from, to, feature_d, feature_e, feature_f
where "from" and "to" contain "id"s from "myvertices".
Based on these, I have tried the following:
g <- graph.data.frame(myedges, directed=TRUE, vertices=myvertices)
but resulted the following:
Error in graph.data.frame(myedges, directed = T, vertices = myvertices) : Duplicate vertex names.
I guess the error message gives a good hint - it seems you got duplicates among vertex ids. For example:
library(igraph)
myvertices <- read.csv(stringsAsFactors=F, text="
id,name,feature_a,feature_b,feature_c
a,foo,1,2,3
b,bar,1,2,3
c,extra,1,2,3")
myedges <- read.csv(stringsAsFactors=F, text="
id,from,to,feature_d,feature_e,feature_f
1,a,b,1,2,3")
graph.data.frame(myedges[, -1], directed=TRUE, vertices=myvertices)
# IGRAPH DN-- 3 1 --
# + attr: name (v/c), feature_a (v/n), feature_b (v/n), feature_c (v/n), feature_d (e/n), feature_e (e/n), feature_f (e/n)
# + edge (vertex names):
# [1] foo->bar
myvertices$id[3] <- "a" # duplicate a
graph.data.frame(myedges[, -1], directed=TRUE, vertices=myvertices)
# Error in graph.data.frame(myedges[, -1], directed = TRUE, vertices = myvertices) :
# Duplicate vertex names
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With