title: How to split matrices by ids in the first column + creating incidence matrix for each id?
body: <p>I am trying to calculate a Euclidean distance between two law firms in terms of the market shares of their branch offices (For now). </p>
  
  <p>Later I will have to calculate every possible distances between two firms in 400 law firms sample. </p>
  
  <p>Put simply, I have data, such as: </p>
  
  <pre><code>Firm  Year  Location  Market_share
  1     2000     1         0.1
  1     2000     2         0.2
  1     2000     3         0.5
  1     2000     4         0.2
  1     2001     1         0.3
  1     2001     2         0.0
  1     2001     3         0.2 
  1     2001     4         0.5
  2     2000     1         0.0
  2     2000     2         0.4
  2     2000     3         0.2
  2     2000     4         0.4
  2     2001     1         0.1
  2     2001     2         0.5
  2     2001     3         0.3
  1     2001     4         0.1
  </code></pre>
  
  <p>The above data will be "balanced" panel data for every firm, so the size of the matrix will be the same. Each firm will have about 200 branch locations and 35 years of branch history. </p>
  
  <p>I want to transform this data into a Year X Location incidence matrix for every firm id with cell values representing its marketshare.</p>
  
  <p>Such as: </p>
  
  <p>Firm 1</p>
  
  <pre><code>            Location 1 Location 2 Location 3 Location 4 
   2000          0.1        0.2       0.5         0.2 
   2001          0.3        0         0.2         0.5
  </code></pre>
  
  <p>Firm 2</p>
  
  <pre><code>            Location 1 Location 2 Location 3 Location 4 
   2000          0         0.4        0.2        0.4
   2001          0.1       0.5        0.3        0.1
  </code></pre>
  
  <p>and so on... </p>
  
  <p>What I need to do is: </p>
  
  <ol>
  <li>Split the above matrix by unique firm id in the first column </li>
  <li>Remove the first column that contains the id info</li>
  <li>Turn the remaining matrix into Year X Location incidence matrix by using (sparse) command.</li>
  <li>Carry out the euclidean distance calculation for all possible combination of firm i and firm j. </li>
  </ol>
  
  <p>How might I approach this?</p>