【问题标题】:igraph: adding vertices = X creating clusters of size = 1igraph:添加顶点 = X 创建大小 = 1 的集群
【发布时间】:2018-11-21 12:44:16
【问题描述】:

我目前正在解决一些图论问题,并且有一个问题我似乎无法找到答案。创建图表时使用:

x <- graph_from_data_frame(el, directed = F, vertices = x)

vertices = x 的添加创建了 size = 1 的组件。

我想查看集群大小,即提取组件并查看大小表:

comp <- components(x)
table(comp$csize)

鉴于边缘列表的性质,我希望没有集群的大小 没有顶点 = x,我的表将从大小 = 2 的簇开始。

为什么添加 vertices = x 会这样做?

谢谢

编辑:

我的边缘列表有变量:

ID   ID.2  soure 
x1   x2    healthcare
x1   x3    child benefit 

顶点数据框包含节点(ID)的一般信息

 ID   date_of_birth   nationality   

 x1     02/09/1999      French 
 x2     12/12/1997      French 
 x3     22/01/2002      French 

【问题讨论】:

  • vertices 参数用于包含顶点元数据。在不知道x 中的内容的情况下很难说。如果您使用dput() 发布您的一些数据或制作一个可重复的最小示例,则更容易诊断。
  • 您好,感谢您的快速回复。我已经编辑了线程并添加了一个可重现的小示例。

标签: r igraph graph-theory sna


【解决方案1】:

我怀疑正在发生的事情是您的节点元数据x 的 data.frame 中出现了 ID,这些 ID 没有出现在边缘列表中。 Igraph 会将这些节点添加为孤立的顶点。下面的一些示例代码来说明问题:

library(igraph)

# generate some fake data
set.seed(42)
e1 <- data.frame(ID = sample(1:10, 5), ID.2 = sample(1:10, 5))
head(e1)
#>   ID ID.2
#> 1 10    6
#> 2  9    7
#> 3  3    2
#> 4  6    5
#> 5  4    9

# make the desired graph object
x <- graph_from_data_frame(e1, directed = F)

# make some attribute data that only matches the nodes that have edges
v_atts1 <- data.frame(ID = names(V(x)), foo = rnorm(length(names(V(x)))))
v_atts1
#>   ID         foo
#> 1 10 -0.10612452
#> 2  9  1.51152200
#> 3  3 -0.09465904
#> 4  6  2.01842371
#> 5  4 -0.06271410
#> 6  7  1.30486965
#> 7  2  2.28664539
#> 8  5 -1.38886070

g1 <- graph_from_data_frame(e1, directed = FALSE, vertices = v_atts1)

# we can see only groups of size 2 and greater
comp1 <- components(g1)
table(comp1$csize)
#> 
#> 2 3 
#> 1 2

# now make attribute data that includes nodes that dont appear in e1
v_atts2 <- data.frame(ID = 1:10, foo=rnorm(10))
g2 <- graph_from_data_frame(e1, directed = FALSE, vertices = v_atts2) 

# now we see that there are isolated nodes
comp2 <- components(g2)
table(comp2$csize)
#> 
#> 1 2 3 
#> 2 1 2

# and inspecting the number of vertices we see that
# this is because the graph has incorporated vertices
# that appear in the metadata but not the edge list
length(V(g1))
#> [1] 8
length(V(g2))
#> [1] 10

如果您想避免这种情况,您可以尝试graph_from_data_frame(e1, directed=FALSE, vertices=x[x$ID %in% c(e1$ID, e1$ID.2),]),它将您的元数据子集化为仅连接的顶点。请注意,您可能需要检查您的 ID 没有被编码为具有未出现在数据中的级别的因素。

【讨论】:

  • 很抱歉现在才回复您! %in% 过程解决了这个问题。非常感谢
猜你喜欢
  • 2018-07-29
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多