title: Sorting Fama French portfolios in R
body: <p>I have a matrix containing monthly stock returns over 16 years for approx 1000 companies, another matrix containing annual size measurements for the companies and a third matrix containing annual book equity to market equity for the same companies. so for every stock I have 192 returns observations, 12 size observations and 12 book to market observations. The matrices are ordered as such:</p>
  
  <pre><code> U:APC(P#T) U:AR(P#T)   U:APA(P#T)  89589Q(P#T)
     0.05         0.03        -0.2         0.11
     0.02         0.0l2       -0.1         0.12
     0.01         0.013        0           0.02
      ...         ...         ...          ....
  </code></pre>
  
  <p>I would like to sort the data into Fama French 2015 portfolios.
  First I would like to sort the stocks on the size variable. I would like to use size at time t to sort all stocks in year t into quintiles. once this is done i would like the take every individual group of stocks and sort by book to market (time t-1) also into quintiles. Once sorted there should be 25 groups of stocks (5 b/m groups for every size group), now I would like to take the average of these groups (once average for every month). I would like to re-sort once every year so that in the end i get a list of 25 portfolios over 192 months that are composed of different stocks every year. How can I do this in R? I am quite new to R.</p>
  
  <p>There is a similar question here: 
  <a href="https://stackoverflow.com/questions/35903657/nested-double-sort-in-matlab">Nested double sort in Matlab</a></p>
  
  <p>however it relates to Matlab.</p>