title: R saving the output of table() into a data frame
body: <p>I have the following data frame:</p>
  
  <pre><code>id&lt;-c(1,2,3,4,1,1,2,3,4,4,2,2)
  period&lt;-c("first","calib","valid","valid","calib","first","valid","valid","calib","first","calib","valid")
  df&lt;-data.frame(id,period)
  </code></pre>
  
  <p>typing </p>
  
  <pre><code>table(df) 
  </code></pre>
  
  <p>results in </p>
  
  <pre><code>period
  id  calib first valid
  1     1     2     0
  2     2     0     2
  3     0     0     2
  4     1     1     1
  </code></pre>
  
  <p>however if I save it as a data frame 'df' </p>
  
  <pre><code> df&lt;-data.frame(table(df))
  </code></pre>
  
  <p>the format of 'df' would be like</p>
  
  <pre><code>id period Freq
  1   1  calib    2
  2   2  calib    1
  3   3  calib    1
  4   4  calib    0
  5   1  first    1
  6   2  first    2
  7   3  first    0
  8   4  first    0
  9   1  valid    0
  10  2  valid    0
  11  3  valid    2
  12  4  valid    3
  </code></pre>
  
  <p>how can I avoid this and how can I save the first output as it is into a data frame?</p>
  
  <p>more importantly is there any way to get the same result using 'dcast'?</p>