Calculate top & lowest ten percent values in multiple columns in R
<p>Load library and sample data:</p>

<pre><code>library(MASS)
View(Cars93)
Cars93$ID=1:93
</code></pre>

<p>Now I want to subset <code>Cars93</code> so that new df (<code>sub0l</code> and <code>sub0h</code>) have all IDs with all columns but with only top (for df <code>sub0h</code>) and lowest 10% values (for df <code>sub0l</code>) in column 17:25, and rest values (11-100 quartile for df <code>sub0l</code> and 0-90 quartile for df <code>sub0h</code>) could be changed to NA. </p>

<p>Here is my attempt to create two dfs with top ten% or lowest ten% values from columns 17:25:</p>

<pre><code>sub0l &lt;- do.call(rbind,by (Cars93,Cars93$ID,FUN= function(x) 
  subset(Cars93, (Cars93[,17:25] &lt;= quantile(Cars93[,17:25], probs=  .10)))))

sub0h &lt;- do.call(rbind,by (Cars93,Cars93$ID,FUN= function(x) 
  subset(Cars93, (Cars93[,17:25] &gt;= quantile(Cars93[,17:25], probs=  .91)))))
</code></pre>

<p>I get an error while subseting top and lowest ten quartiles of columns: </p>

<pre><code>Error in `[.data.frame`(Cars93, ,17:25) : undefined columns selected
Called from: `[.data.frame`(Cars93, ,17:25)
</code></pre>

<p>Any better alternative? </p>