简体   繁体   English

如何在R和Igraph中从名称顶点获取id顶点?

[英]How to get id vertex from name vertex in R and Igraph?

I have a graph with names from 1 to 10 我有一个名称从1到10的图表

library(igraph)
library(Cairo)

    g<- graph(c(0,1,0,4,0,9,1,7,1,9,2,9,2,3,2,5,3,6,3,9,4,5,4,8,5,8,6,7,6,8,7,8),n=10,dir=FALSE)
    V(g)$name<-c(1:10)
    V(g)$label<-V(g)$name
    coords <- c(0,0,13.0000,0,5.9982,5.9991,7.9973,7.0009,-1.0008,11.9999,0.9993,11.0002,7.9989,13.0009,10.9989,14.0009,5.9989,14.0009,7.0000,4.0000)
    coords <- matrix(coords, 10,2,byrow=T)
    plot(g,layout=coords)

listMn<-neighborhood(g,1,0:9)

I'd like to do this but in opposite way 我想以相反的方式这样做

m1<-V(g)[listMn[[7]]]$name

the above instructions gets, 以上说明得到,

7 4 8 9

how to get listMn[[7]]=6 3 7 8 from names 7 4 8 9? 如何从名称7 4 8 9获取listMn [[7]] = 6 3 7 8?

Node numbering starts at zero: listMn[[7]] gives the numbers of the neighbours of the seventh node (number 6, name 7), ie, 6, 3, 7, 8, corresponding to names (add 1 to the numbers) 7, 4, 8, 9. 节点编号从零开始: listMn[[7]]给出第七个节点(编号6,名称7)的邻居编号,即listMn[[7]] ,对应于名称(对数字加1) 7,4,8,9。

Using strings for the names may be less confusing: 使用字符串作为名称可能不那么令人困惑:

V(g)$name <- as.character( 1:10 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM