16.2 C
London
Thursday, July 24, 2025
HomeStatistics TutorialRHow to Use the Tilde Operator (~) in R

How to Use the Tilde Operator (~) in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the tilde operator (~) in R to separate the left hand side of an equation from the right hand side.

This operator is most commonly used with the lm() function in R, which is used to fit linear regression models.

The basic syntax for the lm() function is:

model 

The variable name on the left side of the tilde operator (y) represents the response variable.

The variable names on the right side of the tilde operator (x1, x2) represent the predictor variables.

The following examples show how to use this tilde operator in different scenarios.

Example 1: Use Tilde Operator with One Predictor Variable

Suppose we fit the following simple linear regression model in R:

model 

This particular regression model has one response variable (y) and one predictor variable (x).

If we wrote out this regression equation in statistical notation it would look like this:

y = β0 + β1x

Example 2: Use Tilde Operator with Multiple Predictor Variables

Suppose we fit the following multiple linear regression model in R:

model 

This particular regression model has one response variable (y) and three predictor variables (x1, x2, x3).

If we wrote out this regression equation in statistical notation it would look like this:

y = β0 + β1x1 + β2x2 + β3x3

Example 3: Use Tilde Operator with Unknown Number of Predictor Variables

Suppose we fit the following multiple linear regression model in R:

model 

This particular syntax indicates that we would like to use y as the response variable and every other variable in the data frame as predictor variables.

This syntax is useful when we want to fit a regression model with tons of predictor variables but we don’t want to type out the individual name of every single predictor variable.

Additional Resources

The following tutorials explain how to use other common functions in R:

How to Use Dollar Sign ($) Operator in R
How to Use “NOT IN” Operator in R
How to Use %in% Operator in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories