Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
The Gini coefficient formula, for incomes sorted from smallest to largest, is G = 2·Σ(i·yᵢ) / (n·Σyᵢ) − (n+1)/n, where yᵢ is the i-th sorted value and n is the number of observations. It returns 0 when every value is equal and approaches 1 when one observation holds everything. This guide shows how to calculate the Gini coefficient (also called the Gini index) for one worked example three equivalent ways, then covers the scale, correction and negative-value pitfalls that make tools disagree.
What the Gini coefficient is used for in research
Researchers use it to summarise how unequally a non-negative quantity (income, wealth, citations, grant funding, species abundance) is spread across units. Our World in Data describes two equivalent readings: the expected absolute gap between two randomly chosen people relative to the mean, and the area between the Lorenz curve and the line of perfect equality as a share of the whole area under that line (Gini = A/(A+B)). The World Bank DataBank glossary gives the same definition in terms of the area between the Lorenz curve and a hypothetical line of absolute equality.
Gini coefficient formula: three equivalent forms
1. Sorted-sum formula (how to calculate the Gini index by hand)
Sort the values so that y₁ ≤ y₂ ≤ … ≤ yₙ, then:
G = 2·Σ(i·yᵢ) / (n·Σyᵢ) − (n+1)/n
An algebraically equivalent form is G = (1/n)·(n + 1 − 2·Σ((n+1−i)·yᵢ) / Σyᵢ). Both are given on the Wikipedia Gini coefficient article.
2. Mean absolute difference form
G is the average of all pairwise absolute differences between observations, divided by twice the mean: G = Σᵢ Σⱼ |yᵢ − yⱼ| / (2·n²·ȳ). This is the “average pairwise difference, normalised by twice the mean” description in the World Bank glossary.
3. Lorenz curve form
Plot the cumulative share of the population (x) against the cumulative share of the total (y) after sorting. G = 1 − 2·(area under the Lorenz curve). With n observations the area can be computed with trapezoids.
Worked example: calculating the Gini coefficient for five incomes
This is an illustrative calculation with made-up numbers, not data from any study. Take five incomes (in thousands): 20, 30, 40, 50, 60. Then n = 5 and Σy = 200.
| i | yᵢ | i·yᵢ | Cumulative share of total |
|---|---|---|---|
| 1 | 20 | 20 | 0.10 |
| 2 | 30 | 60 | 0.25 |
| 3 | 40 | 120 | 0.45 |
| 4 | 50 | 200 | 0.70 |
| 5 | 60 | 300 | 1.00 |
- Sorted-sum: Σ(i·yᵢ) = 700, so G = 2·700 / (5·200) − 6/5 = 1.4 − 1.2 = 0.2.
- Pairwise differences: the sum of |yᵢ − yⱼ| over all 25 ordered pairs is 400; the mean is 40, so G = 400 / (2·25·40) = 0.2.
- Lorenz trapezoids: with points (0,0), (0.2,0.10), (0.4,0.25), (0.6,0.45), (0.8,0.70), (1,1.00), the area under the curve is 0.4, so G = 1 − 2·0.4 = 0.2.
Wikipedia notes a sample version that uses 1/(n−1) in place of 1/n; it is consistent but not generally unbiased. Applied here it gives 0.2 × 5/4 = 0.25. Always record which version a tool applies.
How to install or access it
- R: install the
ineqpackage from CRAN, the official R package repository, withinstall.packages("ineq"), then load it withlibrary(ineq). The package reference manual is at https://cran.r-project.org/web/packages/ineq/ineq.pdf. - Spreadsheet: nothing to install; any spreadsheet program that can sort, multiply and sum columns will do.
Step-by-step: calculate the Gini coefficient in software
R: the ineq package
The CRAN ineq package documents Gini(x, corr = FALSE, na.rm = TRUE), where x is a vector of non-negative elements, corr switches a finite-sample correction on or off, and na.rm = TRUE removes missing values first (with FALSE, a missing value gives NA). The general function ineq(x, parameter = NULL, type = "Gini") returns the same measure by default.
- Install and load the package as described above.
- Put your non-negative values in a vector.
- Call
Gini()and compare the result with the hand calculation.
library(ineq)
x <- c(20, 30, 40, 50, 60)
Gini(x) # uncorrected version
Gini(x, corr = TRUE) # finite-sample correction
The manual’s own example uses x <- c(541, 1463, 2445, 3438, 4437, 5401, 6392, 8304, 11904, 22261); ineq(x). Compare the output of Gini(x) with the hand calculation above as a check on your setup; the package defines the exact correction it applies when corr = TRUE, so read the manual entry before reporting that value.
Spreadsheet (numbered steps)
- Put the values in one column and sort ascending.
- Add a rank column i = 1…n and a column i·yᵢ.
- Sum the values and the i·yᵢ column, then apply G = 2·Σ(i·yᵢ)/(n·Σy) − (n+1)/n.
- Check the result against the pairwise or Lorenz method on a small test set such as the one above.
Output and how to read it
Is the Gini coefficient between 0 and 1?
Yes, for non-negative data it ranges from 0 (perfect equality) to 1 (perfect inequality), and Our World in Data notes it is also reported on a 0 to 100 scale as the Gini index. The World Bank glossary expresses its series as a percentage, so 40 on that scale equals 0.40. For a finite sample of N people the maximum is 1 − 1/N, not exactly 1 (Wikipedia). Negative values (for example net wealth with debt) can push the coefficient above 1; Our World in Data notes they are typically dropped or set to zero.
What does a 0.5 Gini coefficient mean?
It means substantial inequality in the distribution. Wikipedia gives example distributions that produce 0.5: the lowest 50% have zero income and the rest share equally, or the lowest 75% hold 25% of the total and the top 25% hold 75%.
What is a good Gini coefficient?
There is no universal “good” value: the coefficient describes dispersion, not desirability. Our World in Data notes it is more sensitive to the middle of the distribution than to the extremes. The World Bank glossary adds that different Lorenz curves can give the same Gini, that it is relative rather than absolute, and that it is not additive across groups, so compare like with like.
Which country has the highest Gini index?
The answer depends on the year, the survey and whether income or consumption is measured; the World Bank notes that survey methods and these choices make values not strictly comparable between countries. Look up the current ranking in the World Bank Poverty and Inequality data rather than relying on a fixed list. Our World in Data states that the coefficient alone does not rank countries.
Troubleshooting
- Result is 20 instead of 0.2: the tool or source reports the 0-100 index; divide by 100.
- NA from R: missing values with
na.rm = FALSE; keep the defaultna.rm = TRUEor clean the vector. - Two tools disagree slightly: check whether one applies an n/(n−1) correction (
corrinineq). - Result above 1 or odd values: negative inputs; decide and document whether to drop or set them to zero.
- Value cannot reach 1: the maximum is 1 − 1/n for n observations.
Free alternatives
| Option | What the source documents |
|---|---|
R package ineq (CRAN) |
Gini() and ineq() with optional finite-sample correction and NA handling. |
| Hand or spreadsheet calculation | The sorted-sum formula above uses only sorting, sums and arithmetic. |
Frequently asked questions
Is the Gini coefficient between 0 and 1?
For non-negative data, yes: 0 is perfect equality and 1 is perfect inequality. It is often also reported as a 0 to 100 index. See “Output and how to read it” above.
What does a 0.5 Gini coefficient mean?
Substantial inequality. For example, if the lowest 50% have zero income and the rest share equally, G is 0.5.
What is a good Gini coefficient?
There is no universal good value; it describes dispersion, not desirability, so compare like with like.
Which country has the highest Gini index?
It depends on the year, survey and whether income or consumption is measured. Check the current World Bank Poverty and Inequality data rather than a fixed list.
How do I find the Gini coefficient from a Lorenz curve?
Gini = A/(A+B), where A is the area between the line of equality and the curve, and equivalently 1 − 2 × (area under the curve).
Why is the maximum below 1 for small samples?
With N people the maximum is 1 − 1/N, reaching 1 only as N grows.
Is the Gini a good fit for every distribution?
No. Because it is not unique, different distributions can share a value, so report it alongside the distribution itself, for example with a chart (see data visualization examples).
Related CASRAI guides
- Simpson diversity index, another single-number summary of a distribution
- Wilcoxon test in R, for R-based nonparametric analysis
- Random forests in R
Sources
- CRAN, ineq package reference manual: https://cran.r-project.org/web/packages/ineq/ineq.pdf (read 2026-11-07)
- World Bank DataBank, Gini index glossary entry: https://databank.worldbank.org/metadataglossary/world-development-indicators/series/SI.POV.GINI (read 2026-11-07)
- Our World in Data, What is the Gini coefficient?: https://ourworldindata.org/what-is-the-gini-coefficient (read 2026-11-07)
- Wikipedia, Gini coefficient (formula cross-check): https://en.wikipedia.org/wiki/Gini_coefficient (read 2026-11-07)







