R Frequency table of multiple categorical variable
<p>I've imported interview data from a SPSS .SAV file as a <code>data.frame</code> and now I'm trying to create a frequency table based on the question number and interview location. Here's an example <code>data.frame</code>:</p>

<pre><code>loc&lt;-c("city1","city2","city1","city2","city1","city1","city2","city2","city1","city2")
q1&lt;-c("YES","YES","NO","MAYBE","NO","NO","YES","NO","MAYBE","MAYBE")
q2&lt;-c("YES","NO","MAYBE","YES","NO","MAYBE","MAYBE","YES","YES","NO")
q3&lt;-c("NO","NO","NO","NO","YES","YES","MAYBE","MAYBE","NO","MAYBE")
df&lt;-data.frame(loc,q1,q2,q3)

df
     loc    q1    q2    q3
1  city1   YES   YES    NO
2  city2   YES    NO    NO
3  city1    NO MAYBE    NO
4  city2 MAYBE   YES    NO
5  city1    NO    NO   YES
6  city1    NO MAYBE   YES
7  city2   YES MAYBE MAYBE
8  city2    NO   YES MAYBE
9  city1 MAYBE   YES    NO
10 city2 MAYBE    NO MAYBE
</code></pre>

<p>Now I would like to count the number of occurances for each answer option <code>"YES","NO","MAYBE"</code> according to the question number <code>"q1","q2","q3"</code>and the location <code>"city1","city"</code>. The resulting <code>data.frame</code> should look like this:</p>

<pre><code>   loc quest  answ freq
1  city1    q1   YES    1
2  city1    q1    NO    3
3  city1    q1 MAYBE    1
4  city2    q1   YES    2
5  city2    q1    NO    1
6  city2    q1 MAYBE    2
7  city1    q2   YES    2
8  city1    q2    NO    1
9  city1    q2 MAYBE    2
10 city2    q2   YES    2
11 city2    q2    NO    2
12 city2    q2 MAYBE    1
13 city1    q3   YES    2
14 city1    q3    NO    3
15 city1    q3 MAYBE    0
16 city2    q3   YES    0
17 city2    q3    NO    2
18 city2    q3 MAYBE    3
</code></pre>

<p>So far I've played with <code>count()</code>,<code>ddply()</code> and <code>summarise()</code> from the <code>plyr</code> package with no luck. My current solution is really hacky and involves splitting <code>df</code> by <code>loc</code>, creating a frequency table with <code>as.data.frame(summary(df_city1))</code>, retrieving the frequency from the summary string and merging the summary <code>data.frame</code>s of <code>city1</code> and <code>city2</code> back together. I guess there has to be an easier/more elegant solution.</p>