Coding categories cleanly
Merge spellings of the same level, keep levels that really differ apart, and handle rare levels with a stated rule.
What runs in CodeSplash
- Frequencies (categorical variables) Verified in Python and R
You will be able to
- Clean case and whitespace so one level has one spelling
- Merge near-duplicate spellings with Recode values and Find similar values
- Decide when to group rare categories
Read first
One level, many spellings
ReadTo a computer, New York, NEW YORK and New York␠␠ with trailing spaces are three different values. A bar chart gets three bars, and a test gets three groups.
The customer list has this problem in both city and plan.
How many plans?
PredictPredict first
The business sells three plans: basic, pro and enterprise. How many different non-empty spellings does the plan column contain?
Make your guess between 3 and 20 spellings, then reveal.
Reveal
Fifteen. Each plan appears in lower case, upper case and title case, and with leading or trailing spaces. Three levels look like fifteen.
Clean, then merge
In the appIn Prepare (⌘3), work from the mechanical to the judgment calls:
- Clean text trims whitespace and sets case. That settles most of
plan. - Recode values maps what remains with a lookup table. Its Find similar values button groups spellings such as
newyorkandnew york; you choose the value to keep. - Open Suggested fixes to see the same steps proposed from the profile.
Each step is a line in your recipe, so the merges are written down rather than lost in a spreadsheet.
Similar isn't the same
ReadA clustering tool proposes merges; you decide. Georgia the state and Georgia the country share a spelling and differ in meaning. Hospital A and Hospital B differ by one letter and are different places.
Rare levels
ExploreGroup rare categories folds infrequent levels into one "Other" level. It helps when a model or a table would otherwise have dozens of tiny groups.
Try it on a copy of city, and look at what ends up inside "Other". Then ask whether anyone cares about one of those levels. A rare level can be the most important one: a rare complication is still the complication you came to study.
Check your understanding
CheckpointCheck yourself
You compare satisfaction across plan before cleaning. What happens?
Show the answer
Answer: b
Nothing stops you. The test runs, on more groups than exist, with fewer people in each. That is why cleaning comes before testing.
Check yourself
Put these cleaning steps in a sensible order.
Show the answer
The order:
- Trim whitespace and set one case with Clean text
- Merge the remaining spellings with Recode values
- Check the result with Frequencies
Mechanical cleaning first removes most of the variants, so the lookup table only has to handle what is left. Then verify.
Recap
Recap- Case and whitespace create phantom levels. Clean them first.
- Recode what remains, and let Find similar values propose rather than decide.
- Group rare levels only after checking that none of them is the point.