Module 02 Activity
Scenario
Read a colleague's script and find the assumptions it makes silently.
What you build
An annotated review of a short script, plus a corrected version.
Steps
- Rename every vague object so the name says what a row or a value is. Flag anything named after a base R function.
- Find each place two vectors are combined and confirm they are the same length or that one has length one.
- Find every comparison and decide what should happen when the value is NA. Replace any
== NAwithis.na(). - Find every comparison of computed decimals and replace
==withall.equal(). - For each function the script calls, check the defaults that matter — especially
na.rm— and state which the script is relying on. - Turn the calculation that appears three times into one function with a guard on its inputs.
Evidence to hand in
- The renamed objects, with the reason for each.
- The length audit of every vector combination.
- The NA handling decisions.
- The decimal comparisons you replaced.
- The defaults the script relies on.
- The extracted function with its guard.
Review checklist
- No object is named after a base R function.
- Every vector combination is length-checked, and no recycling warning is ignored.
- Missingness is tested with
is.na()and never with==. - No computed decimal is compared with
==. - The extracted function stops on impossible input rather than returning Inf or NaN.
