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

I have a graph with names from 1 to 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 would like to do this, but vice versa [/ p>

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

the above instructions get

7 4 8 9

how to get listMn [[7]] = 6 3 7 8 from the names 7 4 8 9?

+3
source share
1 answer

Node numbering starts from scratch: listMn[[7]]gives the number of neighbors of the seventh node (number 6, name 7), i.e. 6, 3, 7, 8, corresponding to the names (add 1 to the numbers) 7, 4, 8, 9.

Using strings for names can be less confusing:

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

All Articles