title: Reshape Data into Time Series using R
body: <p>I have a seemingly simple question that I can't figure out. I would like to take a dataset in which each time period has its own variable (i.e. column) for an observation and reformat it such that each observation has just one variable that ranges over time periods. My current data looks like:</p>
  
  <pre><code>obs &lt;- 1:4
  y1 &lt;- 5:8
  y2 &lt;- 9:12  
  data_matrix &lt;- cbind(obs, y1, y2)   
  </code></pre>
  
  <p>which produces:</p>
  
  <pre><code>     obs y1 y2
  [1,]   1  5  9
  [2,]   2  6 10
  [3,]   3  7 11
  [4,]   4  8 12
  </code></pre>
  
  <p>and I would like it to look like (also creating a time period variable, T):</p>
  
  <pre><code>     obs  T y2
  [1,]   1  1  5
  [2,]   1  2  9
  [3,]   2  1  6
  [4,]   2  2 10
  [5,]   3  1  7
  [6,]   3  2 11
  [7,]   4  1  8
  [8,]   4  2 12
  </code></pre>
  
  <p>Thanks for any advice on how to reshape this. </p>