重复的顶点ID R

时间:2018-12-18 13:26:31

标签: r dataframe igraph

我正在尝试进行网络可视化,以调用经理的活动来存储位置。唯一的问题是我不断收到错误“重复的顶点ID”。我需要拥有多个相同的顶点ID,因为一位经理叫了多个商店。我该如何解决?

我的边缘数据的组织方式如下:

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

我的节点数据仅包含“发件人”列:

from   
12341
12341
23435

这是我尝试运行的代码:

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

1 个答案:

答案 0 :(得分:1)

您收到重复的顶点ID错误,因为您需要引用vertices =中的唯一节点数据。您可以使用unique(nodes),但这会给您另一个错误,因为您在邻接列表数据中引用的节点12没有包含在nodes数据中。

您的节点数据不能仅包含列edges$from中的唯一值,它还必须包含edges$fromedges$to中的所有唯一值,因为您正在将adjacency list数据传递给graph_from_data_frame()功能。

因此,在edges$to中,您还需要按edges$from中的名称引用顶点,例如1234123435

这里有一些R代码,可能包括您要实现的目标。

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");