0

I have an edge list:

 Source  Target 
 a       e      
 d       f      
 k       v      
 y       w      
 f       u      
 e       q      
 c       b      
 a       y      
 w       x      
 a       z      
 x       r      
 u       m      
 p       k   

I want the following path representation:

Level1  Level2  Level3  Level4  Level5
     a       e       q      NA      NA
     a       y       w       x       r
     a       z      NA      NA      NA
     c       b      NA      NA      NA
     d       f       u       m      NA
     p       k       v      NA      NA

I have many trees like these in my two columns and have the following code which is not computing correctly (see output below):

tbl = tbl[order(tbl$target),]
tbl = tbl[order(tbl$source),]

levels = data.frame(level1=c()
                    ,level2=c(),level3=c(),level4=c(), level5=c())
i = 0
while(nrow(tbl)>0){
  i=i+1
  levels[i,1] = tbl[1,1]
  levels[i,2] = tbl[1,2]
  tbl = tbl[-1,]
  j = 2
  while (TRUE) {
    prev = j
    for(item in tbl$source){
      if(levels[i,j]==item){
        index = which(tbl$source==item)
        j = j+1
        levels[i,j]=tbl$target[index]
        tbl=tbl[-index,]
        break
      }
    }
    if(j==5||prev==j){
      break
    }
  }
}
levels

Which gives the incorrect output (see row 6 & 7 -> Should be one row as per structure above):

  V1 V2   V3   V4   V5
1  a  e    q <NA> <NA>
2  a  y    w    x    r
3  a  z <NA> <NA> <NA>
4  c  b <NA> <NA> <NA>
5  d  f    u    m <NA>
6  k  v <NA> <NA> <NA>
7  p  k <NA> <NA> <NA>

is there a way to do this using networkx or networkit.

0 Answers0