How to calculate Gini Coefficient in R (With Example)

The Gini coefficient can be calculated in R using the ginicoeff() function from the inequality package. This function takes a vector of values representing the proportions of the population in each income group as its argument. An example of this calculation is to take a vector of income values and divide it into five equal groups. The Gini coefficient can then be calculated by passing this vector into the ginicoeff() function. The result will be a value between 0 and 1, with 0 being perfect equality and 1 being perfect inequality.


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 .

The following tutorials explain how to calculate a Gini coefficient and how to create a Lorenz curve in Excel:

x