R (4.6) Module 1 – Start here: R, the console and a working setup
What R is and why researchers use it
What R is: a language and environment for statistics, its packages from base R to CRAN and Bioconductor, when to choose it, its versions and how to cite it.
What you will learn
- Describe what base R, the recommended packages, CRAN and Bioconductor each provide
- Choose between R, Python, a spreadsheet and SQL for a step of a research project
- Read an R version number and compare versions with getRversion()
- Cite R in a paper with citation(), including its DOI
What you’ll learn
What you will learn
- Describe what base R, the recommended packages, CRAN and Bioconductor each provide
- Choose between R, Python, a spreadsheet and SQL for a step of a research project
- Read an R version number and compare versions with getRversion()
- Cite R in a paper with citation(), including its DOI
On this page
Eight volunteers took a reaction-time test twice, once after a normal night’s sleep and once after a short night.
Did the short night slow them down, and by how much? In R the answer takes two lines, mean() for the average
slowdown and t.test() for the test, and it comes with a confidence interval and a p-value.
R is a programming language made for questions like this, with an environment around it of packages, help pages and graphics. Below you will see what an R installation holds and where the rest of its packages come from, when R is the right tool and when another tool fits better, how to read the version you are running, and how to cite R when your numbers go into a paper.
R answers a statistical question in a few lines
Here are the eight volunteers in R. c() combines the numbers into a vector, one object that holds all eight, <-
gives it a name, and the last two lines ask the questions. Lines that start with # are comments, which R skips:
# Reaction times in milliseconds of eight volunteers, measured once after a
# normal night's sleep and once after a short night (a small made-up sample).
rested <- c(251, 238, 276, 262, 245, 289, 270, 255)
short_night <- c(268, 249, 301, 271, 262, 310, 284, 269)
# How much slower each volunteer was, on average, and a paired t-test.
mean(short_night - rested)
t.test(short_night, rested, paired = TRUE) R ran the file and printed two answers:
from session import run, version
version()
run("""
Rscript first_look.R
""") Output
# R version 4.6.1 -- "Happy Hop"
$ Rscript first_look.R
[1] 16
Paired t-test
data: short_night and rested
t = 8.6863, df = 7, p-value = 5.37e-05
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
11.64443 20.35557
sample estimates:
mean difference
16
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 first_look_session.py
The first line, [1] 16, is the average slowdown: after the short night the volunteers were 16 milliseconds slower,
on average. Below it, one call to t.test() reports a whole analysis: the test statistic t = 8.6863 with 7 degrees
of freedom, a p-value of 0.0000537 (printed as 5.37e-05), and a 95 percent confidence interval for the slowdown from
about 11.6 to 20.4 milliseconds. paired = TRUE makes the test work on each volunteer’s own difference, which is what
two measurements of the same people call for.
That is the work R is made for. The R Project describes R as “a language and environment for statistical computing and graphics”: a programming language, plus a coherent system around it in which statistical methods come ready to use. Tests like this one, linear and generalised linear models, probability distributions and plots are part of every installation, so an analysis is a short script rather than a program written from scratch.
R started as an implementation of S, a language for data analysis that John Chambers and colleagues developed at Bell Laboratories; its first authors were Ross Ihaka and Robert Gentleman at the University of Auckland, and today the R Core Team develops it. Its licence, the GNU GPL in version 2 or 3, lets anyone use, study, change and share R at no cost, and it runs on Windows, macOS, Linux and other Unix-like systems.
Taking an editor for R
RStudio, Positron and VS Code are editors: they send your code to R and show what it prints. Installing one of them does not install R, and R works without any of them. The next lesson installs R first, then an editor.
Checkpoint
Check: What R is, and a first analysis
Packages: some come with R, the rest from CRAN or Bioconductor
mean() and t.test() worked without any setup because they live in package: A bundle of R functions, data and help pages. It is installed once into a package library and attached to a session with library(). that R
attaches when it starts. A package is a bundle of functions, data and help pages. Two groups come with an R
installation:
- The 14 base package: One of the 14 packages that are part of R itself, such as base, stats, utils and graphics. They carry R's own version number. are part of R:
base,stats(wheret.test()lives),graphics,utilsand ten more. - The 15 recommended package: One of the 15 packages, such as MASS, Matrix, survival and mgcv, that every binary distribution of R installs alongside the base packages., among them
MASS,Matrix,nlme,survivalandmgcv, are, in the words of the R FAQ, “to be included in all binary distributions of R”, so R’s installers put them on your computer too.
R lists both groups itself. The last line of this file shows which packages are attached when a session starts:
# The packages that are part of R, and the recommended ones that every binary
# distribution of R adds.
pkgs <- tools::standard_package_names()
lengths(pkgs)
pkgs$base
pkgs$recommended
# A base package carries the version of R itself.
packageVersion("stats")
# The packages attached when R starts: their functions work without library().
search() from session import run, version
version()
run("""
Rscript packages.R
""") Output
# R version 4.6.1 -- "Happy Hop"
$ Rscript packages.R
base recommended
14 15
[1] "base" "tools" "utils" "grDevices" "graphics" "stats"
[7] "datasets" "methods" "grid" "splines" "stats4" "tcltk"
[13] "compiler" "parallel"
[1] "MASS" "lattice" "Matrix" "nlme" "survival"
[6] "boot" "cluster" "codetools" "foreign" "KernSmooth"
[11] "rpart" "class" "nnet" "spatial" "mgcv"
[1] ‘4.6.1’
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 packages_session.py
packageVersion("stats") prints 4.6.1: a base package carries the version of R itself. search() shows stats,
graphics, grDevices, utils, datasets, methods and base: their functions work by name at once. An installed
package that is not on that list, such as the recommended survival, has to be attached with library(survival)
before its functions work by name.
Everything else comes from two archives. CRAN: The Comprehensive R Archive Network: a set of mirrored sites that carry R itself and the contributed packages that install.packages() installs., the Comprehensive R Archive Network, is a set of
mirrored sites that carry R and more than 25,000 contributed packages; install.packages("name") copies one onto
your computer. Its 50 task view: A CRAN page that lists and describes the packages for one topic, such as Bayesian statistics, econometrics or survival analysis. sort them by topic, from Bayesian statistics and
clinical trials to econometrics, mixed models, survival analysis and time series. Bioconductor: An archive of R packages for genomics and biology, with one release for each R version. Its packages are installed with BiocManager::install().
is the archive for genomics and biology: its release 3.23, made for R 4.6, has 2,418 software packages, installed with
the CRAN package BiocManager through BiocManager::install("name"). Follow a package from an archive to your session:
Where the packages of an R session come from
Text description of the diagram
The figure has two archives at the top, the package library on your computer in the middle and your R session at the bottom.
- CRAN holds more than 25,000 contributed packages. install.packages() copies one into your package library.
- Bioconductor 3.23, a release made for R 4.6, holds 2,418 software packages for biology. BiocManager::install() copies one into your package library.
- The package library already holds the 14 base packages and, with R's installers, the 15 recommended packages. The rest are the ones you installed.
- library(name) loads a package from the library into your R session. stats, graphics, utils and a few others are attached as soon as R starts, so their functions work at once.
Key idea
R’s core is small and its reach is wide: 14 base packages in every R, 15 recommended ones in every installer, and
the rest from CRAN or Bioconductor, installed once and attached with library() in each session that uses them.
Checkpoint
Check: Base, recommended and contributed packages
Where R is the usual choice, and where another tool fits better
Four steps of one research project show why the answer is not always R: fitting a mixed model to repeated measurements of 120 patients; summing a year of transactions that live in a database; showing a model’s predictions inside a web app; fixing three typing errors in a 30-row table of field sites.
- R fits the first. Its statistical methods are in base R and on CRAN, where task views list the packages for mixed models, survival analysis, econometrics and clinical trials, and Bioconductor does the same for genomics.
- SQL fits the second. The database filters, joins and sums the rows where they live, even when the table is larger than a computer’s memory; R or Python then read the small result.
- Python fits the third. Python.org describes it as a language that lets you “work quickly and integrate systems
more effectively”, and its standard
statisticsmodule says it is “not intended to be a competitor” to third-party libraries, so statistical models come from add-on libraries. - A spreadsheet fits the fourth: a small table you can see and fix by hand. Once the same steps must run again on new data, a script is safer, because clicks leave no record of what was done.
Try it on a step of your own work. Answer the two questions, and the table marks the tools that fit, with a reason in every cell:
Answer the questions above: the table then marks what fits your case and says why the rest does not.
| Tool | Statistical models | Rerun on new data | Tables in a database | Part of other software |
|---|---|---|---|---|
| R | Good tests, linear and generalised models in base R; task views list specialised ones | Good a script reruns every step, from the raw data to the figures | Mixed reads what a query returns; the result must fit in memory | Mixed can serve results as an app or an API, but is rarely the core of one |
| Python | Mixed basic statistics built in; models come from add-on libraries | Good a script reruns every step, from the raw data to the figures | Mixed reads what a query returns; the result must fit in memory | Good a general-purpose language, made to work with other systems |
| A spreadsheet | Mixed functions for common statistics; specialised models need other tools | Poor the steps are clicks and edits, so a rerun is done again by hand | Poor made for a table you can see and edit, not for large tables | Poor a file people open, not code other programs call |
| SQL in a database | Poor counts, sums, averages and joins; models are fitted elsewhere | Good a saved query reruns on whatever the tables hold now | Good runs where the data lives, on tables larger than memory | Mixed apps send it queries, but the app is written in another language |
How the answers decide
- “What is the main job of this step?” Fit or test a statistical model: rules out A spreadsheet and SQL in a database and adds a caveat to Python. Fitting and testing models is the work R was built for, with the methods in base R and on CRAN. Python does it with add-on libraries.
- “What is the main job of this step?” Summarise rows that live in a database: rules out A spreadsheet and adds a caveat to R and Python. Let the database filter, join and sum the rows where they live. R or Python then read the much smaller result.
- “What is the main job of this step?” Feed results into an app or a service: rules out A spreadsheet and adds a caveat to R. Code that must live inside other software suits a general-purpose language. R can still serve its results through an app or an API.
- “What is the main job of this step?” Look at a small table and fix it by hand: adds a caveat to R, Python and SQL in a database. For one look at a small table, a spreadsheet is quickest. Write the steps as a script as soon as they must be repeated.
- “Will the same steps run again, on new or corrected data?” Yes, again and again: rules out A spreadsheet. Steps done by clicking in cells are hard to repeat exactly. A script or a saved query runs the same steps on new data.
In short: R for models, tests and research graphics; SQL for large tables in a database; Python for analysis that must live inside other software; a spreadsheet for a quick look by hand. Many projects use two of them, such as SQL to cut a large table down and R to model what is left.
Checkpoint
Check: When R fits, and when another tool does
Versions: what the three numbers mean and why they matter
Every recorded run on these pages starts with the same line, R version 4.6.1 -- "Happy Hop". The three numbers are
the major version 4, the minor version 6 and the patch level 1, and “Happy Hop” is the release’s nickname. R has one
x.y.0 release a year, in spring, and patch releases such as 4.6.1 in between when they are needed. Inside R,
getRversion() gives the version, and the list R.version holds its parts:
# Which R is running, and how R itself describes the version.
getRversion()
R.version$major
R.version$minor
R.version$nickname
# Compare versions as versions: as text, "4.10.0" sorts before "4.6.0".
getRversion() >= "4.6.0"
"4.10.0" > "4.6.0"
numeric_version("4.10.0") > numeric_version("4.6.0") from session import run, version
version()
run("""
Rscript version.R
""") Output
# R version 4.6.1 -- "Happy Hop" $ Rscript version.R [1] ‘4.6.1’ [1] "4" [1] "6.1" [1] "Happy Hop" [1] TRUE [1] FALSE [1] TRUE
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 version_session.py
R.version$minor is "6.1", the minor version and the patch level together, and like every part of R.version it
is text. getRversion() returns a version object, so getRversion() >= "4.6.0" compares the numbers one part at a
time and prints TRUE. Comparing the text instead goes wrong as soon as a part has two digits: the run shows that, as
text, "4.10.0" > "4.6.0" is FALSE, because the character 1 sorts before 6.
Comparing version numbers as text
The wrong version is R.version.string >= "R version 4.1.0": two pieces of text, compared character by character,
so a later 4.10 would sort before 4.6. The right one is getRversion() >= "4.1.0", or numeric_version() on
both sides, which compares the numbers part by part: the run’s last line prints TRUE for 4.10.0 against 4.6.0.
The version matters because each release changes something a script can depend on. Walk through the releases of R 4:
- 4.0.0
Text columns stay text new default
data.frame() and read.table() no longer turn text into factors, because stringsAsFactors now defaults to FALSE. Raw strings such as r"(C:\data)" arrive too. A script written for R 3 can build factors where R 4 keeps text.
- 4.1.0
The native pipe and short functions new syntax
x |> f() passes x to f(), and \(x) x + 1 is short for function(x) x + 1. A script that uses either stops with a syntax error on R 4.0.
- 4.2.0
Conditions of one value, and UTF-8 on Windows breaking change
if() and while() with a condition of more than one value stop with an error instead of a warning. On recent Windows systems R uses UTF-8 as its native encoding, and 32-bit Windows builds are dropped.
- 4.3.0
&& and || take single values only breaking change
Calling && or || with a side of more than one value is now always an error. Use & and | for element-by-element logic, or any() and all() to reduce a vector to one value.
- 4.4.0
%||% joins base R new operator
L %||% R gives L unless L is NULL, and R otherwise, with no package needed. is.atomic(NULL) now returns FALSE.
- 4.5.0
grepv() and the penguins data new functions
grepv() is grep() that returns the matching values themselves, and the datasets package gains penguins and penguins_raw to practise on.
- 4.6.0
A DOI for R, and literal file patterns new
The R citation now carries a DOI, 10.32614/R.manuals, and list.files() gains fixed = TRUE, which matches a pattern as plain text instead of a regular expression.
- 4.6.1
A patch release of 4.6
Patch releases come between the yearly ones when they are needed, and 4.6.1 is mostly bug fixes, so scripts written for 4.6.0 keep working. Every run in this track was made with 4.6.1.
The timeline has 8 events, from 4.0.0 to 4.6.1.
A script that uses |> needs R 4.1.0 or later, and one written for R 3 can build factors where R 4 keeps text. That is
why a script states the oldest R it needs, and a methods section names the version that produced its numbers.
Checkpoint
Check: Reading and comparing R versions
Citing R: citation() writes the reference for you
A paper whose numbers came from R says so, with the version. You do not have to write the reference yourself:
citation() prints the one the R Core Team asks for, ready to paste, and a BibTeX entry for LaTeX users:
# How to cite R: the reference and a BibTeX entry, then the DOI on its own.
citation()
citation()$doi from session import run, version
version()
run("""
Rscript cite.R
""") Output
# R version 4.6.1 -- "Happy Hop"
$ Rscript cite.R
To cite R in publications use:
R Core Team (2026). _R: A Language and Environment for Statistical
Computing_. R Foundation for Statistical Computing, Vienna, Austria.
doi:10.32614/R.manuals <https://doi.org/10.32614/R.manuals>.
<https://www.R-project.org/>.
A BibTeX entry for LaTeX users is
@Manual{,
title = {R: A Language and Environment for Statistical Computing},
author = {{R Core Team}},
organization = {R Foundation for Statistical Computing},
address = {Vienna, Austria},
year = {2026},
doi = {10.32614/R.manuals},
url = {https://www.R-project.org/},
}
We have invested a lot of time and effort in creating R, please cite it
when using it for data analysis. See also ‘citation("pkgname")’ for
citing R packages.
[1] "10.32614/R.manuals"
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 cite_session.py
Since R 4.6.0 the reference carries a DOI, 10.32614/R.manuals, and citation()$doi returns it on its own, as the last
line shows. The year in the reference is read from R.version$year, so it is always the release year of the R you
run.
Packages are cited the same way. citation("name") prints the reference a package’s authors ask for, taken from its
CITATION file or, when it has none, built from its DESCRIPTION file. A base package points back to R: citation("stats")
says that stats is part of R and prints the R reference above. A methods section names R with its version, then each
package that did real work, with its own version from packageVersion("name").
Checkpoint
Check: Citing R and its packages
Practise: what R is and where it fits
Once R is installed, in the next lesson, run getRversion() and citation() on your own computer and compare what
they print with the runs above: the version may differ, and the reference follows it.
The session below asks up to twelve of this lesson’s fifteen questions, one at a time, on what R is, its packages, choosing a tool, versions and citations. Where you slip, it stays with that idea; where you are right, it asks something harder:
Practice that adapts to you
Practise this lesson
15 questions about 5 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.
Interview questions
Warm-up: what is the difference between a base package, a recommended package and a contributed package?
A base package is part of R itself: the 14 of them, such as base, stats, utils and graphics, come with every
R and carry R’s own version number, so they change only with a new R release. A recommended package is one of 15,
such as MASS, Matrix, survival and mgcv, that R’s binary distributions install alongside the base packages;
they are also published on CRAN, where they get updates between R releases. A contributed package is everything else:
one of more than 25,000 packages on CRAN, or a Bioconductor package, which you install yourself with
install.packages() or BiocManager::install(). In practice the difference decides what a colleague with a fresh R
must install before your script runs: nothing for stats, nothing for survival if their R came from an official
installer, and every contributed package your script loads.
Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.
-
Fresher to mid
Your analysis script runs on your laptop. On a colleague's computer library(lme4) fails, and once that is fixed, the same model gives slightly different estimates. What do you compare between the two setups, and what do you report in a paper so that others can repeat the result?
-
Mid to senior
A research group analyses its survey data in spreadsheets, and each monthly report takes two days of copying and clicking. Make the case for or against moving the analysis to R, and say how you would switch without interrupting the monthly reports.
Key takeaways
- R is “a language and environment for statistical computing and graphics”: a paired t-test of eight volunteers is one call, and it prints the test statistic, the p-value and a confidence interval.
- Fourteen base packages are part of R and fifteen recommended ones come with every installer; the rest come from CRAN, with more than 25,000 packages, or from Bioconductor, whose release 3.23, made for R 4.6, has 2,418 software packages for biology.
- R fits models, tests and research graphics; SQL suits large tables in a database, Python suits analysis inside other software, and a spreadsheet suits a quick look by hand.
- R 4.6.1 is major version 4, minor version 6, patch level 1; there is one x.y.0 release a year, and versions are
compared with
getRversion(), never as text. citation()prints the reference for R with its DOI, 10.32614/R.manuals, andcitation("name")the reference for a package.
Terms in this lesson
- base package
- One of the 14 packages that are part of R itself, such as base, stats, utils and graphics. They carry R's own version number.
- Bioconductor
- An archive of R packages for genomics and biology, with one release for each R version. Its packages are installed with BiocManager::install().
- CRAN
- The Comprehensive R Archive Network: a set of mirrored sites that carry R itself and the contributed packages that install.packages() installs.
- package
- A bundle of R functions, data and help pages. It is installed once into a package library and attached to a session with library().
- recommended package
- One of the 15 packages, such as MASS, Matrix, survival and mgcv, that every binary distribution of R installs alongside the base packages.
- task view
- A CRAN page that lists and describes the packages for one topic, such as Bayesian statistics, econometrics or survival analysis.
References
- What is R? (The R Foundation)
- R FAQ (sections 2 and 5) (The R Project (CRAN))
- R developer page: release plans (The R Core Team)
- R NEWS (changes in R 4.0.0 to 4.6.1) (The R Core Team)
- Citing R and R Packages in Publications (citation) (The R Core Team)
- Student's t-Test (t.test) (The R Core Team)
- Test Installed Packages (standard_package_names) (The R Core Team)
- Numeric Versions (numeric_version, getRversion) (The R Core Team)
- Available CRAN packages (The R Project (CRAN))
- CRAN Task Views (The R Project (CRAN))
- Bioconductor 3.23 release announcement (Bioconductor)
- Install Bioconductor packages (Bioconductor)
- survival: Survival Analysis (CRAN page, Priority: recommended) (The R Project (CRAN))
- statistics: mathematical statistics functions (Python Software Foundation)
- Welcome to Python.org (Python Software Foundation)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress