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
13 Answers
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.
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) Vertex names are stored in label attribute.
If g is the graph, vertex names can be accesssed by V(g)$label