title: How to create frequency table under couple conditions in r?
body: <p>I am a rookie in R. I think my questions are basic ones. I want to know the frequency of a variable under couple conditions. I try to use table() but it does not work. I have searched a lot, I still cannot find the answers. </p>
  
  <p>My data looks like this</p>
  
  <pre><code>ID AGE LEVEL End_month
  1  14  1     201005
  2  25  2     201006
  3  17  2     201006
  4  16  1     201008
  5  19  3     201007
  6  33  2     201008
  7  17  2     201006
  8  15  3     201005
  9  23  1     201004
  10 25  2     201007
  </code></pre>
  
  <p>I want to know two things.</p>
  
  <p>First, I want to know the frequency of age under different level. The age shows in certain range and aggregate the rest as a variable. It looks like this.</p>
  
  <pre><code>          level 
            1 2 3 sum
  age 14    1 0 0 1
      16    1 0 0 1
      15    0 0 1 1
      17    0 2 0 2
      19    0 0 1 1
      20+   1 3 0 4
  sum       3 5 2 10
  </code></pre>
  
  <p>Second, I want to know the frequency of different age in different end_month of level 2&amp;3 customer. I want to get a table like this.</p>
  
  <p>For level 2 customer</p>
  
  <pre><code>         End_month
           201004 201005 201006 201007 201008 sum
  age  15  0      0      0      0      0      0
       19  0      0      0      0      0      0
       17  0      0      2      0      0      2
       19  0      0      0      0      0      0
       25  0      0      0      1      0      1
       33  0      0      0      1      1      2
  sum      0      0      2      2      1      5
  </code></pre>
  
  <p>For level 3 customer</p>
  
  <pre><code>         End_month
           201004 201005 201006 201007 201008 sum
  age  15  0      1      0      0      0      1
       19  0      0      0      1      0      1
       17  0      0      0      0      0      0
       19  0      0      0      0      0      0
       25  0      0      0      0      0      0
       33  0      0      0      0      0      0
  sum      0      1      0      1      0      2
  </code></pre>
  
  <p>Many thanks in advance.</p>