statuser
statuser

Reputation: 63

Duplicate Vertex IDs R

I am trying to make a network visualization for calling activity from a manager to store locations. The only problem is I keep getting the error "Duplicate Vertex IDs". I need to have multiple of the same vertex IDs as one manager has called more than one store. How do I get around this?

My edges data is organized as follows:

from    to    weight
12341    1      5
12341    2      4
23435    1      3

My node data includes only the from column:

from   
12341
12341
23435

This was the code I tried to run:

MANAGER_LOC <- graph_from_data_frame(d = edges, vertices = nodes, 
directed = TRUE)

Upvotes: 1

Views: 3412

Answers (1)

Franziska W.
Franziska W.

Reputation: 356

You are getting the duplicate vertex ID error because you need to reference unique node data in vertices = . You could use unique(nodes), but this will give you another error, because nodes 1 and 2 you are referencing in your adjacency list data are not included in your nodes data.

Your node data cannot only include unique values from column edges$from, it must include all unique values from edges$from and edges$to, because you are passing adjacency list data to the graph_from_data_frame() function.

So in edges$to you also need to reference vertices by their names as in edges$from, e.g. 12341 or 23435.

Here is some R-Code, maybe including what you are trying to achieve.

enter image description here

#graph from your data frame
MANAGER_LOC <- graph_from_data_frame(
  d = edges
  ,vertices = unique(c(edges$from, edges$to))
  ,directed = TRUE);

#plot also includes vertices 1 and 2
plot(
  x = MANAGER_LOC
  ,main = "Plot from your edges data");


#plot from your data assuming you are referencing an id in edges$to
MANAGER_LOC <- graph_from_data_frame(
  d = merge(
    x = edges
    ,y = data.frame(
      to_vertice_id = 1:length(unique(edges$from))
      ,to_vertice = unique(edges$from))
    ,by.x = "to"
    ,by.y = "to_vertice_id"
    ,all.x = T)[,c("from","to_vertice","weight")]
  ,vertices = unique(edges$from)
  ,directed = TRUE);

#plot does not include vertices 1 and 2
plot(
  x = MANAGER_LOC
  ,main = "Plot assuming vertice ID
  reference in edges$to");


#plot from your data assuming you are referencing the xth value of edges$from in edges$to
MANAGER_LOC <- graph_from_data_frame(
  d = merge(
    x = edges
    ,y = data.frame(
      to_vertice_ref = 1:nrow(edges)
      ,to_vertice = edges$from)
    ,by.x = "to"
    ,by.y = "to_vertice_ref"
    ,all.x = T)[,c("from","to_vertice","weight")]
  ,vertices = unique(edges$from)
  ,directed = TRUE);

#plot does not include vertices 1 and 2
plot(
  x = MANAGER_LOC
  ,main = "Plot assuming edges$from
  reference in edges$to");

Upvotes: 2

Related Questions