17

Please consider the following

library(igraph)
id <- c("1","2","A","B")
name <- c("02 653245","03 4542342","Peter","Mary")
category <- c("digit","digit","char","char")
from <- c("1","1","2","A","A","B")
to <- c("2","A","A","B","1","2")

nodes <- cbind(id,name,category)
edges <- cbind(from,to)

g <- graph.data.frame(edges, directed=TRUE, vertices=nodes)

Now I want to access a specific vertex of the graph using the ids I used to create the graph from the data frame id <- c("1","2","A","B").

Let's say I want to access the third vertex - originally identified with "A". Is there any way to access the vertex with something like

V(g)$id == "A"

And is there anyway to obtain the id from name? That is, if I run

which(V(g)$name == "Peter")

I get 3. How to get A instead?

Mike Wise
  • 18,767
  • 6
  • 71
  • 95
CptNemo
  • 5,805
  • 14
  • 46
  • 95

2 Answers2

25

First of all, igraph uses the vertex attribute name as symbolic ids of vertices. I suggest you add the ids as name and use another name for the other attributes, e.g. realname.

But often you don't need to know the numeric ids if you are using symbolic names, because all functions accepts (well, they should) symbolic ids as well. E.g. if you want the degree of vertex Peter, you can just say degree(g, "Peter").

If you really want the the numeric id, you can do things like these:

as.numeric(V(g)["Peter"])
# [1] 3
match("Peter", V(g)$name)
# [1] 3

If you want to get to id from name in your example, you can just index that vector with the result:

id[ match("Peter", V(g)$name) ]
Gabor Csardi
  • 10,076
  • 1
  • 31
  • 49
  • Following your example, let's say I know that `V(g)$name` is `Peter` in my graph and I want to find the corresponding symbolic name `(A)`. If I do `which(V(g)$name == "Peter"` I get back the index (3) not the symbolic name (A). (I updated my question) – CptNemo Nov 26 '13 at 21:42
  • (Also in my example `name` and `id` are different vectors in the generating data frame). – CptNemo Nov 26 '13 at 21:50
  • I see. igraph uses the vertex attributes `name` as symbolic id. You can still use `match` and just index `id` with the result: `id[ match("Peter", V(g)$name) ]`. – Gabor Csardi Nov 26 '13 at 21:58
  • I found for some functions, with or without a 'name' attribute, the computational time differs quite a lot. For example the strength function: – yliueagle Jun 24 '16 at 09:59
  • @yliueagle Then just use the fastest way? – Gabor Csardi Jun 25 '16 at 12:26
  • A simpler but less general solution is to use indexing and `names()`. I.e. `names(V(g)["Peter"])`. – Alex Firsov Mar 13 '18 at 15:40
  • I'm not sure if it's OK to be posting on such an old thread, but it is directly relevant so I'll go ahead. I have tried the above in python3 and it doesn't work. I get an 'invalid syntax' 'syntaxerror' pointing to the $ sign, so I suppose this is the fault. Is this different in python3? – Meep Jun 27 '19 at 14:25
  • @21joanna12 This question is in R, not in Python – Gabor Csardi Jul 16 '19 at 21:43
  • Just to point out, the `as.numeric(V(g)["Peter"]` could also be run in a vectorized manner to give you all the ids quickly, i.e. `as.numeric(V(g)[name]` – nate Nov 06 '19 at 19:29
0

The answer might be useful. My recommendation is same as of @Gabor Csardi about id as name, and name as real_name.

library(igraph)
name <- c("1","2","A","B")
real_name <- c("02 653245","03 4542342","Peter","Mary")
category <- c("digit","digit","char","char")
from <- c("1","1","2","A","A","B")
to <- c("2","A","A","B","1","2")

nodes <- cbind(name,real_name,category)
edges <- cbind(from,to)

g <- graph.data.frame(edges, directed=TRUE, vertices=nodes)

list.vertex.attributes(g)
#Output: [1] "name"      "real_name" "category"
which(V(g)$real_name == "Peter")
#Output: [1] 3
V(g)$name[which(V(g)$real_name == "Peter")]
#Output: [1] "A"
rykhan
  • 189
  • 3
  • 15