Question

I have a graph stored as a dataframe that each row represents a link. Some nodes in the graph have a group of alias. What I want to do is to merge the alias information into the graph. For example a graph is

  node1 node2
1     A     E
2     B     F
3     C     G

Node A has alias A1, A2, node E has alias E1, E2, so all the connections between {A, A1, A2} and {E, E1, E2} are added to the dataframe. I think I can use the package igraph to achieve that.

Was it helpful?

Solution

Let's store the alias count in a lookup-table with row names:

> aliases=data.frame(count=c(1,2,2,3,2,1))
> row.names(aliases)=c("A","B","C","E","F","G")

This function expands a row of the data frame, and numbers the original thing with a zero:

expand.row=function(r){
  e1=r[1];e2=r[2]
  n1=aliases[e1,1];n2=aliases[e2,1]
  expand.grid(
    paste(e1,seq_len(n1)-1,sep=""),
    paste(e2,seq_len(n2)-1,sep=""))
  }

So that:

> expand.row(c("B","E"))
  Var1 Var2
1   B0   E0
2   B1   E0
3   B0   E1
4   B1   E1
5   B0   E2
6   B1   E2

Then apply over the edge data frame, merge the bits into a single matrix:

> full = do.call(rbind,apply(as.matrix(d),1,expand.row))
> full
  Var1 Var2
1   A0   E0
2   A0   E1
3   A0   E2
4   B0   F0
5   B1   F0
6   B0   F1
7   B1   F1
8   C0   G0
9   C1   G0

and there's your graph:

> g = graph.edgelist(as.matrix(full))
> plot(g)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top