HOWTO · R
Use the Tilde Symbol (~) in R
Learn what the tilde symbol (~) means in R formulas, use it with lm(), and find its documentation with help().
On this page
In R, the tilde operator (~) creates a formula. In a model formula such as y ~ x, the left side names the response to model, and the right side specifies the predictor. Read it as “model y using x.” Creating the formula alone does not fit a model; a function such as lm() interprets it.
How to Use the Tilde Operator in R
Write response ~ predictor1 + predictor2 to specify a response and two predictors. In a model formula, + adds model terms rather than adding the predictor values together. A formula can also have only a right side, as in ~ x; whether that form is appropriate depends on the function receiving it.
Fit a Linear Model
The following self-contained example uses R’s standard lm() function. It creates a data frame, fits a model, and prints its coefficients.
measurements <- data.frame(
x = c(1, 2, 3, 4, 5),
y = c(3, 5, 6, 9, 11)
)
model <- lm(y ~ x, data = measurements)
print(coef(model))
(Intercept) x
0.8 2.0
Here, y is the response and x is the predictor. The data argument tells lm() where to look for those columns. By default, this model includes an intercept: coef(model) returns the intercept and the coefficient for x. In the fitted line, the intercept represents the fitted response at x = 0, and the coefficient for x represents the change in the fitted response for a one-unit increase in x. Use summary(model) to request the fuller statistical summary.
To adapt the formula, replace the column names and supply your own data frame. For multiple predictors, use a formula such as y ~ x1 + x2. Within lm(), y ~ x1 * x2 includes both predictors and their interaction, while y ~ x - 1 removes the intercept. These are syntax illustrations, not executed examples. Check spelling and ensure that referenced columns exist in the supplied data.
Construct a Formula Dynamically
When variable names are stored as strings, as.formula() can convert formula text into an object. Run this self-contained example to construct and print a formula from the response name "y" and predictor names "x1" and "x2":
response <- "y"
predictors <- c("x1", "x2")
formula_text <- paste(response, "~", paste(predictors, collapse = " + "))
model_formula <- as.formula(formula_text)
print(model_formula)
y ~ x1 + x2
The inner paste() joins the predictor names with +, and the outer call places the response before ~. The resulting formula specifies y using both predictors. Construction alone does not fit a model or estimate coefficients. To fit it, pass model_formula to lm() with a data frame containing the named columns; the columns are not needed just to construct the formula.
Look Up the Operator
Use the help() function with the operator in quotes: help("~"). This requests R’s tilde documentation. Its presentation depends on your R interface and help settings; it does not require a particular RStudio file-browser tab. For model-term syntax, also consult help("formula").
Conclusion
Use ~ to construct formulas and pass them to functions that accept formulas. For a linear model, place the response on the left and predictors on the right. Use help("~") to look up the operator and help("formula") for model-formula conventions.