Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the 10% of highest and lowest values from a vector in R?

Tags:

r

subset

highest

As introduce in the title, I would like to select the 10% highest and the 10% lowest values from a vector. How can I manage to do that?

Anyone can help me ? Thanks a lot

like image 922
Oscar-fr Avatar asked Dec 22 '25 05:12

Oscar-fr


2 Answers

This is an example that takes roughly 10%:

v <- rnorm(100)
sort(v)[1:(length(v)/10)]                  # lowest, in increasing order.
sort(v, decreasing=TRUE)[1:(length(v)/10)] # highest, in decreasing order.
like image 156
PascalVKooten Avatar answered Dec 23 '25 23:12

PascalVKooten


This will return a vector containing the bottom and top 10% of x:

> set.seed(123)
> x<-rnorm(100)
> x[{q<-rank(x)/length(x);q<0.1 | q>=0.9}]
 [1]  1.558708  1.715065 -1.265061  1.786913 -1.966617 -1.686693 -1.138137
 [8]  1.253815 -1.265396  2.168956 -1.123109  1.368602  1.516471 -1.548753
[15]  2.050085 -2.309169 -1.220718  1.360652  2.187333  1.532611
like image 32
mrip Avatar answered Dec 23 '25 22:12

mrip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!