19

My general problem is that I loose the vertex names / labels (not sure about the right word here) when generating a graph using iGraph.

I have an edge list IC_edge_sub of a bipartite network, that looks like the following:

  new_individualID new_companyID
1             <NA>     10024354c
3        10069415i      2020225c
4        10069415i     16020347c
5        10069272i      2020225c
6        10069272i     16020347c
7        10069274i      2020225c

I then create a graph element:

IC_projected_graphs <- bipartite.projection(IC_twomode, types = 
                         is.bipartite(IC_twomode)$type)

Then collapse it to identify only connections between companyIDs

IC_projected_graphs <- bipartite.projection(IC_twomode, types =   
                         is.bipartite(IC_twomode)$type)

And then get the adjacency matrix:

CC_matrix_IC_based <- get.adjacency(CC_graph_IC_based); CC_matrix_IC_based

In iGraph node numbering starts at zero and thus also the matrix naming starts at zero. However, I would instead now need the "new_companyID" as specified in the 2nd column of the edgelist in the eventual CC_matrix_IC_based matrix.

Can you help me how to use the information form the original edgelist to put in rownames and colnames in the eventual adjacency matrix?

I googled it and searched stack flow, but could not really find a working answer. Thanks a lot for your help

Andrie
  • 176,377
  • 47
  • 447
  • 496
Henning Piezunka
  • 255
  • 1
  • 3
  • 9

3 Answers3

28

Vertex names are usually stored in a vertex attribute named name in igraph. So, if your graph is stored in the variable g, then you can use V(g)$name to retrieve the names of all the vertices.

Tamás
  • 47,239
  • 12
  • 105
  • 124
  • 4
    unfortunately this does not work in my case. I simply get NULL back. Too bad. Any alternative ideas by chance. Sorry, but I am really tuck on this. – Henning Piezunka Jun 08 '12 at 21:45
  • How to get the relation of the numeric vertex id and name? – pengchy Dec 14 '16 at 04:09
  • @HenningPiezunka This occurs when the vertex attribute list does not contain a `name` entry, but you can still set it using syntax of the form `vertex_attr(g, "name") <- c("vector", "of", "names")` optionally selecting a subset of vertices with the `index` argument, which defaults to `index = V(graph)`. See the documentation under [Query vertex attributes of a graph](https://igraph.org/r/doc/vertex_attr.html) – Anil Feb 07 '22 at 18:41
2

The key issue was that I had not saved the names when generating the graph. Afterwards I needed to ensure not to lose the data. In the following the overall solution:

# Subsetting / triangulating data for selected games
GC_edge_sub <- subset (GC_edge, mb_titleID %in% loggames_yearly_sample$mb_titleID)
GC_edge_sub <- subset(GC_edge_sub, select = c("new_titleID", "new_companyID"))
head(GC_edge_sub)

# Generating the vertex names
vertex_new_companyID <- data.frame(names = unique(GC_edge_sub$new_companyID))
vertex_new_titleID <- data.frame(names = unique(GC_edge_sub$new_titleID))
vertex <- rbind(vertex_new_companyID, vertex_new_titleID)

# Creation of GC_twomode
GC_twomode <- graph.data.frame(GC_edge_sub, vertices = vertex)
GC_projected_graphs <- bipartite.projection(GC_twomode, 
                                            types = is.bipartite(GC_twomode)$type)
GC_matrix_GC_based <- get.adjacency(GC_twomode)
dim(GC_matrix_GC_based)

# Collapsing the matrix
# Be aware that if you use the classical command 
# `CC_graph_GC_based <- GC_projected_graphs$proj2` 
# it collapses, but looses the colnames and rownames
# I thus: a) create a subset of the adjacency matrix; and
#         b) create the lookef for matrix by multiplication
rowtokeep <- match(vertex_new_companyID$names, colnames(GC_matrix_GC_based))
coltokeep <- match(vertex_new_titleID$names, rownames(GC_matrix_GC_based))
GC_matrix_GC_based_redux <- GC_matrix_GC_based[rowtokeep, coltokeep]
# We now have a CG matrix.Let's build from this a GG matrix.
CC <- GC_matrix_GC_based_redux %*% t(GC_matrix_GC_based_redux)
user438383
  • 5,716
  • 8
  • 28
  • 43
Henning Piezunka
  • 255
  • 1
  • 3
  • 9
0

Vertex names are stored in label attribute.

If g is the graph, vertex names can be accesssed by V(g)$label

  • Please check previous answers before posting a solution, your answer only repeats info already posted in previous answers. Thanks – L Tyrone Apr 16 '23 at 00:44