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
🌐
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, ...
🌐
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.
Discussions

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
differential equations - What does [,i] do in R? - Stack Overflow
This question is in a collective: a subcommunity defined by tags with relevant content and experts. More on stackoverflow.com
🌐 stackoverflow.com
What is the %in% operator for?
Ok let's say I have a list of all the people who live in Chicago. We'll name that variable popChi. This variable is a text vector, so imagine popChi = c("name1", "name2", ...) So if I were to call popChi, the program would return something like "Bob Jones", "Susan Smith", ... Just for this example, assume nobody shares a name. If I give you a name, for example, testname = "Bobby Brown", and I asked you if he lives in Chicago, you could run the function: testname %in% popChi and it would return TRUE or FALSE depending on if any of the observations in popChi match testname. %in% is just a boolean operator. Otherwise we'd have to write a list of OR operators like so: testname == popChi[1] | testname == popChi[2] | testname == popChi[3] | ... You can see how that could get tedious if you have a list of 10 million names. Another way to understand it, it simplifies this procedure: matchvar = which(popChi == testname) !is.na(matchvar) More on reddit.com
🌐 r/Rlanguage
10
0
December 24, 2021
What does the "r" stand for?
🌐 r/AskReddit
12
0
July 5, 2025
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.

🌐
R Project
cran.r-project.org › doc › manuals › r-release › R-intro.html
An Introduction to R
See Graphical procedures, which ... R [Contents][Index] ... R commands, case sensitivity, etc. ... R is an integrated suite of software facilities for data manipulation, calculation and graphical display....
🌐
Medium
medium.com › @kirudang › r-programming-tips-in-operator-with-ec25f1a19d46
R programming tips — %in% operator with !! | by Kiel Dang | Medium
February 3, 2023 - R programming tips — %in% operator with !! R is a powerful programming language widely used in data analysis and statistics. To get the most out of R, it’s important to understand best practices …
🌐
R-bloggers
r-bloggers.com › r bloggers › how to use %in% operator in r
How to use %in% operator in R | R-bloggers
July 19, 2022 - This analysis’s (fictitious) objective is to determine whether there are data points available for particular days. head(ChickWeight) weight Time Chick Diet 1 42 0 1 1 2 51 2 1 1 3 59 4 1 1 4 64 6 1 1 5 76 8 1 1 6 93 10 1 1 ... The percent in percent operator can be used to compare two vectors. It will return an array with the same number of elements as the initial vector, each of which will indicate whether an element is present or absent (True or False).
Find elsewhere
🌐
Reddit
reddit.com › r/explainlikeimfive › eli5: what is r? how are programming languages used for statistics?
r/explainlikeimfive on Reddit: ELI5: What is R? How are programming languages used for statistics?
November 26, 2023 -

Hello, I started looking into R and understand that it is a program language, but I'm unsure how it's linked back to Statistics... Is it similar to SQL in that you are calling and adjusting data across tables?

I'm looking into learning R and I've seen "tidyverse" mentioned many times. Would appreciate a description of this as well.

Top answer
1 of 2
39
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.
2 of 2
5
R is an open-source language from the 90s that was originally developed to teach statistics based on an older statistics language called S. Given its origin, its bent as a language is focused on efficient statistical analysis. SQL is a database language, focused more on storage and querying large datasets, and less on statistical analysis. Tidyverse is the most common extension for R, and introduces a grammar and syntax of data science with a standard philosophy. I use it a lot, and love the 'pipe' syntax which is basically the programming equivalent of the word 'and then do this'. It encourages programming run-on sentences. That makes a lot of sense to me as a data scientist, but is probably bad form for modular computer programmers.
🌐
DataCamp
datacamp.com › doc › r › operators
Operators in R
Just like in mathematics, operators in R have a hierarchy of precedence. For instance, multiplication and division are performed before addition and subtraction. Use parentheses to ensure the desired order of operations. ... While x+y and x + y are functionally identical, the latter is easier to read.
🌐
DataCamp
datacamp.com › doc › r › functions
Built-in Functions in R
Learn about common numeric and character functions in R for variable creation and recoding. Practice with the Intermediate R Course.
🌐
Quora
quora.com › What-does-mean-in-R-7
What does ~ mean in R? - Quora
Its meaning depends on context, but the core idea is “left-hand side explained by right-hand side.” ... Example: y ~ x1 + x2 means model y as a function of x1 and x2. ... In R, the tilde (~) is the formula operator: a compact, symbolic way to express relationships between variables that ...
🌐
ICPSR
icpsr.umich.edu › sites › hmca › posts › shared › what-is-r
What is R? How do I use it? | HMCA
For example, a graphical user interface (or “GUI”) allows the analyst to carry out data analysis tasks by selecting items from menus and lists, rather than entering commands. One such GUI is the R Commander, written by John Fox. The R Commander is accessed by installing and loading the Rcmdr package within R.
🌐
Wikipedia
en.wikipedia.org › wiki › R_(programming_language)
R (programming language) - Wikipedia
2 weeks ago - The core R language is extended by a large number of software packages, which contain reusable code, documentation, and sample data. Some of the most popular R packages are in the tidyverse collection, which enhances functionality for visualizing, transforming, and modelling data, as well as ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › which-function-in-r
The which() function in R programming | DigitalOcean
August 3, 2022 - Well, the which function has returned the positions of the value ‘0’ in the matrix. The first occurrence of “0” is in the second row first column. Then the next occurrence is in the first row, second column. Then we have 4th row, second column.
🌐
R Project
r-project.org
R: The R Project for Statistical Computing
R is a free software environment for statistical computing and graphics. It compiles and runs on a wide variety of UNIX platforms, Windows and MacOS. To download R, please choose your preferred CRAN mirror. If you have questions about R like how to download and install the software, or what ...
🌐
DataCamp
datacamp.com › blog › all-about-r
What is R? - An Introduction to The Statistical Computing Powerhouse | DataCamp
October 17, 2023 - R is a statistical programming tool that’s uniquely equipped to handle data, and lots of it. Wrangling mass amounts of information and producing publication-ready graphics and visualizations is easy with R.
🌐
Stack Overflow
stackoverflow.com › questions › 65849389 › what-does-i-do-in-r
differential equations - What does [,i] do in R? - Stack Overflow
In R, what does [,i] mean? And what is the difference between say [,i] and [i] in the context of this system of of equations?
🌐
Reddit
reddit.com › r/rlanguage › what is the %in% operator for?
r/Rlanguage on Reddit: What is the %in% operator for?
December 24, 2021 -

I've found tons of articles about this, but I'm just a beginner with no programming experience so they are all waaay over my head. Something about comparing vectors and dfs with other data types? I'm totally lost. Can somebody please dumb this way down for me?

In this case I'm using it with filter as part of the argument:

filter(site %in% c("Site1 name", "Site2 name"))

Is this just telling R that I'm using a vector as an argument? If so, why is that even necessary?

🌐
University of Washington
faculty.washington.edu › otoomet › info201-book › r-intro.html
Chapter 4 Introduction to R | Technical Foundations of Informatics
We have selected R because of its simplicity–as a language that is designed for such tasks from ground up, its tools are rather simple. This is also a reason why R is very popular in areas like health and social sciences–data processing in R is typically easier and requires less coding ...
🌐
Medium
lilian-hale98.medium.com › introduction-to-r-part-2-964432f87f73
Introduction to R (part 2). Basic data types in R | by Lilian Hale | Medium
July 31, 2020 - Relational operators are used to compare between values. Here is a list of relational operators available in R.