title: R group by multiple columns and mean value per each group based on different column
body: <p>data set exist data with <code>age, gender, state, income, group</code>. Group represents the group that each user belongs to:</p>
  
  <pre><code>     group      gender state age       income
   1       3      Female  CA     33  $75,000 - $99,999
   2       3        Male  MA     41  $50,000 - $74,999
   3       3        Male  KY     32  $35,000 - $49,999
   4       2      Female  CA     23  $35,000 - $49,999
   5       3        Male  KY     25  $50,000 - $74,999
   6       3        Male  MA     21  $75,000 - $99,999
   7       3      Female  CA     33  $75,000 - $99,999
   8       3        Male  MA     41  $50,000 - $74,999
   9       3        Male  KY     32  $35,000 - $49,999
  10       2      Female  CA     23  $35,000 - $49,999
  11       3        Male  KY     25  $50,000 - $74,999
  12       3      Female  MA     21  $75,000 - $99,999
  </code></pre>
  
  <p>Above is dummy data and goal is to get the concept correct.</p>
  
  <p>The goal is to group by <code>group, gender, income</code> and get the count and for each group get the mean age from the users who belong to that group. Then set the data in following structure: "Expanded Version"</p>
  
  <pre><code>    group  male female CA  MA  KY  $35,000 - $49,999  $50,000 - $74,999 $75,000 - $99,999  mean_age
       2      0     2     2   0   0          2                1              0                   23
  ...
  </code></pre>
  
  <p>Here are the attempts: using <code>dplyr</code></p>
  
  <pre><code>&gt; data %&gt;% group_by(group, 
  + gender, 
  + state, 
  + income) %&gt;% 
  + summarize(n()) %&gt;% 
  + mutate(mean_age = mean(age))
  </code></pre>
  
  <p>I was also exploring <code>spread</code> function. </p>