Calculating quintile based scores on R
<p>I have a dataframe with year (2006 to 2010), 4 industry sectors, 150 firm names and the net income of these firms. In total I have 750 observations, one for each firm for each year. I want to give scores to firms for their income within each industry year based on the quintiles. So, firms with income in the top 20% within each industry-year get a score of 5, the next 20% get a score of 4 and so on. The bottom 20% get a score of 1. </p>

<p>The sample data base is:</p>

<pre><code>Year Industry Firm Income
2006 Chemicals ABC 334.50
2007 Chemicals ABC 388.98
.
.
2006 Pharma XYZ 91.45
.
.
</code></pre>

<p>How do I do this in R? I have tried <code>aggregate</code> and <code>tapply</code> along with <code>quantile</code> but am not able to arrive at the logic that should be used for this. Please help. </p>

<p>I tried this just to allocate a score of 1 to the lowest 20%, but it returned an error. </p>

<pre><code>db10$score &lt;- ifelse(db10$income &lt; aggregate(income~Year+industry,db10,quantile,c(0.2)),1,0)
</code></pre>