Stating
Assumptions in R
Learn to explicitly declare what you assume about data types, missingness, and units before writing a single line of analysis code.
What Are Data Assumptions?
An assumption is a belief about your data that you accept as true before analysis. Every dataset carries hidden expectations: that ages are numeric, that prices are in dollars, that no values are missing. If any assumption is wrong, your entire analysis can silently produce incorrect results.
The Three Pillars of Assumptions
Why Stating Assumptions Matters
The Assumption Statement Rule
Before any analysis, write a comment block in your R script that explicitly states every assumption. Then write code that tests each one. If a test fails, stop the analysis and investigate.
# 1. `age` is numeric (integer or double)
# 2. `age` has no NA values
# 3. `age` is in years (not months)
# 4. `price` is in USD
# TESTS:
stopifnot(is.numeric(df$age))
stopifnot(!any(is.na(df$age)))
stopifnot(all(df$age >= 0 & df$age <= 150))
Implicit vs. Explicit Assumptions
| Implicit (Dangerous) | Explicit (Safe) | |
|---|---|---|
| Visibility | Hidden in your head | Written in code comments |
| Testable? | No: you forget to check | Yes: stopifnot() validates |
| When caught | After wrong results published | Before analysis runs |
| Reproducibility | Others can't see your logic | Fully documented for collaborators |
| Example | "I guess weight is in kg" | # Assume weight in kg, range 0-300 |
Pillar 1: Data Type Assumptions
Pillar 2: Missingness Assumptions
colSums(is.na(df))
# Assumption: income has \(< 5\%\) NA
stopifnot(mean(is.na(df$income)) < 0.05)
Pillar 3: Unit Assumptions
How to State Assumptions: Checklist
Example 1: Student Grades Dataset
You receive a CSV with columns name, grade, and gpa. Before computing average GPA, state your assumptions.
students <- read.csv("students.csv")
# ASSUMPTIONS:
# 1. `name` is character
# 2. `grade` is integer (9, 10, 11, 12)
# 3. `gpa` is numeric, range \([0.0, 4.0]\)
# 4. No NA values in gpa
# 5. GPA is on a 4.0 scale (not 5.0)
# VALIDATE:
stopifnot(is.character(students$name))
stopifnot(is.numeric(students$gpa))
stopifnot(all(students$gpa >= 0 &
students$gpa <= 4.0,
na.rm = TRUE))
stopifnot(!any(is.na(students$gpa)))
Example 2: Weather Sensor Data
A weather station logs timestamp, temp, and humidity every hour. You want to compute daily averages.
# 1. `timestamp` is POSIXct (not character)
# 2. `temp` is numeric, in Celsius
# 3. `temp` range: \([-50, 60]\) (reasonable)
# 4. `humidity` is numeric, range \([0, 100]\) (%)
# 5. Rows are hourly: \(\sim 24\) per day
# 6. Missing readings \(< 10\%\) per day
stopifnot(inherits(weather$timestamp,
"POSIXct"))
stopifnot(all(weather$temp >= -50 &
weather$temp <= 60,
na.rm = TRUE))
Example 3: E-Commerce Transactions
An online store exports order_id, product, quantity, price, and currency. You need to compute total revenue.
# 1. `price` is numeric, non-negative
# 2. `currency` is all "USD" (single currency)
# 3. `quantity` is positive integer
# 4. No duplicate order_id values
# 5. No NA in price or quantity
stopifnot(all(orders$currency == "USD"))
stopifnot(!any(duplicated(orders$order_id)))
stopifnot(all(orders$quantity > 0))
stopifnot(all(orders$price >= 0))
R Functions for Checking Assumptions
| What to Check | R Function | Returns |
|---|---|---|
| Column type | class(x), is.numeric(x) | Type name / TRUE-FALSE |
| All types at once | str(df) | Structure summary |
| Missing values | is.na(x), sum(is.na(x)) | Logical vector / count |
| Value range | range(x), summary(x) | Min and max / stats |
| Unique values | unique(x), length(unique(x)) | Distinct values / count |
| Assert condition | stopifnot(cond) | Error if FALSE |
Common Mistakes
Question 1 of 5
Which R function would you use to halt your script if an assumption is violated?
Question 2 of 5
A column called zip_code contains values like "02134". What data type assumption should you state?
Question 3 of 5
You discover that 40% of income values are NA, and all missing values come from respondents who declined to answer. This missingness pattern is:
Question 4 of 5
You merge two datasets: one records weight in kilograms, the other in pounds. Both columns are named "weight". What is the best first step?
Question 5 of 5
Consider this R code snippet:
df <- read.csv("survey.csv")
avg_age <- mean(df$age)
cat("Average age:", avg_age)
The programmer runs this code and gets the output: Average age: NA
Which unstated assumption most likely caused the NA result?