Formula Syntax
in R Programming
Learn how R uses the tilde operator ~ to express relationships between variables, and interpret what goes into a statistical model.
What Is a Formula in R?
In R, a formula is a special expression that describes the relationship between variables. It tells R which variable you want to predict (the response) and which variables you want to use as inputs (the predictors).
Formulas appear everywhere in R: in linear models, plots, statistical tests, and more. Understanding formula syntax is a foundational skill for data analysis in R.
The Tilde Operator: ~
The tilde (~) is the heart of every R formula. Everything on the left is what you are trying to explain or predict. Everything on the right is what you use to explain it.
Response vs. Predictor Variables
The Core Rule of Formula Syntax
Formula Operators Explained
+ vs. *: A Key Distinction
| y ~ x1 + x2 | y ~ x1 * x2 | |
|---|---|---|
| Main effect of x1 | ✓ Included | ✓ Included |
| Main effect of x2 | ✓ Included | ✓ Included |
| Interaction x1:x2 | ✕ Not included | ✓ Included |
| Equivalent to | x1 + x2 | x1 + x2 + x1:x2 |
Where Formulas Appear in R
How to Build a Formula
Example 1: Simple Linear Model
Scenario: You have a dataset of students with columns score (exam score) and hours (hours studied). You want to see if study time predicts exam score.
model <- lm(score ~ hours, data = students)
# View the results
summary(model)
Example 2: Multiple Predictors
Scenario: Now your dataset also has sleep (hours of sleep the night before). You want to model score using both hours studied and sleep.
model2 <- lm(score ~ hours + sleep, data = students)
Example 3: Adding an Interaction
Scenario: You suspect that the benefit of studying more hours depends on how much sleep a student got. Students who slept well might benefit more from studying.
model3 <- lm(score ~ hours * sleep, data = students)
# This is equivalent to:
lm(score ~ hours + sleep + hours:sleep, data = students)
Special Formula Patterns
Interpreting Model Inputs
When you call summary(model), R shows you a Coefficients table. Each row corresponds to a term from your formula:
Coefficients:
Estimate
(Intercept) 30.5
hours 5.2
sleep 2.8
Common Mistakes with Formulas
Formula Syntax Cheat Sheet
| Formula | Meaning | Terms Included |
|---|---|---|
| y ~ x | Simple regression | intercept, x |
| y ~ x1 + x2 | Multiple regression | intercept, x1, x2 |
| y ~ x1 * x2 | With interaction | intercept, x1, x2, x1:x2 |
| y ~ . | All predictors | intercept + every column |
| y ~ x - 1 | No intercept | x only (through origin) |
| y ~ I(x^2) | Polynomial term | intercept, \( x^2 \) |
Question 1 of 5
In the formula weight ~ height + age, which variable is the response?
Question 2 of 5
What does the formula y ~ x1 * x2 expand to?
Question 3 of 5
You want to model test_score using every other column in your data frame as a predictor. Which formula is correct?
Question 4 of 5
A student writes lm(y = x, data = df) and gets an error. What is wrong?
Question 5 of 5
In the output of summary(lm(score ~ hours, data = df)), the coefficient for hours is 5.2. What does this mean conceptually?