Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206553

How to calculate the total from each cycles by using igraph in R?

$
0
0

I have a data frame which is as below :

h = data.frame(fr  = c('A','A','X','E','B','W','C','Y'),
               t   = c('B','E','Y','C','A','X','A','W'),
               Amt = c( 40, 30, 55, 10, 33, 78, 21, 90))

I've found all the possible vertex sequence that starts with the smallest vertex number by using r igraph find all cycles as reference. And the result is as below :

[[1]]
A E C A 
1 3 6 1 

[[2]]
A B A 
1 4 1 

[[3]]
X Y W X 
2 7 5 2 

Now I'd like to

  • calculate the sum from each cycles

  • numbers of edges in each cycles

It'd be like this :

A - B - A : 40 + 33 = 73 ; numbers of edges : 2

A - E - C - A : 30 + 10 + 21 = 61 ; numbers of edges : 3

X - Y - W - X : 55 + 90 + 78 = 223 ; numbers of edges : 3

Does anyone have any ideas to use R to calculate? That would be great appreciation !!


FURTHER EDIT PART

Thanks to the reply, I can calculate two items above !! However, I got a tiny problem here !!

I don't know what the problem I got so that I cannot calculate correctly !! Even I modify many times.

It should be like this :

[[1]]        [[2]]     [[3]]
A E C A      A B A     X Y W X

  Path            sumAmt numberOfEdges
  <fct>            <dbl>         <int>
1 "A - B - A"         73             2
2 "A - E - C - A"     61             3
3 "X - Y - W - X"    223             3

But After I put in my code, it cannot show up the first node :

[[1]]        [[2]]     [[3]]
  E C A        B A       Y W X

  Path            sumAmt numberOfEdges
  <fct>            <dbl>         <int>
1 " - B - A"         33             2
2 " - E - C - A"     31             3
3 " - Y - W - X"    168             3

Here's my code on finding cycles. Does anything I miss to put-in ??

h = data.frame(fr  = c('A','A','X','E','B','W','C','Y'),
               t   = c('B','E','Y','C','A','X','A','W'),
               Amt = c( 40, 30, 55, 10, 33, 78, 21, 90))

library(igraph)
g <- graph.data.frame(h, directed = TRUE)

Cycles = NULL
for(fr in V(g)) {
  for(t in neighbors(g, fr, mode = "out")) {
    Cycles = c(Cycles, 
    lapply(all_simple_paths(g, t, fr, mode = "out"), function(p)c(fr,p)))
  }
}

LongCycles = Cycles[which(sapply(Cycles, length) > 1)]
LongCycles[sapply(LongCycles, min) == sapply(LongCycles, `[`, 1)]

Does anyone have ideas? That would be helpful !!


Viewing all articles
Browse latest Browse all 206553

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>