There are two great open-source languages for working with data, and this book has taught you one of them. The other is R, and it comes at the same problems from the opposite direction. Python is a general-purpose programming language that grew a superb data ecosystem on top. R is a language designed from the start for statistics and graphics, where a t-test, a linear model, and a polished chart are built-in verbs rather than imported tools. This chapter is a guided overview: what R is, how it compares to Python, how its libraries line up with the ones you know, and why none of the statistics you have learned has to be relearned to use it.
A mean is a mean, a p-value is a p-value, and a regression coefficient means the same thing in any language. Every idea in this book, from the describing-data chapters through hypothesis tests, regression, and machine learning, has a direct R implementation, and the statistical one is often shorter in R because the method is part of the language. Learning R is not learning statistics again. It is learning a second way to say what you already know.
What R Is, and Where It Came From
R began in the early 1990s when Ross Ihaka and Robert Gentleman at the University of Auckland built a free implementation of S, the statistical language John Chambers had created at Bell Labs. It became part of the GNU project, and today it is maintained by a global core team and a vast community. That heritage matters: R was written by statisticians for statistical work, and the design shows everywhere.
| Piece | What it is |
|---|---|
| The language | Vector-first and functional, with a y ~ x formula notation for models that reads like the statistics it expresses. |
| CRAN | The Comprehensive R Archive Network, a curated repository of around twenty thousand packages, one for almost every statistical method ever published. |
| RStudio / Posit | The dominant IDE, pairing a console, editor, plots, and help in one window. It is to R what Jupyter is to Python. |
| The tidyverse | A coherent family of modern packages (dplyr, ggplot2, tidyr, readr) that share a philosophy of tidy data and readable pipelines. |
Three things sit at R's core. Its everyday container is the vector, and like a NumPy array it is
vectorized, so operations apply to whole vectors at once. Its table is the data frame, the direct
ancestor of the pandas DataFrame. And its formula syntax, writing a model as
outcome ~ predictor, lets you state a regression or an ANOVA almost exactly as you would on paper. One
friendly warning for Python users: R counts from 1, not 0, so x[1] is the first element.
In R you do not install anything to run a t-test, fit a linear model, or draw from a normal distribution. The
functions t.test(), lm(), and rnorm() are part of the base language. Every
distribution follows one naming scheme: dnorm, pnorm, qnorm, and
rnorm give the density, cumulative probability, quantile, and random draws for the normal, and
binom, pois, t, chisq, and f follow the identical
pattern. That regularity is why statisticians find R so quick to think in.
R and Python: Two Roads to the Same Place
The most common question a new analyst asks is “which one should I learn?” The honest answer is that they overlap enormously and the best analysts read both. They differ most in the direction they were built from, and that origin still shapes where each one shines.
A fair summary: reach for R when the work is statistics-first, when you want the best exploratory
graphics, or when you are writing a reproducible statistical report; reach for Python when the work
heads toward machine learning, production systems, or general software. And you need not choose permanently. R's
reticulate package lets R call Python, tools like Quarto run both in one document, and many teams use each
where it is strongest. The rivalry is friendlier than the internet suggests.
The R Toolkit and Its Python Equivalents
Because you already know the Python stack from the previous chapter, the fastest way to learn R's is by translation. Almost every tool you have met has a close R counterpart, and often the correspondence is nearly one to one.
| Task | In R | The Python tool you know |
|---|---|---|
| Numeric arrays | base vectors and matrices, vectorized by default | NumPy arrays |
| Data tables | data.frame and the tidyverse tibble | pandas DataFrame |
| Wrangling | dplyr (filter, select, mutate, group_by, summarise) and tidyr | pandas (query, [], assign, groupby, agg, melt) |
| Reading files | readr (read_csv), readxl (read_excel) | pandas read_csv, read_excel |
| Dates & strings | lubridate, stringr | pandas .dt and .str accessors |
| Visualization | ggplot2, the grammar of graphics | Matplotlib + seaborn |
| Tests & models | base t.test, aov, lm, glm, chisq.test | scipy.stats + statsmodels |
| Machine learning | tidymodels / caret, plus glmnet, randomForest, xgboost | scikit-learn |
| Notebooks / reports | R Markdown and Quarto | Jupyter |
| Interactive apps | Shiny | Streamlit, Dash |
A word on ggplot2, R's signature contribution to data visualization. It implements the grammar of graphics: you build a chart by declaring a mapping from data columns to visual properties (position, color, size) and adding layers (points, lines, bars) on top. It is so influential that Python's own plotting libraries have borrowed from it, and for many statisticians it is the single best reason to open R.
The Same Analysis, in Both Languages
Nothing makes the transfer clearer than seeing it. Here is the exact workflow from the Python chapter, read a file, group by a column, summarize, and plot, written in R beside the pandas you already know. Read them side by side and notice how the steps are identical even where the words differ.
Read the data and peek at it.
# readxl + the tidyverse
library(tidyverse)
library(readxl)
orders <- read_excel("store-orders.xlsx")
head(orders)
summary(orders)# pandas
import pandas as pd
orders = pd.read_excel(
"store-orders.xlsx")
orders.head()
orders.describe()Group by region and total the revenue. This is the split-apply-combine you learned, in two dialects.
orders |> group_by(region) |> summarise(revenue = sum(revenue)) |> arrange(desc(revenue))
(orders
.groupby("region")
.revenue.sum()
.sort_values(ascending=False))R's pipe |> plays the exact role of pandas method chaining: it passes the result of one step into the
next, so both read top to bottom as a pipeline. Run either one on the store-orders file from the last chapter and both
report the same answer, North leading at 56,668 dollars, because the data and the arithmetic are the
same; only the language differs.
Fit a model, and draw a chart. Here R's statistical heritage shows: the model is a one-liner in the base language, and ggplot2 builds the plot in readable layers.
# a linear model, built in model <- lm(revenue ~ quantity, data = orders) summary(model) # grammar of graphics ggplot(orders, aes(quantity, revenue)) + geom_point() + geom_smooth(method = "lm")
# statsmodels import statsmodels.formula.api as smf model = smf.ols( "revenue ~ quantity", orders).fit() model.summary() # seaborn import seaborn as sns sns.regplot(data=orders, x="quantity", y="revenue")
Look at what just happened. The formula revenue ~ quantity is identical in both, because
statsmodels deliberately borrowed R's formula notation. The regression coefficients, the standard errors, the
R-squared, the p-values will match. Everything you learned about reading a regression table in the
regression chapters applies letter for letter. That is the whole point of
this chapter in a single example.
The mean is mean(x). A t-test is t.test(a, b). A confidence interval falls out of that
same call. ANOVA is aov(y ~ group), a chi-square test is chisq.test(tbl), logistic
regression is glm(y ~ x, family = binomial), and a correlation is cor(x, y). Every
technique in this handbook is a short R call away, and because R was built for statistics, the call is usually the
shortest path there is.
R in Statistics, Machine Learning, and AI
R's center of gravity is statistics, and in depth of statistical method it is second to none. Where a technique is recent or specialized, there is very often an R package for it years before anywhere else, written by the researchers who invented it.
| Area | What R brings |
|---|---|
| Classical statistics | Tests, lm, glm, and ANOVA are built in; the output is exactly the tables this book taught you to read. |
| Mixed & hierarchical models | lme4 and nlme are the reference implementations for the multilevel models of an earlier chapter. |
| Survival analysis | The survival package is the standard tool, Kaplan-Meier and Cox models included, across the whole field. |
| Time series | forecast and fable make ARIMA and exponential smoothing a line of code. |
| Bayesian methods | brms and rstanarm put full Bayesian regression, backed by Stan, within easy reach. |
| Machine learning | tidymodels and caret give a unified modeling interface much like scikit-learn, over engines such as glmnet, ranger, and xgboost. |
| Reporting | R Markdown, Quarto, and Shiny turn an analysis into a reproducible document or an interactive app. |
Where does Python pull ahead? Chiefly in deep learning and production AI. The frameworks that define modern neural networks, PyTorch and TensorFlow, are Python-first, and the engineering ecosystem for serving models at scale, the subject of the MLOps chapter, is largely Python too. R can reach these through interfaces, but this is Python's home turf. The practical reading of the whole landscape is simple: for statistical modeling, inference, and graphics, R is a first-class and often superior choice; for deep learning and shipping systems, lean Python. Many strong data scientists keep both within reach.
R's influence reaches well beyond its own users. The grammar of graphics
behind ggplot2, formalized by Leland Wilkinson and realized by Hadley Wickham, reshaped how the whole field thinks
about charts, and the tidy data principles from the same work, one variable per column, one
observation per row, are now the quiet standard for data everywhere, pandas included. And R's y ~ x
formula interface was adopted wholesale by Python's statsmodels. Ideas born in the statistics world flow outward; R
is where many of them were born.
π Key Takeaways
- βR is a statistics language first: built by statisticians, with tests, models, distributions, and graphics as part of the language rather than add-ons.
- βR and Python overlap enormously, and that overlap is the whole content of this book; R leans to statistics and graphics, Python to machine learning and production.
- βThe stacks map almost one to one: vectors to NumPy, data.frame to pandas, dplyr to pandas, ggplot2 to Matplotlib and seaborn, lm and glm to statsmodels, tidymodels to scikit-learn.
- βThe dplyr pipe and pandas chaining are the same idea, and the workflow, read, group, summarize, model, plot, is identical in either language.
- βStatistics you already know translates directly:
mean(),t.test(),lm(),glm(),aov(),chisq.test(), each a short R call. - βR leads in statistical depth (mixed models, survival, Bayesian, forecasting); Python leads in deep learning and production. Knowing both is a superpower.
- βNothing in this handbook has to be relearned for R: the concepts are language-independent, and only the syntax changes.
Quiz: Test Yourself
Eight questions on R, its comparison to Python, and how the two toolkits line up. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved.
R and Python are the open-source pair, but plenty of statistics is still done in dedicated commercial packages. Statistical Software tours SPSS, Stata, EViews, and Minitab, how to import data into them and, crucially, how to read the output they produce. Browse the full Contents for what is published and what is on the way.