我有一个图表对象g_sub
,并希望在v_matches
中创建一组顶点g
附近的子图order
,{{1} } 1.5。
换句话说,我想找到哪些顶点连接到v_matches
(顺序1),哪些顶点连接到顺序== 1顶点当且仅当这些顶点是也连接到v_matches
顶点。
在非技术术语中,我想找到朋友的朋友(订单2),前提是这些朋友也是我的朋友(订单1.5)'。我找不到在igraph
中轻松完成此操作的方法。
我编写的代码计算v_matches
的第2阶邻域,但我希望它计算1.5阶。非常感谢任何帮助。
v_matches <- which(V(g)$SomeVertexAttribute==SomeValue)
g_sub <- induced.subgraph(graph=g,vids=unlist(neighborhood(g,order=2,nodes=v_matches)))
更新:这是一个函数和我编写的一些代码,用于将order=2
子图转换为order 1.5
。但是,我不相信它运作正常。
Order2ToOrder1.5 <- function(g_sub,categoryValue) {
# get the new indexes of the 'ego' sites for the subgraph
matches_subgraph_gsub <- which(V(g_sub)$`Categorical 1`==categoryValue)
# get the edge list of the subgraph
edgelistTemp <- get.edgelist(g_sub)
# get unique "from" values
uniqueFrom <- unique(edgelistTemp[,1])
badVerts <- c() # yes, I know it is bad form to append to a vector using a for loop...
for (i in 1:length(uniqueFrom)){
# we find the immediate neighbours of each vertice
tempGraph <- as.vector(neighborhood(graph=g_sub,order=1,nodes=uniqueFrom[i])[[1]])
# if the neighbourhood does not contain one of the 'ego' sites (i.e. in matches_subgraph_gsub), then we flag this vertex for deletion
matchTemp <- match(tempGraph,matches_subgraph_gsub)
if (length(which(is.na(matchTemp)))==length(matchTemp)) {badVerts <- append(badVerts,uniqueFrom[i])}
}
# now we can delete these from the subgraph:
g_return <- delete.vertices(g_sub,badVerts)
}
categoryValue <- 21
matches <- which(V(g)$`Categorical 1`==categoryValue)
g_sub <- induced.subgraph(graph=g,vids=unlist(neighborhood(graph=g,order=2,nodes=matches)))
# We can use the new function to convert from order2 to order1.5:
g_sub_1.5 <- Order2ToOrder1.5(g_sub,categoryValue)