Vale Baia Vale Baia - 1 year ago 89
R Question

R/Network Analysis - How to create edges by node's attributes

Dear Stackoverflow community,

I am currently using R to compile an affiliation network where nodes are companies/umbrella organisations and ties are defined as "member of". At the moment, my list is still small and I can create edges as follow, based on the position of the nodes (I use igraph):

g <- igraph::add_edges(g, c(1,51,

However, I am adding new nodes and the final network will include at least 500 organisations. This means that the position of a node can change everytime I add a new one. Since I cannot redo the edges everytime I add a new node, is there a way I can add edges knowing the names of the nodes?

The names of the nodes are treated as an attribute, I tried to use the same command as above including names - as opposed to positions - but it did not work:

g <- igraph::add_edges(g, c(V(g)$name=="Company1", V(g)$name == "Umbrella2"))

Any suggestion on how I could create edges by specifying the names and not the position?

Answer Source

I believe you're looking for as.numeric(V(g)["Company1"]).

I would strongly advice against building your network structure in an R-script, though. Even for a small network, I would have inputed my data in an excel-file, create an R-script that reads the data as an edge-list and creates an igraph from it. That way, you can add your companies and organisations as you go with greater oversight of what data has actually gone in to your network, which I guess is what you're looking for in the first place. Doing that here would be out of bounds for the question though.

As for the adding-nodes-by-name, I wrote this example for you which I hope is pedagogical.


# Make an empty Bipartite graph
g <- make_bipartite_graph(0, NULL, directed=TRUE)
g <- delete_vertices(g, 1)

# Create vertices of two different types: companies and umbrellas
g  <- add_vertices(g, 5, color = "red", type=TRUE, name=paste("Company", 1:5, sep="_"))
g  <- add_vertices(g, 2, color = "blue", type=FALSE, name=paste("Umbrella", 1:2, sep="_"))

# In a bipartate graph edges may only appear BETWEEN verticies of different types. Companies
# can belong to umbrellas, but not to each other.

# Look at the types:
ifelse(V(g)$type, 'Company', 'Umbrella')  # true for companies, false for umbrellas

# Lets add some edges one by one. This is what I believe you're asking for in the question:
g <- add_edges(g, c(as.numeric(V(g)["Company_1"]), as.numeric(V(g)["Umbrella_1"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_1"]), as.numeric(V(g)["Umbrella_2"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_2"]), as.numeric(V(g)["Umbrella_1"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_3"]), as.numeric(V(g)["Umbrella_1"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_4"]), as.numeric(V(g)["Umbrella_2"])))
g <- add_edges(g, c(as.numeric(V(g)["Company_5"]), as.numeric(V(g)["Umbrella_2"])))

# Note that "Company_1" belongs to two umbrella organisations, as I assume your companies can:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download