title: How to summarize key statistics by two variables?
body: <p>Here is some sample code:</p>
  
  <pre><code>dat = data.frame(income = c(100,200,300,400,500,600), 
                   sex = c("M","M","M", "F","F","F"), 
                   num.kid = c(1,2,3,1,2,3))
  </code></pre>
  
  <p>I want to produce a 2-dimensional table that summarizes the key statistics (e.g. mean and var) of income distribution by sex and num.kid.</p>
  
  <p>For example, <code>table(dat$sex, dat$num.kid)</code> would give me a 2x3 table with <code>sex</code> as rows and <code>num.kid</code> as columns, but the table would be filled with the count of those combinations. How can I bring a third variable (e.g. income) into the table? How can I fill the table with mean or var of <code>income</code> by <code>sex</code> and <code>num.kid</code>? This is almost like filling out an Excel pivot table using R code.</p>