Recode values: replace codes and group ranges into classes

Updated

Recoding replaces values in a column by rules: every 1 becomes Morning, every value from 58 to 62 becomes OK, every missing value becomes Unknown. It turns codes into readable names, groups a measurement into classes, and cleans up inconsistent labels, without retyping the data by hand. The result goes into a new column, or replaces the original.

When to use it

  • A data logger or an MES export writes shifts, machines or defect types as numbers, and your reports need names.
  • You want to group a measurement into classes, such as below, within and above specification, to count each class or to draw a Pareto chart.
  • Labels are inconsistent (Pass, PASS, OK) and must be made the same before you count them or unstack by them.
  • You need numbers from text, such as TRUE/FALSE or Pass/Fail turned into 1 and 0.

How to do it in StatLab

  1. Select a cell in the column to recode and choose Data › Recode values….
  2. Check the Column.
  3. Fill in one rule per row: a Current value and its New value. Add a rule gives more rows. A current value can be:
    • one value: 3, Line A. Text must match exactly, including capitals;
    • a range of numbers: 1:5 covers every value from 1 to 5, both ends included;
    • *: missing values.
  4. Under Put the result, choose In a new column (named like Shift_coded) or Replace the original column.
  5. Press OK. Results records which column was recoded into which, and Ctrl+Z undoes it.

How the rules apply:

  • Each value takes the first rule that matches it, from the top. Put the most specific rule first.
  • Values that no rule matches stay as they are.
  • The result's type follows its contents. If every result is a number, the column is numeric; if any result is text, the whole column is text, and the unmatched numbers become text too.

Example

A heat-treatment line logs the shift as 1, 2 or 3 and the hardness of each part in HRC. The specification is 58 to 62 HRC. For one part, the shift was not recorded.

Before:

C1 ShiftC2 Hardness
158.2
261.5
355.9
163.1
*59.4
257.3
364.8
162

Shift codes to names. Recode Shift with four rules, into a new column:

Current valueNew value
1Morning
2Afternoon
3Night
*Unknown

Hardness to classes. Recode Hardness with three rules, in this order:

Current valueNew value
58:62OK
0:58Soft
62:100Hard

The ranges touch at 58 and 62, so no value can fall between them. Because the first matching rule wins, a part at exactly 58 or 62 HRC is OK, which is right for a specification that includes its limits.

After both recodes:

C1 ShiftC2 HardnessC3-T Shift_codedC4-T Hardness_coded
158.2MorningOK
261.5AfternoonOK
355.9NightSoft
163.1MorningHard
*59.4UnknownOK
257.3AfternoonSoft
364.8NightHard
162MorningOK

Counting the classes gives four parts OK, two soft and two hard:

Parts by hardness class01234PartsSoftOKHard
Parts by hardness class

The new columns make the next question easy to ask: both night-shift parts are out of specification, one soft and one hard. Eight parts are far too few to conclude anything, but it is a lead worth collecting more data for.

How to read the results

  • Scan the new column for leftovers. Any original value still visible means no rule matched it. In a text result, an unmatched 4 stays as the text 4, which is easy to miss.
  • Check the label. A -T label means text; a plain label means every result was a number.
  • Count each class. Make sure the counts add up to the number of rows, including the missing ones you recoded.

Common mistakes

  • Gaps between ranges. Rules 0:57.9 and 58:62 leave nothing for 57.95, which then stays as a number in a text column. Make ranges touch (0:58, 58:62) and use rule order to decide the shared boundary.
  • Case and spelling in text rules. A rule for Pass does not match pass or PASS. Add a rule for each spelling, all with the same new value.
  • Replacing the original by accident. Replace the original column loses the raw values. Recoding into a new column keeps the measurement next to its class, which you will want when someone asks why a part was called soft.
  • Forgetting the missing values. Without a * rule, missing values stay missing. That is often right, but decide on purpose.

Frequently asked questions

Can I recode text into numbers?

Yes. If every result is a number, the new column is numeric. For example, rules Pass → 1 and Fail → 0 give a numeric column, as long as no other text is left.

Can I recode dates?

Yes, with single dates or ranges of dates written as you would type them. For grouping dates by month or weekday, Extract from date/time is usually simpler.

What if two rules cover the same value?

The first rule from the top wins. Use this on purpose for boundaries, as in the example.

How do I group values into equal-width classes?

Write one range rule per class, such as 0:10, 10:20, 20:30, with the shared boundaries going to the earlier rule. For a quick formula instead, the calculator can compute a class with FLOOR(Hardness / 2) * 2.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab