Sam Lipworth
Sam Lipworth

Reputation: 107

Add non connected vertices to igraph

I have an edge list eg: df:

sample1    sample2
sample2    sample3
sample1    sample3

I have an additional 2 samples, sample4 and sample5 which have no connections to any other vertices.

A full list of samples is held in a separate data frame eg df2:

sample1
sample2
sample3
sample4
sample5

How can I add these non-connected edges so that the show in my igraph (and so that the are included in the network analysis?)

Upvotes: 1

Views: 877

Answers (1)

elcortegano
elcortegano

Reputation: 2684

This is actually very easy. You can use the function graph_from_data_frame to build your network, as I assume you know.

As vertices parameter you can include all the relevant nodes you need (eg. sample1 ... to sample5), and as d parameter (referred to de edges) the list of edges where in one column (named from) you set the origin vertice, and in the other (named to) the vertice of destination.

Example of "vertices" (I call it "nodes" below) dataframe

id
sample1
sample2
sample3

Example of "edge" dataframe

from to
sample1 sample2

You can use then:

require("igraph")
net = graph_from_data_frame(d=edge, vertices=nodes, directed=FALSE)
plot ( net )

For instance. As you can see in the example, the vertex 1 and 2 are connected, while the 3 stands alone.

Please let me recommend you this tutorial: http://kateto.net/networks-r-igraph

Upvotes: 2

Related Questions