DEV Community

loading...

How to calculate percentile,Decile and Quartile in R-programming

maxwizard01
・5 min read

Percentiles in R

So much time people need to calculate percentiles in statistics. This is a little as R failed to have no inbuilt function for that like quantiles and mean.

So how do we go about this?? Well some people accept the we should use the general formula
P(N+1)/100. Where p is the percentile value. They believe this will actually tell us the position of such percentiles.
However, this isn't always true infact most of the time it's not accurate.

What is the best way to find percentile??

Well, there is inbuilt function in R to do that but only few people aware of that.
What! There's inbuilt function for??
Don't doubt it, it's even what you know.
To calculate your percentiles use quartile() function. inside it includes parameters called 'prob' and equal to your percentiles.
Our formula structure look like this
quantile(data, percentage)
Let's take a look at some

Example1.

Let say I need to find the 20 percentiles of the following data.
1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7.

data=c(1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7)
quantile(data,0.2)
Enter fullscreen mode Exit fullscreen mode

Results

data=c(1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7)
> quantile(data,0.2)
20% 
  3
Enter fullscreen mode Exit fullscreen mode

Note: we are to change the percentage value to decimal, 20% =0.2. that is why we type 0.2.
Now let's try another example

Example 2

find height of the 40 percentiles of the following heights.
11,22,17,13,27,25,17,27,23,17,19,22,17,19,16,23,25,17,36,15,14,25,26,17,18,29,17,15,28,17

Codes>>

allHeight=c(11,22,17,13,27,25,17,27,23,17,19,22,17,19,16,23,25,17,36,15,14,25,26,17,18,29,17,15,28,17)
percent40=quantile(allHeight,0.4)
print(percent40)
Enter fullscreen mode Exit fullscreen mode

Result>>

> percent40=quantile(allHeight,0.4)
> print(percent40)
40% 
 17 
Enter fullscreen mode Exit fullscreen mode

can you see how easy it is to find your percentiles?? if you are given the following percentiles you type their respective value.

percentiles percent/100 value to be typed
3% 3/100 0.03
15% 15/100 0.15
55% 55/100 0.55

So always remember to convert your percentage to decimal.

How to calculate the Quartiles

To calculate quartile is a little thing in R. just used the quantile() function.

Example

calculate the first quantile of the following heights.
11,22,17,13,27,25,17,27,23,17,19,22,17,19,16,23,25,17,36,15,14,25,26,17,18,29,17,15,28,17.

SOLUTION
Codes>>

height=c(11,22,17,13,27,25,17,27,23,17,19,22,17,19,16,23,25,17,36,15,14,25,26,17,18,29,17,15,28,17)
print(quantile(height))
Enter fullscreen mode Exit fullscreen mode

Results>>

> height=c(11,22,17,13,27,25,17,27,23,17,19,22,17,19,16,23,25,17,36,15,14,25,26,17,18,29,17,15,28,17)
> print(quantile(height))
  0%  25%  50%  75% 100% 
11.0 17.0 18.5 25.0 36.0 
Enter fullscreen mode Exit fullscreen mode

Note: the first quantile is below 25%, second quantile below 50%, third quantile is below 75%. etc.

What if you need to find your quantiles without listing unnecessary one??

I still get your back on that too but note the following fact.

Quantile Percentile
first quantile 25%
second quantile 50/100
third quantile 75/100

The table above is show the equality between quantile and percentile. Therefore to find the first quantile in the previous question, we find the 25th percentile. so our codes look like the following
codes

height=c(11,22,17,13,27,25,17,27,23,17,19,22,17,19,16,23,25,17,36,15,14,25,26,17,18,29,17,15,28,17)
firstQuatile=quantile(height,0.25)
print(firstQuantile)
Enter fullscreen mode Exit fullscreen mode

Result>>

> firstQuatile=quantile(height,0.25)
> print(firstQuantile)
Error in print(firstQuantile) : object 'firstQuantile' not found
> print(firstQuatile)
25% 
 17 
Enter fullscreen mode Exit fullscreen mode

Example2

calculate the third quantile from the following data.
1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7.

Solution

Firstly note that third quantile is equivalent to 75%. therefore we use 75% which is going to be change to 0.75.
Codes>>>

mydata=c(1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7)
thirdQuatile=quantile(height,0.25)
print(thirdQuantile)
Enter fullscreen mode Exit fullscreen mode

Result>>

> mydata=c(1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7)
> thirdQuantile=quantile(height,0.25)
> print(thirdQuantile)
25% 
 17 
Enter fullscreen mode Exit fullscreen mode

Can you see how easy it is??.

How to calculate Deciles in R.

well, we don't have any inbuilt function for decile too, However we can also use equaivalent percentile. how do we do that??? keep calm it is as easy as 1+1.
To Calculate Decile use quantile() function also. and input the conversion of your decile(in decimal ) inside the parameters.
For example.
first-Decile == 1/10 =0.1
second-Decile == 2/10 = 0.2
Third-Decile== 3/10 = 0.3.
you can continue the logic like that to finish all.

How to write code to calculate Deciles

without wasting time, let's take a look at some question on this Decile.

Example1.

calculate the third-decile of the following data.
1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7.

Solution
we know that third decile is same as 3/10 i.e 0.3. therefore our codes will be the following

data=c(1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7)
thirdDecile=quantile(data,0.3)
print(thirdDecile)
Enter fullscreen mode Exit fullscreen mode

Result

>data=c(1,2,1,3,7,5,7,7,3,7,9,2,7,9,6,3,5,7,6,5,4,5,6,7,8,9,7,5,8,7)
> thirdDecile=quantile(data,0.3)
> print(thirdDecile)
30% 
  5 
Enter fullscreen mode Exit fullscreen mode

Now try to find the sixth,seventh,eighth and nineth decile.

How hope you find this article interesting?? you can chat me up to on whatsapp (07045225718) or facebook to ask any question concerning or want to do any correction. thank you for reading.

Discussion (0)