How can I calculate the percentile rank in R?

How can I calculate the percentile rank in R?

Calculating the percentile rank in R is a statistical technique used to determine the position of a particular data point within a given dataset. This method is commonly used to compare the relative standing of a specific value to the rest of the data. To calculate the percentile rank in R, one needs to first arrange the data in ascending order and then assign a percentage to each data point based on its position in the dataset. This percentage represents the percentile rank, with a value of 100 indicating that the data point is at the highest position in the dataset. Using this technique, one can accurately interpret and compare the significance of a specific data point in relation to the rest of the data.

Calculate Percentile Rank in R (2 Examples)


The percentile rank of a value tells us the percentage of values in a dataset that rank equal to or below a given value.

You can use the following methods to calculate percentile rank in R:

Method 1: Calculate Percentile Rank for Entire Dataset

library(dplyr)

df %>%
  mutate(percent_rank = rank(x)/length(x))

Method 2: Calculate Percentile Rank by Group

library(dplyr)

df %>%
  group_by(group_var) %>%
  mutate(percent_rank = rank(x)/length(x))

The following examples show how to use each method in practice with the following data frame:

#create data frame
df <- data.frame(team=rep(c('A', 'B'), each=7),
                 points=c(2, 5, 5, 7, 9, 13, 15, 17, 22, 24, 30, 31, 38, 39))

#view data frame
df

   team points
1     A      2
2     A      5
3     A      5
4     A      7
5     A      9
6     A     13
7     A     15
8     B     17
9     B     22
10    B     24
11    B     30
12    B     31
13    B     38
14    B     39

Example 1: Calculate Percentile Rank for Entire Dataset

The following code shows how to use functions from the package in R to calculate the percentile rank of each value in the points column:

library(dplyr)

#calculate percentile rank of points values
df %>%
  mutate(percent_rank = rank(points)/length(points))

   team points percent_rank
1     A      2   0.07142857
2     A      5   0.17857143
3     A      5   0.17857143
4     A      7   0.28571429
5     A      9   0.35714286
6     A     13   0.42857143
7     A     15   0.50000000
8     B     17   0.57142857
9     B     22   0.64285714
10    B     24   0.71428571
11    B     30   0.78571429
12    B     31   0.85714286
13    B     38   0.92857143
14    B     39   1.00000000

Here’s how to interpret the values in the percent_rank column:

  • 7.14% of the points values are equal to or less than 2.
  • 17.86% of the points values are equal to or less than 5.
  • 28.57% of the points values are equal to or less than 7.

And so on.

Example 2: Calculate Percentile Rank by Group

The following code shows how to use functions from the package in R to calculate the percentile rank of each value in the points column, grouped by team:

library(dplyr)

#calculate percentile rank of points values grouped by team
df %>%
  group_by(team) %>%
  mutate(percent_rank = rank(points)/length(points))

# A tibble: 14 x 3
# Groups:   team [2]
   team  points percent_rank
             
 1 A          2        0.143
 2 A          5        0.357
 3 A          5        0.357
 4 A          7        0.571
 5 A          9        0.714
 6 A         13        0.857
 7 A         15        1    
 8 B         17        0.143
 9 B         22        0.286
10 B         24        0.429
11 B         30        0.571
12 B         31        0.714
13 B         38        0.857
14 B         39        1   
  • 14.3% of the points values for team A are equal to or less than 2.
  • 35.7% of the points values for team A are equal to or less than 5.
  • 57.1% of the points values for team A are equal to or less than 7.

And so on.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

Cite this article

stats writer (2024). How can I calculate the percentile rank in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-the-percentile-rank-in-r/

stats writer. "How can I calculate the percentile rank in R?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-the-percentile-rank-in-r/.

stats writer. "How can I calculate the percentile rank in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-the-percentile-rank-in-r/.

stats writer (2024) 'How can I calculate the percentile rank in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-the-percentile-rank-in-r/.

[1] stats writer, "How can I calculate the percentile rank in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I calculate the percentile rank in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top