R percentage of counts from a data.frame
<p>I need to calculate the percentage of counts of variables and put it in a vector</p>

<p>I have a frame as follows:</p>

<pre><code>group &lt;- c('A','A','A','B','B','B')
hight &lt;- c('tall','tall','short','tall','short','short')

group    hight
A        tall
A        tall
A        short
B        tall
B        short
B        short
</code></pre>

<p>If a run table(df) I get:</p>

<pre><code>     hight
group short tall
A     1    2
B     2    1
</code></pre>

<p>To calculate the percanteges</p>

<pre><code>t=table(df)
percentages &lt;- data.frame(group=c('A','B'), percent = c(t[1]/(t[1]+t[2]),t[3]/(t[3]+t[4])))
percentages
percent.vector &lt;- c(t[1]/(t[1]+t[2]),t[3]/(t[3]+t[4]))
percent.vector
</code></pre>

<p>I get what I want:</p>

<pre><code>    group   percent
1     A 0.3333333
2     B 0.6666667

[1] 0.3333333 0.6666667
</code></pre>

<p>... but I guess there is a better way to do it. I couldn't do this calculation for a higher range of groups. </p>

<p>How can I simplify the calculation of the percentages?</p>

<p>Thanks</p>