title: Nesting a SUM and a MEAN in an aggregate to get means of scores per group
body: <p>I can't find a dataset similar to my problem so I changed the dataset Iris (dataset in R) to look similar - it's close enough!</p>
  
  <pre><code>data = iris
  data$type = gl(5,30,150,labels=c("group1","group2","group3","group4","group5"))
  data$ID = gl(30,5,150)
  </code></pre>
  
  <p>Then I used the following code </p>
  
  <pre><code>xtabs(Sepal.Length ~ Species + type, aggregate(Sepal.Length ~ Species + type + ID, data, mean))
  </code></pre>
  
  <p>which results in</p>
  
  <pre><code>type
  Species      group1 group2 group3 group4 group5
    setosa      30.16  19.90   0.00   0.00   0.00
    versicolor   0.00  12.20  35.88  11.28   0.00
    virginica    0.00   0.00   0.00  26.24  39.64
  </code></pre>
  
  <p>My understanding is that what my code is doing is adding together Sepal.Length for each ID then taking the mean of those values by each of Species and type.</p>
  
  <p>Is this correct?</p>
  
  <p>If not, how would I get this?</p>
  
  <p>Additionally, how would I get this if my data is such that each ID has multiple types? (can't figure out how to construct this in R)</p>
  
  <p>Actually, just to be perfectly clear</p>
  
  <p>What I want is a code that sums together the Sepal.Length for each ID AND type then it will take the average of those sums over all IDs and post an average Sepal.Length by type and species/</p>