Attribute analysis and statistics

Distinct counts, median and standard deviation by group

Go beyond totals: count distinct values, find the median, and measure the spread of values with the standard deviation for each group, and see how missing values are counted.

Intermediate2 min read

You will learn

  • Count distinct values per group.
  • Use the median when values are skewed.
  • Choose between sample and population standard deviation.

Prerequisites

Data for this tutorial

Sample villages (synthetic) Download villages.zip

Format
ESRI Shapefile (ZIP: .shp, .shx, .dbf, .prj, .cpg)
Geometry
Point
CRS
EPSG:4326 (WGS 84 longitude, latitude)
Content
25 villages in 5 districts of 2 states
Fields used in the tutorials
FieldTypeDescription
village_idtextIdentifier V001 to V025.
villagetextVillage name. Common names repeat (Rampur, Shivpur, Madhopur), as they do in real gazetteers.
districttextDistrict: Rampur, Varanasi, Jaunpur, Patna or Gaya.
statetextState: Uttar Pradesh or Bihar.
populationintegerResidents. One village (Amarpur) has no recorded population (NULL).
householdsintegerHouseholds.
has_schooltextyes or no.

Licence: CC0 1.0. All tutorial data is synthetic and generated by Orbis. About the datasets

The task

Per state you want to know how many different village names occur, the typical (median) village population, how much populations vary, and how many villages lack a population value.

Steps

  1. Load villages.zip, open the attribute table and click Group and summarize. Tick state.
  1. Set up these aggregates (change the function of the default one, and add the others with Count and Median next to Add, then change their function and field):

    FunctionFieldOutput name
    Count recordsvillages
    Count distinct valuesvillagenames
    Medianpopulationmedian_population
    Standard deviation (sample)populationsd_population
    Count NULL valuespopulationno_population
  1. Click Run.

    The summary by state with the number of villages, distinct names, median and standard deviation of population and the count of missing values
    Distinct counts, medians and standard deviations by state.

Expected result

statevillagesnamesmedian_populationsd_populationno_population
Bihar10942252039.950
Uttar Pradesh151129002063.031

Standard deviations are shown to two decimals. Bihar has 10 villages but only 9 names because "Rampur" occurs in two of its districts. Uttar Pradesh has one village without a population, so its median and standard deviation use 14 values.

Common mistakes

  • Expecting COUNT DISTINCT to count empty values. Distinct counts ignore NULL. Use Count NULL values to count missing values.
  • Using the mean for skewed data. A few large villages raise the mean; the median is the value in the middle and is not affected by extremes.

Troubleshooting

  • The standard deviation is empty. The group has fewer than two values (sample statistic). Use the population version or check the group size with Count values (non-NULL).
  • Distinct counts look too high. Values that differ only in case or spaces are distinct. Clean them first.