私はグラフ オブジェクトを持っており、1.5での一連の頂点の近傍のgサブグラフを作成したいと考えています。g_subv_matchesgorder
言い換えれば、どの頂点がv_matches(順序 1) に接続されているか、およびこれらが頂点にも接続されている場合にのみv_matches、どの頂点が order==1 頂点に接続されているかを見つけたいと考えています。
非技術的な用語では、「友人の友人 (順序 2)、これらの友人が私の友人でもある場合 (順序 1.5)」を見つけたいと考えています。でこれを簡単に行う方法が見つかりませんigraph。
私が書いたコードは の次数 2 の近傍をv_matches計算しますが、次数 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)