How to Easily Calculate Percentiles in R (With Examples) | Online Tutorials Library List | Tutoraspire.com (2024)

The nth percentile of a dataset is the value that cuts off the first n percent of the data values when all of the values are sorted from least to greatest.

For example, the 90th percentile of a dataset is the value that cuts of the bottom 90% of the data values from the top 10% of data values.

One of the most commonly used percentiles is the 50th percentile, which represents the median value of a dataset: this is the value at which 50% of all data values fall below.

Percentiles can be used to answer questions such as:

  • What score does a student need to earn on a particular test to be in the top 10% of scores? To answer this, we would find the 90th percentile of all scores, which is the value that separates the bottom 90% of values from the top 10%.
  • What heights encompass the middle 50% of heights for students at a particular school? To answer this, we would find the 75th percentile of heights and 25th percentile of heights, which are the two values that determine the upper and lower bounds for the middle 50% of heights.

How to Calculate Percentiles in R

We can easily calculate percentiles in R using the quantile() function, which uses the following syntax:

quantile(x, probs = seq(0, 1, 0.25))

  • x: a numeric vector whose percentiles we wish to find
  • probs:a numeric vector of probabilities in [0,1] that represent the percentiles we wish to find

Finding Percentiles of a Vector

The following code illustrates how to find various percentiles for a given vector in R:

#create vector of 100 random values uniformly distributed between 0 and 500data #Find the quartiles (25th, 50th, and 75th percentiles) of the vectorquantile(data, probs = c(.25, .5, .75))# 25% 50% 75% # 97.78961 225.07593 356.47943 #Find the deciles (10th, 20th, 30th, ..., 90th percentiles) of the vectorquantile(data, probs = seq(.1, .9, by = .1))# 10% 20% 30% 40% 50% 60% 70% 80% # 45.92510 87.16659 129.49574 178.27989 225.07593 300.79690 337.84393 386.36108 # 90% #423.28070#Find the 37th, 53rd, and 87th percentilesquantile(data, probs = c(.37, .53, .87))# 37% 53% 87% #159.9561 239.8420 418.4787 

Finding Percentiles of a Data Frame Column

To illustrate how to find the percentiles of a specific data frame column, we’ll use the built-in dataset iris:

#view first six rows of iris datasethead(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa6 5.4 3.9 1.7 0.4 setosa

The following code shows how to find the 90th percentile value for the column Sepal.Length:

quantile(iris$Sepal.Length, probs = 0.9)#90% #6.9 

Finding Percentiles of Several Data Frame Columns

We can also find percentiles for several columns at once using the apply() function:

#define columns we want to find percentiles forsmall_iris#use apply() function to find 90th percentile for every columnapply(small_iris, 2, function(x) quantile(x, probs = .9))#Sepal.Length Sepal.Width Petal.Length Petal.Width # 6.90 3.61 5.80 2.20 

Finding Percentiles by Group

We can also find percentiles by group in R using the group_by() function from thedplyr library.

The following code illustrates how to find the 90th percentile of Sepal.Length for each of the
three species in the iris dataset:

#load dplyr librarylibrary(dplyr)#find 90th percentile of Sepal.Length for each of the three speciesiris %>% group_by(Species) %>% summarise(percent90 = quantile(Sepal.Length, probs = .9))# A tibble: 3 x 2# Species percent90# #1 setosa 5.41#2 versicolor 6.7 #3 virginica 7.61

The following code illustrates how to find the 90th percentile for all of the variables by Species:

iris %>% group_by(Species) %>% summarise(percent90_SL = quantile(Sepal.Length, probs = .9), percent90_SW = quantile(Sepal.Width, probs = .9), percent90_PL = quantile(Petal.Length, probs = .9), percent90_PW = quantile(Petal.Width, probs = .9))# A tibble: 3 x 5# Species percent90_SL percent90_SW percent90_PL percent90_PW# #1 setosa 5.41 3.9 1.7 0.4 #2 versicolor 6.7 3.11 4.8 1.51#3 virginica 7.61 3.31 6.31 2.4 

Visualizing Percentiles

There is no built-in function to visualize the percentiles of a dataset in R, but we can create a plot to visualize the percentiles relatively easily.

The following code illustrates how to create a plot of the percentiles for the data values ofSepal.Lengthfrom theirisdataset:

n = length(iris$Sepal.Length)plot((1:n - 1)/(n - 1), sort(iris$Sepal.Length), type="l", main = "Visualizing Percentiles", xlab = "Percentile", ylab = "Value")

How to Easily Calculate Percentiles in R (With Examples) | Online Tutorials Library List | Tutoraspire.com (1)

Additional Resources

A Guide to apply(), lapply(), sapply(), and tapply() in R
Create New Variables in R with mutate() and case_when()

How to Easily Calculate Percentiles in R (With Examples) | Online Tutorials Library List | Tutoraspire.com (2024)
Top Articles
Which Of The Following Duties To The Client Under Code And Standards Section A Ensures That Information
spellbound - Chapter 2 - starryJ
Craigslist Warren Michigan Free Stuff
Design215 Word Pattern Finder
Camera instructions (NEW)
Monthly Forecast Accuweather
Ghosted Imdb Parents Guide
Tj Nails Victoria Tx
Otterbrook Goldens
Snarky Tea Net Worth 2022
Bbc 5Live Schedule
Slag bij Plataeae tussen de Grieken en de Perzen
Bc Hyundai Tupelo Ms
Foodland Weekly Ad Waxahachie Tx
Quest Beyondtrustcloud.com
Costco Gas Foster City
Fdny Business
All Obituaries | Buie's Funeral Home | Raeford NC funeral home and cremation
[Cheryll Glotfelty, Harold Fromm] The Ecocriticism(z-lib.org)
MLB power rankings: Red-hot Chicago Cubs power into September, NL wild-card race
Nz Herald Obituary Notices
Two Babies One Fox Full Comic Pdf
Account Now Login In
Black Panther 2 Showtimes Near Epic Theatres Of Palm Coast
Xpanas Indo
130Nm In Ft Lbs
Tom Thumb Direct2Hr
A Man Called Otto Showtimes Near Carolina Mall Cinema
Uno Fall 2023 Calendar
Elanco Rebates.com 2022
Angela Muto Ronnie's Mom
Appleton Post Crescent Today's Obituaries
Greencastle Railcam
Blue Beetle Movie Tickets and Showtimes Near Me | Regal
Leena Snoubar Net Worth
Casamba Mobile Login
The Conners Season 5 Wiki
Arnesons Webcam
Is Ameriprise A Pyramid Scheme
844 386 9815
Sherwin Source Intranet
Ouhsc Qualtrics
Workday Latech Edu
Spn 3464 Engine Throttle Actuator 1 Control Command
Product Test Drive: Garnier BB Cream vs. Garnier BB Cream For Combo/Oily Skin
Is TinyZone TV Safe?
Santa Ana Immigration Court Webex
sin city jili
91 East Freeway Accident Today 2022
The Missile Is Eepy Origin
Competitive Comparison
Invitation Quinceanera Espanol
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6044

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.