01 / 21
y ~ x

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.

R Programming

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: ~

y Response ~ "is modeled by" x1 + x2 Predictor(s) y ~ x1 + x2

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

Response (y)
The outcome variable you want to predict or explain. Also called the dependent variable. Always goes on the left side of ~.
Predictor (x)
The input variable(s) you believe influence the outcome. Also called independent variables. Always goes on the right side of ~.

The Core Rule of Formula Syntax

response ~ predictor1 + predictor2 + ... Left of ~ Single response variable Right of ~ One or more predictors + Add predictor : Interaction * Main + Interaction - Remove

Formula Operators Explained

1
+ (plus): Include an additional predictor. y ~ x1 + x2 means "model y using both x1 and x2."
2
: (colon): Interaction term only. y ~ x1:x2 includes only the interaction between x1 and x2.
3
* (star): Main effects plus interaction. y ~ x1*x2 expands to x1 + x2 + x1:x2.
4
- (minus): Remove a term. y ~ x1*x2 - x1:x2 removes the interaction, keeping only main effects.

+ 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

lm()
Linear regression models. lm(y ~ x, data=df) fits a straight line.
glm()
Generalized linear models for logistic, Poisson, and other regressions.
t.test()
Compare group means: score ~ group tests if scores differ by group.
ggplot2
Faceting with facet_wrap(~variable) uses formula syntax for layouts.

How to Build a Formula

1
Identify your response: What outcome are you trying to predict or explain? This goes on the left of ~.
2
Choose your predictors: What variables might influence the outcome? List them on the right of ~.
3
Connect predictors with +: If you have multiple predictors, join them with +.
4
Consider interactions: If predictors might depend on each other, use * instead of +.
5
Pass to a function: Place the formula inside lm(), glm(), t.test(), or another model function along with your data argument.

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.

# Fit a simple linear model
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.

# Multiple predictors with +
model2 <- lm(score ~ hours + sleep, data = students)
score ~ hours sleep + Two independent predictors, no interaction

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.

# Include interaction with *
model3 <- lm(score ~ hours * sleep, data = students)

# This is equivalent to:
lm(score ~ hours + sleep + hours:sleep, data = students)

Special Formula Patterns

y ~ .
Use all other columns in the data frame as predictors. The dot is a shortcut for "everything else."
y ~ 1
An intercept-only model with no predictors. Useful as a baseline or null model.
y ~ x - 1
Remove the intercept. Forces the regression line through the origin \( (0,0) \).

Interpreting Model Inputs

When you call summary(model), R shows you a Coefficients table. Each row corresponds to a term from your formula:

# summary(model2) output (simplified)
Coefficients:
             Estimate
(Intercept)  30.5
hours        5.2
sleep        2.8

Common Mistakes with Formulas

✕ Swapping Sides
Writing x ~ y instead of y ~ x. The response must be on the left; predictors on the right.
✕ Using = Instead of ~
Writing y = x in a formula. R uses ~ for formulas, not the assignment or equality operator.
✕ Forgetting the Data Argument
Writing lm(y ~ x) without specifying data = df. R won't know where to find the variables.

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?