Why Category Order Needs a Decision
A factor’s level order is not merely cosmetic. It can determine how R sorts and displays categories, how ordered-factor comparisons behave, and which contrasts may be used when a model matrix is formed. For genuinely ordered categories, the sequence should reflect their meaning. For nominal categories, imposing a ranking invents information the data do not contain.
The key decision is therefore substantive: does one category meaningfully rank above another? A response such as “low, medium, high” has an order. A variable such as payment method—cash, card, or bank transfer—does not. Alphabetical sorting may be convenient for display, but it is not evidence of an underlying ranking.
Set the Intended Order Explicitly in R
When R creates a factor without an explicit level list, it generally determines the levels by sorting the observed values. That may be acceptable for a nominal variable’s display, but it can be wrong for ordered categories. For example, alphabetical ordering of text labels can put “Tier 10” before “Tier 2.” Do not rely on a default sort to represent a substantive order.
For an ordinal variable, supply the intended levels in sequence. For example, the R expression `ordered(priority, levels = c("Routine", "Same day", "Immediate"))` creates an ordered factor whose levels run from Routine through Immediate. The alternative `factor(priority, levels = c("Routine", "Same day", "Immediate"), ordered = TRUE)` also creates an ordered factor. Check the result with `levels(priority_ord)` and `is.ordered(priority_ord)`.
The listed sequence is an assertion about the categories, not a request for R to infer their meaning. Before creating the factor, confirm that the labels in the data correspond to the levels you intend to use. An explicit order also makes the code easier to review and reproduce: another analyst can see the intended sequence rather than having to infer it from a sort.
Worked Example: Ordering Service Priorities
A hypothetical insurer records the service priority assigned to 12 claim files. The labels and counts are: Routine, 5; Same day, 4; Immediate, 3. Create an ordered factor, calculate the category proportions and cumulative proportions in priority order, and explain why the default level order should not be trusted.
Method. First define the substantive order, from least urgent to most urgent. Then use that sequence as the factor levels. The counts sum to \(5+4+3=12\), so the proportions are each count divided by 12.
R encoding. Use `ordered(priority, levels = c("Routine", "Same day", "Immediate"))`. The intended level sequence is Routine, Same day, Immediate. Check it with `levels(priority_ord)`; checking the sequence matters even if a printed frequency table happens to look plausible.
Calculations. Routine has proportion \(5/12=0.4167\); Same day has proportion \(4/12=0.3333\); Immediate has proportion \(3/12=0.2500\). In the intended order, the cumulative proportions are \(5/12=0.4167\), \((5+4)/12=9/12=0.7500\), and \((5+4+3)/12=12/12=1.0000\).
Check. The category counts total 12, the proportions total \(0.4167+0.3333+0.2500=1.0000\), and the final cumulative proportion is 1.0000. Alphabetical sorting would place Immediate before Routine and Same day, which would not express increasing urgency. The common mistake is to accept that automatic order as the priority order; the correction is to provide and verify the intended levels explicitly.
Order Is Not Distance
An ordinal factor gives a ranking, not a scale of measurement. This distinction explains why converting its categories to numbers can be misleading. Suppose three categories satisfy \(c_1 \prec c_2 \prec c_3\), where \(\prec\) means “comes before” in the stated order. Assigning ranks 1, 2, and 3 is one way to record that order. But the original information does not say that the gap from \(c_1\) to \(c_2\) equals the gap from \(c_2\) to \(c_3\).
Both assignments preserve the order: the first category is below the second, which is below the third. Yet the differences are 1 and 1 under the ranks \(r\), and 1 and 18 under the values \(g\). The order alone cannot tell us which differences are appropriate. That is why an ordered factor should not automatically be treated as a numeric measurement or as evidence of equal spacing.
Worked Example: Correcting a Misleading Tier Order
A hypothetical portfolio contains eight records with tier labels: Tier 2, Tier 10, Tier 1, Tier 2, Tier 10, Tier 1, Tier 10, Tier 2. The labels represent increasing tiers. Show why a default text sort is misleading, set the intended factor order, and interpret the resulting integer codes carefully.
Method. Count each label, inspect the order R would obtain by sorting the text, and then define the intended sequence explicitly. The counts are Tier 1: 2, Tier 2: 3, and Tier 10: 3; their sum is \(2+3+3=8\).
A typical alphabetical order is Tier 1, Tier 10, Tier 2, because the text “Tier 10” is compared before “Tier 2.” For increasing tier order, define the factor as `ordered(tier, levels = c("Tier 1", "Tier 2", "Tier 10"))`. Check that `levels(tier_ord)` returns Tier 1, Tier 2, Tier 10.
With that explicit sequence, the proportions are \(2/8=0.2500\), \(3/8=0.3750\), and \(3/8=0.3750\). The cumulative proportions in tier order are \(2/8=0.2500\), \((2+3)/8=5/8=0.6250\), and \((2+3+3)/8=8/8=1.0000\). These values check: the counts sum to eight and the proportions sum to one.
The internal integer codes for the ordered levels are 1, 2, and 3. Those codes mean first, second, and third in the declared sequence; they do not mean the recorded tier numbers 1, 2, and 10. For example, the difference between codes for Tier 2 and Tier 10 is 1, not the numerical difference of 8 between their labels. The common mistake is to treat `as.numeric(tier_ord)` as recovering the tier values. It returns the level codes, so interpret them only as ranks unless a separate quantitative definition justifies a numerical scale.
Nominal Factors, Comparisons, and Model Inputs
If categories have no inherent order, store them as an ordinary factor rather than an ordered factor. For example, “Web,” “Telephone,” and “Broker” are different service channels, but none is naturally greater than another. A chosen display sequence is possible, but a display sequence alone does not make the variable ordinal.
The distinction can matter beyond sorting. R’s default contrast settings commonly use polynomial contrasts for ordered factors when creating a model matrix, whereas unordered factors commonly use treatment contrasts. The exact contrasts depend on the model function and options in effect, so inspect them rather than assuming. If a genuinely nominal variable is accidentally made ordered, a downstream model can encode a trend across an arbitrary category sequence. If the variable is genuinely ordinal, the chosen sequence still needs to be defensible; ordering alone does not establish that a polynomial trend is appropriate.
This is separate from choosing a reference category. A nominal factor can have an explicitly arranged level sequence without claiming that its categories are ordered. The order used for display or reference-level handling should not be confused with an ordinal interpretation. For any factor used downstream, verify both its class and its levels.
| Question | Nominal factor | Ordinal factor |
|---|---|---|
| Do categories have an inherent ranking? | No | Yes |
| Should the analyst specify levels? | Useful for reproducible display or a deliberate reference choice | Yes; specify the substantive order |
| Do numeric codes imply meaningful distances? | No | No; codes express rank only |
| Example | Payment channel | Low, medium, high risk band |
Worked Example: Ranking Satisfaction Responses
A hypothetical service survey has nine responses: Very dissatisfied, Dissatisfied, Neither, Satisfied, Very satisfied, Very dissatisfied, Dissatisfied, Satisfied, Very satisfied. Define the response as ordinal, find the count and cumulative proportion for each response in order, and identify the middle observation.
Method. The response labels have a defensible satisfaction order, so specify that order from least to greatest satisfaction. Use the ordered categories to arrange the counts and locate the median position.
Create the factor with `ordered(response, levels = c("Very dissatisfied", "Dissatisfied", "Neither", "Satisfied", "Very satisfied"))`. The counts in this sequence are 2, 2, 1, 2, and 2. They total \(2+2+1+2+2=9\).
The corresponding proportions are \(2/9=0.2222\), \(2/9=0.2222\), \(1/9=0.1111\), \(2/9=0.2222\), and \(2/9=0.2222\). The cumulative proportions are \(2/9=0.2222\), \(4/9=0.4444\), \(5/9=0.5556\), \(7/9=0.7778\), and \(9/9=1.0000\). Rounded to four decimals, the proportions sum to \(0.9999\) because of rounding; before rounding, they sum to one.
With nine observations, the middle position is \((9+1)/2=5\). The first two categories occupy positions 1–4, so the fifth observation is “Neither.” Thus the median category is Neither. The common mistake is to sort the labels alphabetically and then identify a middle category from that order; the correction is to establish the meaningful response order first.
A Practical Verification Routine
Before using a categorical variable in a summary or downstream model, a short check can catch many avoidable errors. The following sequence separates the subject-matter decision from the R encoding decision.
Ask whether the categories have a defensible ranking in the application. If not, keep the variable nominal.
For an ordinal variable, list every category from lowest to highest (or the reverse, if that convention is clearly documented).
Use `ordered(x, levels = ...)` for an ordered factor, or `factor(x, levels = ...)` for a nominal factor with a deliberate level sequence.
Inspect `levels(x)`, `is.ordered(x)`, and a frequency table. Confirm the categories appear in the intended order and the counts agree with the data.
If the factor enters a model, inspect the contrasts or model matrix. Confirm that the encoding matches the intended analysis rather than an accidental text order.
Common Mistakes
- Assuming alphabetical order is substantive order. Text sorting can put labels such as “Tier 10” before “Tier 2.” Supply the intended sequence explicitly.
- Imposing an order on a nominal variable. A sequence chosen for a table does not establish that one category is greater than another.
- Treating ordinal ranks as equal-spaced measurements. The integer codes record position in the order, not the size of a difference.
- Assuming an ordered factor guarantees the desired model encoding. Contrast defaults and model settings matter; inspect the actual contrasts used.
- Checking only the printed table. A plausible-looking frequency table does not confirm the factor class or the intended levels. Check both.
Check Your Understanding
For each problem, decide what the categories mean before choosing an R representation.
- A hypothetical risk team records four bands—Low, Guarded, Elevated, and Severe—with counts 6, 5, 3, and 2. Give an explicit ordered-factor expression and the cumulative proportions in that sequence. (Answer: `ordered(band, levels = c("Low", "Guarded", "Elevated", "Severe"))`; cumulative proportions are 0.3750, 0.6875, 0.8750, and 1.0000.)
- A data set contains product labels Plan 1, Plan 2, and Plan 12, where the number indicates an increasing plan tier. What level sequence should be specified, and why is a default text sort unsafe? (Answer: Plan 1, Plan 2, Plan 12; text sorting can place Plan 12 before Plan 2.)
- A hypothetical dataset records the contact channel—Email, Phone, or Portal—for 20 customers. Should it be an ordered factor? State the appropriate factor type and explain what would be wrong with assigning an ordinal meaning. (Answer: Use a nominal factor; there is no inherent ranking among the channels, so an ordered encoding would invent one.)
- An ordered factor has levels “Basic,” “Plus,” and “Premier.” What do its internal codes 1, 2, and 3 establish, and what do they not establish? (Answer: They establish the category rank Basic before Plus before Premier; they do not establish equal distances or measured values.)
- A hypothetical survey has ordered ratings Poor, Fair, Good, Excellent, with counts 2, 4, 3, and 1. Give the cumulative proportion through Good and identify the median category among the ten responses. (Answer: The cumulative proportion through Good is \(9/10=0.9000\); the fifth and sixth observations are Fair, so the median category is Fair.)