Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dataframe to igraph error: Duplicate vertex names

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.

like image 429
Fredrik Avatar asked Jan 12 '16 14:01

Fredrik


1 Answers

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
like image 184
lukeA Avatar answered Sep 30 '22 10:09

lukeA