Written and maintained by CASRAI Editorial Board
Last updated
What is a categorical variable? Definition and examples
A categorical variable records which group or category an observation belongs to, not an amount. The CDC’s Principles of Epidemiology treats nominal- and ordinal-scale variables as qualitative, or categorical, variables. Categorical variables come in three practical forms: nominal (categories with no ranking), ordinal (categories that can be ranked) and binary (two categories). For the full four-level framework, see levels of measurement; for the term itself, see types of variables.
Nominal categorical variable examples
Nominal values are categories without any numerical ranking. Examples: county of residence (CDC); gender, ethnic background, and religious or political affiliation (Penn State STAT 504). In a research dataset, add the study site, treatment arm or funder type.
Ordinal categorical variable examples
Ordinal values can be ranked but are not necessarily evenly spaced. Examples: stage of cancer (CDC); extent of agreement and school letter grades (Penn State). The gap between stage I and stage II is not guaranteed to equal the gap between III and IV.
Binary (dichotomous) categorical variable examples
A nominal variable with two mutually exclusive categories is sometimes called a dichotomous variable (CDC). Examples: alive or dead, vaccinated or unvaccinated (CDC); “Did you get the flu? Yes or No” (Penn State). Penn State’s companion example, “How severe was your flu? Low, Medium or High”, is ordinal.
Formula: frequency and proportion
Categorical variables have no mean to compute. The basic summary is the count of each category and its share of the total:
pi = ni / N
- ni: the number of observations in category i.
- N: the total number of observations, the sum of all ni.
- pi: the proportion in category i; multiply by 100 for a percentage. The proportions sum to 1.
Worked example with the arithmetic shown
Illustrative data, invented for this page, not from a real study. Suppose 80 survey respondents each report one department (nominal) and one rating of lab-space availability (ordinal).
| Department | ni | Calculation | pi |
|---|---|---|---|
| Biology | 32 | 32 / 80 | 0.40 (40%) |
| Chemistry | 20 | 20 / 80 | 0.25 (25%) |
| Physics | 16 | 16 / 80 | 0.20 (20%) |
| Other | 12 | 12 / 80 | 0.15 (15%) |
| Total | 80 | 32 + 20 + 16 + 12 | 1.00 |
The mode is Biology (32 respondents). Now the ordinal rating: Low = 30, Medium = 35, High = 15, so N = 80. Cumulative counts are 30, 65 and 80, or 37.5%, 81.25% and 100%. With 80 values the median sits between the 40th and 41st ranked responses. Both fall in the Medium group (positions 31 to 65), so the median category is Medium. Ranking makes this median meaningful; for the nominal department variable it would not be.
To test whether departments are equally represented, compare the observed counts to an expected 80 / 4 = 20 each with a chi-square goodness-of-fit test: (32 − 20)2/20 = 7.20; (20 − 20)2/20 = 0; (16 − 20)2/20 = 0.80; (12 − 20)2/20 = 3.20. Sum: χ2 = 11.20 with df = 4 − 1 = 3, p = .011.
Interpretation: reading guidance by variable type
| Example | Type | Sensible summary | Related method on CASRAI |
|---|---|---|---|
| County of residence | Nominal | Counts, proportions, mode | Chi-square test, Cramer’s V |
| Vaccinated / unvaccinated | Binary | Counts, proportion in one category | Phi coefficient |
| Stage of cancer | Ordinal | Counts, proportions, median category | Ordinal logistic regression |
The worked example gives a result for the department variable: a chi-square of 11.20 on 3 degrees of freedom exceeds the 5% critical value of 7.815, so the counts differ from an even split. It says nothing about why. For help matching a variable type to a test, see how to choose a statistical test.
Assumptions and common mistakes
- Numeric codes do not make a variable quantitative. Coding Biology = 1 and Chemistry = 2 labels categories; averaging those codes is meaningless. Record the coding scheme in a codebook.
- Classification depends on the analysis. Penn State notes many variables can be treated as nominal or ordinal depending on the purpose of the analysis, for example majors such as English, psychology and computer science.
- Categories must be mutually exclusive, as in the CDC’s dichotomous definition, so each observation fits one category.
- Chi-square test counts, not percentages. Run it on the observed counts; the expected counts should not be tiny, so check them before relying on the p-value.
How to report it (APA example)
For the worked example: “Respondents were most often from Biology (n = 32, 40%), followed by Chemistry (n = 20, 25%), Physics (n = 16, 20%) and Other (n = 12, 15%). Department counts differed from an equal distribution, χ2(3, N = 80) = 11.20, p = .011.” Report the median category for ordinal variables (“the median rating was Medium”).
How to calculate it in Excel, R or Python
Excel (departments in A2:A81): count with =COUNTIF(A2:A81,"Biology"), proportion with =COUNTIF(A2:A81,"Biology")/COUNTA(A2:A81). For the goodness-of-fit p-value, put observed counts in B2:B5 and expected counts (20 each) in C2:C5, then =CHISQ.TEST(B2:B5,C2:C5).
R:
dept <- factor(rep(c("Biology","Chemistry","Physics","Other"), c(32,20,16,12)))
tab <- table(dept)
prop.table(tab)
chisq.test(tab) # equal expected proportions by default
rating <- factor(rep(c("Low","Medium","High"), c(30,35,15)),
levels = c("Low","Medium","High"), ordered = TRUE)
Python (pandas, SciPy):
import pandas as pd
from scipy.stats import chisquare
dept = pd.Series(["Biology"]*32 + ["Chemistry"]*20 + ["Physics"]*16 + ["Other"]*12)
print(dept.value_counts(normalize=True))
print(chisquare([32, 20, 16, 12])) # expected counts default to equal
Frequently asked questions
What is a categorical variable?
A variable whose values are categories rather than measured amounts. Nominal and ordinal variables are both classed as categorical (CDC); a two-category nominal variable is binary.
What is a categorical variable in research?
Any recorded characteristic of participants or units that sorts them into groups: site, treatment arm, disease stage, agreement rating. Penn State pairs such variables with frequency tables and bar plots for display.
Is a categorical variable the same as a qualitative variable?
The CDC uses the terms together: nominal- and ordinal-scale variables are “qualitative or categorical”.
What is the difference between nominal and ordinal?
Ordinal categories have a natural order; nominal categories do not.
Sources
- Centers for Disease Control and Prevention. Principles of Epidemiology in Public Health Practice, Lesson 2, Section 2 (archived). https://archive.cdc.gov/www_cdc_gov/csels/dsepd/ss1978/lesson2/section2.html. Date checked: 2026-10-29.
- Penn State Eberly College of Science. STAT 504: Analysis of Discrete Data, Lesson 1. https://online.stat.psu.edu/stat504/Lesson01. Date checked: 2026-10-29.








