title: Centrality calculations from multiple TPMs
body: <p>Brain wanted. I don't know if this can be resolved with igraph. Basically:</p>
  <p>a.) from my data I would like to create TPM per id (done see the sample code)</p>
  <p>b.) I would like to create a directed graph for each TPM</p>
  <p>c.) calculate the betweennness of specific nodes (in my example is 1 and 5)</p>
  <p>d.) return in a separate file based on id the betweennness of the desired nodes</p>
  <p>How can I do this for a large dataset with more than 1000 TPMs?</p>
  <p>Some similar <a href="https://stackoverflow.com/questions/24888007/using-igraph-for-multiple-graphs-in-r">topic</a></p>
  <p>Desired output:</p>
  <p><a href="https://i.stack.imgur.com/4xGiB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4xGiB.png" alt="enter image description here" /></a></p>
  <p>Data structure:</p>
  <p><a href="https://i.stack.imgur.com/k704K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k704K.png" alt="enter image description here" /></a></p>
  <p>Sample code:</p>
  <pre><code>Transition matrix creation:
  
  
  lapply(seq_len(nrow(stack)),
         function(i) {
           tmp &lt;- trans.matrix(as.matrix(stack[i, 2:6]))
           write.csv(tmp, file =  paste0(i, &quot;.csv&quot;), quote = FALSE)
         })
  </code></pre>
  <p>Outcome TPMs per id, each df represents TPM per id</p>
  <pre><code>df1&lt;-structure(list(X1 = c(1, 2, 3, 4), `2` = c(1, 0, 0, 0), `3` = c(0, 
  1, 0, 0), `4` = c(0, 0, 1, 0), `5` = c(0, 0, 0, 1)), class = c(&quot;spec_tbl_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -4L), spec = structure(list(
      cols = list(X1 = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `2` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `3` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `4` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `5` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
      &quot;collector&quot;)), skip = 1L), class = &quot;col_spec&quot;))
  
  df2&lt;-structure(list(X1 = c(0, 7, 8, 9), `6` = c(0, 1, 0, 0), `7` = c(0, 
  0, 1, 0), `8` = c(0, 0, 0, 1), `9` = c(1, 0, 0, 0)), class = c(&quot;spec_tbl_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -4L), spec = structure(list(
      cols = list(X1 = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `6` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `7` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `8` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `9` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
      &quot;collector&quot;)), skip = 1L), class = &quot;col_spec&quot;))
  
  df3&lt;-structure(list(X1 = c(10, 14, 22, 23), `14` = c(0, 0, 0, 1), 
      `22` = c(1, 0, 0, 0), `23` = c(0, 0, 1, 0), `25` = c(0, 1, 
      0, 0)), class = c(&quot;spec_tbl_df&quot;, &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
  ), row.names = c(NA, -4L), spec = structure(list(cols = list(
      X1 = structure(list(), class = c(&quot;collector_double&quot;, &quot;collector&quot;
      )), `14` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `22` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `23` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `25` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
  &quot;collector&quot;)), skip = 1L), class = &quot;col_spec&quot;))
  
  df4&lt;-structure(list(X1 = c(1, 2, 13), `1` = c(0, 0.5, 1), `2` = c(1, 
  0, 0), `13` = c(0, 0.5, 0)), class = c(&quot;spec_tbl_df&quot;, &quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -3L), spec = structure(list(
      cols = list(X1 = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `1` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `2` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `13` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
      &quot;collector&quot;)), skip = 1L), class = &quot;col_spec&quot;))
  
  df5&lt;--structure(list(X1 = c(1, 2), `1` = c(0, 0.333333333333333), `2` = c(1, 
  0.333333333333333), `5` = c(0, 0.333333333333333)), class = c(&quot;spec_tbl_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -2L), spec = structure(list(
      cols = list(X1 = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `1` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `2` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), `5` = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
      &quot;collector&quot;)), skip = 1L), class = &quot;col_spec&quot;))
  
  
  
  Sample data:
  
      stack&lt;-structure(list(X1 = c(1, 2, 3, 4, 5), a = c(1, 0, 10, 2, 2), 
          b = c(2, 9, 22, 13, 2), c = c(3, 8, 23, 1, 1), d = c(4, 7, 
          14, 2, 2), e = c(5, 6, 25, 1, 5)), class = c(&quot;spec_tbl_df&quot;, 
      &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -5L), spec = structure(list(
          cols = list(X1 = structure(list(), class = c(&quot;collector_double&quot;, 
          &quot;collector&quot;)), a = structure(list(), class = c(&quot;collector_double&quot;, 
          &quot;collector&quot;)), b = structure(list(), class = c(&quot;collector_double&quot;, 
          &quot;collector&quot;)), c = structure(list(), class = c(&quot;collector_double&quot;, 
          &quot;collector&quot;)), d = structure(list(), class = c(&quot;collector_double&quot;, 
          &quot;collector&quot;)), e = structure(list(), class = c(&quot;collector_double&quot;, 
          &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
          &quot;collector&quot;)), skip = 1L), class = &quot;col_spec&quot;))
  </code></pre>
  <p>Sample data</p>