R Percentile
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A percentile is a statistical measure that indicates the value below which a percentage of data falls.
For example, the 70th percentile is the value below which 70% of the observations may be found.
Calculate Percentile in R
In R, we use the quantile()
function to calculate the percentile. For example,
marks <- c(97, 78, 57, 64, 87) # calculate 70th percentile of marks result <- quantile(marks, 0.70) print(result)
Output
70% 85.2
In the above example, we have used the quantile()
function to calculate the 70th percentile of the marks vector. Notice the code,
quantile(marks, 0.70)
Here,
- marks - a vector whose percentile is to be calculated
- 0.70 - a percentile value. For the 70th percentile we use 0.70 argument
Calculate Multiple Percentile a Vector in R
We use the c()
function to pass multiple percentiles to quantile()
at once in R. For example,
marks <- c(97, 78, 57, 64, 87) # calculate 70th, 50th, 80th percentile of marks result <- quantile(marks, c(0.7, 0.5, 0.8)) print(result)
Output
70% 50% 80% 85.2 78.0 89.0
Here, we have used the c()
function to pass multiple percentiles: 0.7, 0.5, 0.8 to quantile()
all at once.
Hence, quantile()
returns 70th, 50th and 80th percentile of marks respectively.
Calculate Percentile in R Data Frame
R allows us to calculate the percentile of specific data frame columns. For example,
# Create a data frame dataframe1 <- data.frame ( Name = c("Juan", "Kay", "Jay", "Ray", "Aley"), Age = c(22, 15, 19, 30, 23), ID = c(101, 102, 103, 104, 105) ) # calculate 55th and 27th percentile of the Age column result <- quantile(dataframe1$Age, c(0.55, 0.27)) print(result)
Output
55% 27% 22.20 19.24
Here, we have calculated the 55th and 27th percentile of the Age column of the dataframe1 data frame.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.