title: How to aggregate a factor at each level of another factor, grouping by two other factors in disaggregated data
body: <p>Say that there is descriptive data on candidates across election years, districts (or states), and party. The data are currently dis-aggregated at the 'sub-district' level (say, voting precincts).</p>
  
  <p>Currently, when I try to aggregate the data to the district-level the various methods return counts that are inaccurate. In other words, the aggregation is not adequately taking into account that the candidates appear in the data multiple times per year, per district. What I need is an aggregate count of the number of times a particular party appear in a particular district, regardless of the repeated/duplicated information at the precinct level. In other words, I need a result that shows the party count for the district-year dyad for each unique candidate-year dyad. <strong><em>(Note: candidates may be repeated across election-years and/or districts, but may have different parties; Henry Clay in 1836 and 1840).</em></strong></p>
  
  <p>My question is: <strong><em>How do I aggregate data to obtain a count of a factor (party) at each level of another factor (district) by grouping two other factors (year and candidate-name [ID])?</em></strong></p>
  
  <h2>Sample of Data Structure:</h2>
  
  <pre><code>year&lt;-rbind("1836", "1836", "1836", "1836", 
              "1840", "1840", "1840", "1840", 
              "1844", "1844", "1844", "1844", 
              "1848", "1848", "1848", "1848")
  
  candidate&lt;-rbind("Henry Clay", "Henry Clay", 
                   "Daniel Webster", 
                   "Daniel Webster", "Henry Clay", 
                   "Henry Clay", "Daniel Webster", 
                   "Daniel Webster", 
                   "Millard Fillmore", 
                   "Millard Fillmore", 
                   "Martin Van Buren", 
                   "Martin Van Buren", 
                   "Millard Fillmore", 
                   "Millard Fillmore", 
                   "Martin Van Buren", 
                   "Martin Van Buren")
  
  party&lt;-rbind("Democratic-Republican", 
               "Democratic-Republican", "Whig", 
               "Whig", "National Republican", 
               "National Republican", "Whig", 
               "Whig", "Know-Nothing", 
               "Know-Nothing", "Democrat", 
               "Democrat", "Know-Nothing", 
               "Know-Nothing", "Democrat", 
               "Democrat")
  
  district&lt;-rbind("Alaska", "Alaska", "Vermont", 
                  "Vermont", "Alaska", "Alaska", 
                  "Vermont", "Vermont", "Alaska", 
                  "Alaska", "Vermont", "Vermont", 
                  "Alaska", "Alaska", "Vermont", 
                  "Vermont")
  
  precinct&lt;-rbind("Pre1", "Pre2", "Pre1", "Pre2", 
                  "Pre1", "Pre2", "Pre1", "Pre2", 
                  "Pre1", "Pre2", "Pre1", "Pre2", 
                  "Pre1", "Pre2", "Pre1", "Pre2")
  
  sample&lt;-as.data.frame(cbind(year, candidate, party, district, 
                precinct))
  </code></pre>
  
  <p>Examples of Different Methods of Aggregating Data:</p>
  
  <h2>table</h2>
  
  <pre><code>party.counts1&lt;-data.frame(table(sample$V3, sample$V1, sample$V4))
  </code></pre>
  
  <h2>aggregate:</h2>
  
  <p>Attempt 2a is close to final result needed, but returns counts that do not specify factor-level (party) and are still 'over-counting' party-district data based on precinct-level appearance of the party-candidate in a given year.   </p>
  
  <pre><code>party.counts2&lt;-aggregate(sample$V3, by=list(sample$V4, sample$V1), FUN=length)
  
  party.counts2a&lt;-aggregate(sample$V3~sample$V1:sample$V4:sample$V2, data=sample, FUN=length)
  </code></pre>
  
  <h2>reshape</h2>
  
  <p>Reshape example displays similar problem as previous aggregate 2a attempt.</p>
  
  <pre><code>library(reshape2)
  mdata &lt;- melt(sample, id.vars=c("V1", "V2", "V4", "V5"), measure.vars=c("V3"))
  
  party.counts3&lt;-dcast(mdata, value~V1:V2:V4, length)
  </code></pre>
  
  <p>Again, my question is: <strong><em>How do I aggregate data to obtain a count of a factor (party) at each level of another factor (district) by grouping two other factors (year and candidate-name [ID])?</em></strong></p>