There is a formula data type in R that specifies some selection and transformation of columns in a dataframe as input to a modeling or mathematical function.

For example,

lm(mpg ~ wt + hp, data = mtcars)

Will fit a linear model with mpg as the response (y) variable and wt and hp as the predictor (x) variable, both taken from the mtcars dataframe.

Why is this useful? Well, without it, you have to prepare x and y matrix arguments every time you fit a model. This is how most other mathematical languages work and it's kind of a pain. You'd have to write something like

# pseudo-code
x <- as.matrix(mtcars[, c("wt", "hp")])
y <- mtcars[, "mpg"]
fit_lm(x, y)

The formula type makes it easy and agile to try models with various terms or transformations (i.e. log(mpg) ~ wt + hp) without having to define x and y inputs every...single...time.

The formula type also avoids having to type a lot of indexing $ or [] or quotes "hp". Elegance is prized in R and it's nice to have clean code without lots of quotes and brackets.

The ~ symbol is a part of a formula. It separates the response and predictor variables in specification of a model. Or, sometimes, there is a one-sided formula if the math has now equivalent of a response variable and it's just ~x1 + x2.

The . is a shorthand for "include all other columns." So, mpg ~ . is shorthand for mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb.

model.matrix is a function that creates the matrix input to a regression. It will expand out factor variables into dummy variables. It will keep numeric variables as-is. If there is a transformation defined in the formula, it will perform it. You can give it the set of variables to the right of the ~ and it will do all this preparation for you.

Answer from Arthur on Stack Overflow
🌐
McGill Library
libraryguides.mcgill.ca › c.php
Common Operators in R - Learn R - Guides at McGill Library
December 24, 2025 - This guide focuses on transformation and cleaning functions in R that are especially useful for working with tabular datasets.
🌐
DataCamp
datacamp.com › doc › r › operators
Operators in R
Learn about R's binary and logical operators for vectors and matrices. Includes examples and tips for effective use in programming.
Discussions

What does "$" do?
The other answers are correct. I find it helpful to think of it as accessing a named variable in the next level of any data object in R. So a dataframe 'df' with a column named 'a': df$a Or a list named 'list1' with many dataframes, one of which is named df and you want to access the column named b: list1$df$b Or a list of lists with dataframes (hopefully you get the idea by now): list1$list2$df$c More on reddit.com
🌐 r/RStudio
4
1
August 27, 2022
Hello everyone, newbie here. What does “!” do in R?
You might see it with something like x != 42, which would indicate which elements of x are not equal to 42. More on reddit.com
🌐 r/rprogramming
11
10
April 1, 2022
Reddit - The heart of the internet
Reddit is where millions of people gather for conversations about the things they care about, in over 100,000 subreddit communities. More on reddit.com
🌐 reddit.com
1 day ago
ELI5: What is R? How are programming languages used for statistics?
R is a programming language that is specialized for working with datasets, especially tabular datasets. It is like SQL in that it lets you manipulate datasets across multiple sources and tables, but it is much more powerful. The connection to statistics is just because it is setup to make it very easy to do them - there’s a single command to run linear regression, simple tools to plot things, and very human-readable code. One key feature of R is that a lot of functions are “vectorized”, which lets you do the same thing to a bunch of numbers all at once without having to write your own loop (like you would in a lower language like C). So, if the variable a has a single number stored in it, I can write sqrt(a) and get the square root. But if the variable a has a vector of 1000 numbers stored in it, I can still just write sqrt(a) and it will return a vector with all 1000 square roots. Regarding “tidyverse”: like many programming languages, R has some free “add-ons” called packages or libraries. These are just collections of new functions, tools, methods, and data objects that someone else has written and made available. For example, the “parallel” package (which comes default) gives you tools for parallel computing (using multiple cores at once). Well, there is a guy named Hadley Wickham that is one of the most well known R gurus, and he and his team have written a whole “ecosystem” of R packages that help manipulate and visualize data. These packages include a very customizable plotting package called ggplot2, and two data manipulating packages called tidyr and dplyr. Along with some other packages (e.g. stringr for pattern matching/regular expressions), these are all bundled up into the “tidyverse” collection of packages. The reason tidyverse is mentioned so much and so explicitly is that those packages introduce a lot of their own syntax and structure that’s shared between them, such that it’s almost like a mini-language within the larger R language. You can almost always accomplish the same things with both tidyverse and non-tidyverse methods, but they will look extremely different, sometimes involve two different ways of thinking about the problem, and definitely use different syntax. Describing an entire programming language is a bit difficult, but I’m happy to answer any follow up questions you might have. More on reddit.com
🌐 r/explainlikeimfive
14
17
November 26, 2023
Top answer
1 of 2
5

There is a formula data type in R that specifies some selection and transformation of columns in a dataframe as input to a modeling or mathematical function.

For example,

lm(mpg ~ wt + hp, data = mtcars)

Will fit a linear model with mpg as the response (y) variable and wt and hp as the predictor (x) variable, both taken from the mtcars dataframe.

Why is this useful? Well, without it, you have to prepare x and y matrix arguments every time you fit a model. This is how most other mathematical languages work and it's kind of a pain. You'd have to write something like

# pseudo-code
x <- as.matrix(mtcars[, c("wt", "hp")])
y <- mtcars[, "mpg"]
fit_lm(x, y)

The formula type makes it easy and agile to try models with various terms or transformations (i.e. log(mpg) ~ wt + hp) without having to define x and y inputs every...single...time.

The formula type also avoids having to type a lot of indexing $ or [] or quotes "hp". Elegance is prized in R and it's nice to have clean code without lots of quotes and brackets.

The ~ symbol is a part of a formula. It separates the response and predictor variables in specification of a model. Or, sometimes, there is a one-sided formula if the math has now equivalent of a response variable and it's just ~x1 + x2.

The . is a shorthand for "include all other columns." So, mpg ~ . is shorthand for mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb.

model.matrix is a function that creates the matrix input to a regression. It will expand out factor variables into dummy variables. It will keep numeric variables as-is. If there is a transformation defined in the formula, it will perform it. You can give it the set of variables to the right of the ~ and it will do all this preparation for you.

2 of 2
0

The dot means all columns in your data.frame not otherwise mentioned.

Running a few examples with the iris dataset shows this pretty well.

library(tidyverse)

model.matrix( ~ ., data = iris) %>% colnames()
# [1] "(Intercept)"       "Sepal.Length"      "Sepal.Width"      
# [4] "Petal.Length"      "Petal.Width"       "Speciesversicolor"
# [7] "Speciesvirginica" 

model.matrix( ~ Petal.Length, data = iris) %>% colnames()
# [1] "(Intercept)"  "Petal.Length"

model.matrix(Petal.Length ~ ., data = iris) %>% colnames()
# [1] "(Intercept)"       "Sepal.Length"      "Sepal.Width"      
# [4] "Petal.Width"       "Speciesversicolor" "Speciesvirginica" 

You could forgo the . syntax and spell it out. I think this is a better approach since in this case a reader does not need to know what the columns in iris are. You can just tell what our model will include. It also has the side effect of preventing you from including something accidentally.

model.matrix( ~ Petal.Length + Sepal.Length + Sepal.Width + Species, data = iris) %>% colnames()
# [1] "(Intercept)"       "Sepal.Length"      "Sepal.Width"      
# [4] "Petal.Length"      "Petal.Width"       "Speciesversicolor"
# [7] "Speciesvirginica" 

Another resource on making these model matrices that I have used is this article: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7873980/. It's specific for RNA seq, but the model matrices can be used for any similar style of comparison/model.

🌐
Reddit
reddit.com › r/rstudio › what does "$" do?
r/RStudio on Reddit: What does "$" do?
August 27, 2022 - I find it helpful to think of it as accessing a named variable in the next level of any data object in R. ... Or a list named 'list1' with many dataframes, one of which is named df and you want to access the column named b: ... It gives you money. $$$ No seriously though, it allows you to access columns in a dataframe. ... Both [[ and $ select a single element of the list. The main difference is that $ does not allow computed indices, whereas [[ does.
🌐
R Project
r-project.org › about.html
R: What is R?
R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible. The S language is often the vehicle of choice for research in statistical methodology, ...
🌐
Ubc-library-rc
ubc-library-rc.github.io › IntroR › content › Concepts.html
Concepts & Syntax | Intro to R and RStudio
These operators are used to assign a value to a variable. Examples include <- and =. ... These operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), and division (/). Type ?Arithmetic to read the R document in the help tab.
Find elsewhere
🌐
Quora
quora.com › What-does-mean-in-R-7
What does ~ mean in R? - Quora
If I’m traveling or reading a lot, eBooks. If it’s a “sit and enjoy” book, physical. ... GGPlot is a packages used for better presentation of data. ... That’s a tilde. In Latin and derivatives it used to be placed over a letter to mark a missing one, in the way we do an apostrophe. You can still see them in Spanish punctuation and the French use a similar circumflex - fenêtre (fenestra). It doesn...
🌐
Reddit
reddit.com › r/rprogramming › hello everyone, newbie here. what does “!” do in r?
r/rprogramming on Reddit: Hello everyone, newbie here. What does “!” do in R?
April 1, 2022 - It’s does NOT! ... In almost every programming language, the exclamation point / bang sign means "not". So when someone is wanting to compare values, they will often use ! in making a comparison. ... Edit: Since OP is new to R and others maybe as well. The %>% are called pipes from the Tidyverse package.
🌐
Medium
medium.com › @kirudang › r-programming-tips-in-operator-with-ec25f1a19d46
R programming tips — %in% operator with !! | by Kiel Dang | Medium
February 3, 2023 - With the !!, the %in% operator checks if y is present in the vector x, but since y is not present in x, the output is still “y is not in x”, and !! doesn’t change the result.
🌐
R for Data Science
r4ds.had.co.nz › functions.html
19 Functions | R for Data Science
It’s impossible to do in general because so many good names are already taken by other packages, but avoiding the most common names from base R will avoid confusion. # Don't do this! T <- FALSE c <- 10 mean <- function(x) sum(x) Use comments, lines starting with #, to explain the “why” of your code. You generally should avoid comments that explain the “what” or the “how”. If you can’t understand what the code does from reading it, you should think about how to rewrite it to be more clear.
🌐
W3Schools
w3schools.com › r › r_operators.asp
R Operators
If...Else Nested If And Or R While Loop R For Loop ... Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values:
🌐
RDocumentation
rdocumentation.org › packages › dplyr › versions › 0.7.8 › topics › do
do function - RDocumentation
This is a general purpose complement to the specialised manipulation functions filter(), select(), mutate(), summarise() and arrange(). You can use do() to perform arbitrary computation, returning either a data frame or arbitrary objects which will be stored in a list.
🌐
R-bloggers
r-bloggers.com › r bloggers › the do.call() function in r: unlocking efficiency and flexibility
The do.call() function in R: Unlocking Efficiency and Flexibility | R-bloggers
June 1, 2023 - At its core, the do.call() function in R allows you to call other functions by constructing the function call as a list. It takes two arguments: the first being the function you want to call, and the second being a list of arguments to pass ...
🌐
R for Data Science
r4ds.hadley.nz › functions.html
25 Functions – R for Data Science (2e)
Data frame functions work like dplyr verbs: they take a data frame as the first argument, some extra arguments that say what to do with it, and return a data frame or a vector. To let you write a function that uses dplyr verbs, we’ll first introduce you to the challenge of indirection and how you can overcome it with embracing, {{ }}. With this theory under your belt, we’ll then show you a bunch of examples to illustrate what you might do with it.
🌐
HBC Training
hbctraining.github.io › Intro-to-R › lessons › 03_introR-functions-and-arguments.html
Functions in R | Introduction to R - ARCHIVED
September 8, 2017 - Finally, you can “return” the value of the object from the function, meaning pass the value of it into the global environment. The important idea behind functions is that objects that are created within the function are local to the environment of the function – they don’t exist outside of the function. NOTE: You can also have a function that doesn’t require any arguments, nor will it return anything.
🌐
DigitalOcean
digitalocean.com › community › tutorials › with-and-within-function-in-r
R with() and within() function - All you need to know! | DigitalOcean
August 4, 2022 - With R with() function, we can operate on R expressions as well as the process of calling that function in a single go! That is with() function enables us to evaluate an R expression within the function to be passed as an argument. It works on data frames only. That is why the outcome of the evaluation of the R expression is done with respect to the data frame passed to it as an argument.
🌐
R Project
cran.r-project.org › doc › manuals › r-devel › R-lang.pdf pdf
The R Language Definition Version 4.7.0 Under development (2026-07-12) DRAFT
In every computer language variables provide a means of accessing the data stored in memory. R does not provide direct access to the computer’s memory but rather provides a number
🌐
Spark By {Examples}
sparkbyexamples.com › home › r programming › usage of %in% operator in r
Usage of %in% Operator in R - Spark By {Examples}
May 28, 2024 - The %in% operator in R is used to check if the values of the first argument are present in the second argument and return a logical vector indicating if
🌐
GeeksforGeeks
geeksforgeeks.org › r language › how-to-use-do-call-in-r
How to Use do.call in R - GeeksforGeeks
July 23, 2025 - In R Programming language the do.call() function is used to execute a function call using a list of arguments.
🌐
R Project
r-project.org › help.html
R: Getting Help with R
To access documentation for the standard lm (linear model) function, for example, enter the command help(lm) or help("lm"), or ?lm or ?"lm" (i.e., the quotes are optional). To access help for a function in a package that’s not currently loaded, specify in addition the name of the package: For example, to obtain documentation for the rlm() (robust linear model) function in the MASS package, help(rlm, package="MASS").