title: Creating a directed adjacency matrix from a dataframe with many columns
body: <p>I want to create a directed adjacency matrix from data like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>x1</th>
  <th>x2</th>
  <th>x3</th>
  <th>x4</th>
  <th>x5</th>
  <th>x6</th>
  <th>x7</th>
  <th>x8</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>2</td>
  </tr>
  <tr>
  <td>22</td>
  <td>22</td>
  <td>22</td>
  <td>3</td>
  <td>3</td>
  <td>3</td>
  <td>2</td>
  <td>3</td>
  </tr>
  <tr>
  <td>3</td>
  <td>3</td>
  <td>3</td>
  <td>5</td>
  <td>5</td>
  <td>2</td>
  <td>3</td>
  <td>23</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Where the columns represent states in time.</p>
  <p>The adjacency matrix should reflect the following logic:</p>
  <p>For the column x1:
  <strong>1</strong> should go to the 3 rows in column x2,</p>
  <p><strong>22</strong> should go to the 3 rows in column x2,</p>
  <p><strong>3</strong> should go to the 3 rows in column x2</p>
  <p>For the column x2: The same pattern going to column x3.
  And this for all columns. So it's like linking each element in a given column to all elements of the following column, and so on.</p>
  <p>The output should be a matrix with columns and rows N x N (where N in the number of unique values in the whole matrix) and... well, an adjacency matrix.</p>
  <p>This dataframe is just a sample, the one I have to use has hundreds of columns.</p>
  <p>For these 8 columns, the output should resemble something like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th></th>
  <th>1</th>
  <th>2</th>
  <th>3</th>
  <th>5</th>
  <th>22</th>
  <th>23</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>6</td>
  <td>1</td>
  <td>0</td>
  <td>0</td>
  <td>0</td>
  <td>0</td>
  </tr>
  <tr>
  <td>2</td>
  <td>0</td>
  <td>0</td>
  <td>2</td>
  <td>0</td>
  <td>0</td>
  <td>0</td>
  </tr>
  <tr>
  <td>3</td>
  <td>0</td>
  <td>1</td>
  <td>4</td>
  <td>1</td>
  <td>0</td>
  <td>1</td>
  </tr>
  <tr>
  <td>5</td>
  <td>0</td>
  <td>1</td>
  <td>0</td>
  <td>1</td>
  <td>0</td>
  <td>0</td>
  </tr>
  <tr>
  <td>22</td>
  <td>0</td>
  <td>0</td>
  <td>1</td>
  <td>0</td>
  <td>2</td>
  <td>0</td>
  </tr>
  <tr>
  <td>23</td>
  <td>0</td>
  <td>0</td>
  <td>0</td>
  <td>0</td>
  <td>0</td>
  <td>0</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>This is a representation of how the graph should look like. (edited)</p>
  <p><a href="https://i.stack.imgur.com/2We8a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2We8a.png" alt="enter image description here" /></a></p>
  <p>I've been trying to make it work, but am really lost by now...
  TIA</p>
  <p>P.S. I'm working with R but Python could also work.</p>