Working with Three-Dimensional Frequency Tables in R
<p>I'm able to generate 3D frequency tables, but I'm having a hard time working with them to get the information I want.  What's the best way to approach this?</p>

<h2>What I Have</h2>

<p>A data frame named ballots with columns <code>member</code>, <code>vote_time</code>, and <code>vote</code>, all strings.  The possible values in the vote column are Yes, No, Abstain, Present, and Absent.  There is exactly one row for every combination of <code>member</code> and <code>vote_time</code> values.  The vote_times are a bit random in that most dates have no votes, some have one and some have several votes.</p>

<h2>What I Want</h2>

<p>A data frame with the columns <code>member</code>, <code>vote_date</code>, and <code>absent_ratio</code>.  The <code>absent_ratio</code> column would show the percentage of ballots entered by a given member on a given date that were <em>Absent</em> instead of Yes or No, etc.</p>

<h2>What I've Tried</h2>

<p>Adding a <code>vote_date</code> column to the original data frame was easy enough.</p>

<pre><code>ballots$vote_date &lt;- as.Date(ballots$vote_time)
</code></pre>

<p>I've tried using both the built-in <code>table</code> function and the <code>count</code> function in the <code>plyr</code> package.  They are able to give me the <em>number</em> of Absent votes for a given member/date combination, but I'm not sure how to turn this into a ratio.</p>

<pre><code>library(plyr)
daily_vote_count &lt;- count(ballots, c('vote_date', 'name_en'))
daily_count_by_vote &lt;- count(ballots, c('vote_date', 'vote', 'name_en'))
daily_absense_rate &lt;- subset(daily_count_by_vote,
  name_en == daily_vote_count$name_en &amp;
  vote_date == daily_vote_count$vote_date &amp;
  vote == "Absent"
)$freq / daily_vote_count # DOESN'T WORK
</code></pre>

<p>The warnings point out that I'm working with objects of different lengths, but I'm unsure of how to change my approach.  Thanks for any tips you can give me.</p>

<h2>An Equivalent Problem</h2>

<p>Since it was pointed out that this is difficult to advise on without any data, here's an equivalent question using <code>mtcars</code>.  <em>For every combination of cylinder &amp; gear count, what percentage of cars have a fuel efficiency greater than 20 miles per gallon?</em></p>

<p>With this command, I can count the number of cars in each group.</p>

<pre><code>table(mtcars$mpg&gt;20, mtcars$cyl, mtcars$gear)
</code></pre>

<p>What I can't figure out is how to calculate the right percentage.  The proportion of <code>TRUE</code> + the proportion of <code>FALSE</code> for each <code>cyl</code>/<code>gear</code> pair should add to 100%.  But prop.table() doesn't seem to return what I want.</p>

<pre><code>prop.table(table(mtcars$mpg&gt;20, mtcars$cyl, mtcars$gear))    # No
prop.table(table(mtcars$mpg&gt;20, mtcars$cyl, mtcars$gear),1)  # Nope
prop.table(table(mtcars$mpg&gt;20, mtcars$cyl, mtcars$gear),2)  # Still wrong
</code></pre>

<p>I've tried rearranging the arguments in the <code>table()</code> funtion as well, but I haven't stumbled upon the right answer yet.</p>