Table of Contents
The Gini coefficient is a measure of income or wealth inequality, with a value ranging from 0 to 1. In R, it can be calculated using the “Gini” function from the “ineq” package. This function takes a vector of income or wealth data as its input and returns the Gini coefficient as its output. An example of calculating the Gini coefficient in R would be:
income_data
Calculate Gini Coefficient in R (With Example)
Named after Italian statistician , the Gini coefficient is a way to measure the income distribution of a population.
The value for the Gini coefficient ranges from 0 to 1 where higher values represent greater income inequality and where:
- 0 represents perfect income equality (everyone has the same income)
- 1 represents perfect income inequality (one individual has all the income)
You can find a list of Gini coefficients by country .
The following examples show two ways to calculate a Gini coefficient in R by using the Gini() function from the DescTools package.
Example 1: Calculate Gini Coefficient Using Individual Incomes
Suppose we have the following list of annual incomes for 10 individuals:
Income: $50k, $50k, $70k, $70k, $70k, $90k, $150k, $150k, $150k, $150k
The following code shows how to use the Gini() function to calculate the Gini coefficient for this population:
library(DescTools) #define vector of incomes x <- c(50, 50, 70, 70, 70, 90, 150, 150, 150, 150) #calculate Gini coefficient Gini(x, unbiased=FALSE) [1] 0.226
The Gini coefficient turns out to be 0.226.
Note: In a real-world scenario there would be hundreds of thousands of different incomes for individuals in a certain country, but in this example we used 10 individuals as a simple illustration.
Example 2: Calculate Gini Coefficient Using Frequencies
Suppose we have the following frequency table that shows the number of individuals in a certain population with specific incomes:

The following code shows how to use the Gini() function to calculate the Gini coefficient for this population:
library(DescTools) #define vector of incomes x <- c(10, 20, 25, 55, 70, 90, 110, 115, 130) #define vector of frequencies n <- c(6, 7, 7, 14, 22, 20, 8, 4, 1) #calculate Gini coefficient Gini(x, n, unbiased=FALSE) [1] 0.2632289
Note: You can find the complete documentation for the Gini() function from the DescTools package .
Additional Resources
The following tutorials explain how to calculate a Gini coefficient and how to create a Lorenz curve in Excel:
Cite this article
stats writer (2024). “How do you calculate the Gini coefficient in R, and can you provide an example?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-calculate-the-gini-coefficient-in-r-and-can-you-provide-an-example/
stats writer. "“How do you calculate the Gini coefficient in R, and can you provide an example?”." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-you-calculate-the-gini-coefficient-in-r-and-can-you-provide-an-example/.
stats writer. "“How do you calculate the Gini coefficient in R, and can you provide an example?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-calculate-the-gini-coefficient-in-r-and-can-you-provide-an-example/.
stats writer (2024) '“How do you calculate the Gini coefficient in R, and can you provide an example?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-calculate-the-gini-coefficient-in-r-and-can-you-provide-an-example/.
[1] stats writer, "“How do you calculate the Gini coefficient in R, and can you provide an example?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. “How do you calculate the Gini coefficient in R, and can you provide an example?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
