What is R?

What R is, what people use it for, and how to install R and an IDE

A friendly introduction to R: what it is good at, how it compares with other tools, its ecosystem of packages, and how to install R, RStudio or Positron and your first packages.

Level 1 · Foundations ⏱ ~40 min Prerequisites: none

NoteLearning objectives

By the end of this lesson you will be able to:

  • explain in your own words what R is and what it is typically used for;
  • describe the main parts of the R ecosystem (CRAN, the tidyverse, Quarto, Shiny);
  • install R and an IDE (RStudio Desktop or Positron) on your own computer;
  • recognise the four panes of RStudio and what each one is for;
  • install and load a package with install.packages() and library().
# This lesson uses base R only: nothing to load.

1. Why learn a programming language for data?

Imagine you have a spreadsheet with measurements of 344 penguins: species, island, bill length, body mass. Your supervisor asks:

  • “What is the average body mass of each species?”
  • “Make me a plot comparing them.”
  • “Is the difference larger than what we would expect by chance?”
  • “Oh, and we just got new data from 2025 — can you redo everything?”

With point-and-click software you would repeat every step by hand, and in two months nobody (including you) would remember exactly which buttons were pressed. With a programming language you write the steps down once, as a script. Re-running the analysis on new data is then a single click, and anyone can read the script to see exactly what was done. This property is called reproducibility, and it is one of the main reasons scientists and analysts use code.

R is a language designed precisely for this kind of work. Here is the penguin question answered in R — you do not need to understand the code yet, just notice how short it is:

tapply(penguins$body_mass, penguins$species, mean, na.rm = TRUE)
#>    Adelie Chinstrap    Gentoo 
#>      3701      3733      5076

By the end of this course you will be able to write code like this (and much more) on your own.

2. What is R?

R is a free, open-source programming language and software environment for statistics, data analysis and graphics. It was created in the early 1990s by Ross Ihaka and Robert Gentleman at the University of Auckland, and it is now developed by the R Core Team and a very large community of contributors.

A few features make R distinctive:

  • It was built by statisticians, for data. Vectors, tables, missing values (NA) and statistical models are part of the language itself, not add-ons.
  • It is interactive. You type a command, R answers immediately. This makes exploring data fast.
  • It is extensible through packages. A package is a bundle of functions, data and documentation written by someone else that you can install with one line of code.
  • It is free. Free as in “no licence fee”, and free as in “you can read and modify the source code”.

Most R packages are written in R itself; parts that need to be very fast are often written in C, C++ or Fortran and called from R. You get the speed without having to write that code yourself.

What is R used for?

Area Typical tasks
Statistics descriptive statistics, hypothesis tests, regression, mixed models, survival analysis, Bayesian models
Data wrangling importing, cleaning, reshaping and joining data from files and databases
Visualisation publication-quality plots, maps, interactive graphics
Machine learning classification, regression, clustering, dimensionality reduction
Communication reports, articles, slides, websites and dashboards that mix code, text and results

A typical data analysis follows the cycle import → tidy → transform → visualise → model → communicate. R has well-developed tools for every step, and this course follows exactly that path.

3. R compared with other tools

There is no single “best” tool: each one is a trade-off between how quickly you can start and how far it can take you.

Tool Strengths Limitations for data analysis
Spreadsheets (Excel, LibreOffice, Google Sheets) Immediate, visual, everybody has one Hard to reproduce, error-prone with large data, limited statistics
Point-and-click statistics software (e.g. SPSS, JASP, jamovi) Gentle learning curve, menus for common tests Less flexible; analyses can be harder to automate and share
R Very wide range of statistical methods, strong graphics, reports with Quarto A programming language: there is a learning curve at the start
Python General-purpose language, strong in machine learning and software engineering Statistical modelling and reporting rely on several separate libraries
Commercial statistical languages (e.g. SAS, Stata) Mature, well documented, common in some industries Paid licences; smaller open ecosystem

R and Python are the two most widely used open-source languages for data science, and they cooperate rather than compete: you can call Python from R (with the reticulate package) and vice versa. The concepts you learn here — data frames, vectors, functions, models — transfer directly.

TipWhich language should I learn first?

The honest answer: the one used by the people around you. Learning statistical thinking and a clean data workflow matters far more than the specific language. Once you master one language, picking up the second is much easier.

Is R difficult?

R has a reputation for being quirky, and some of its older functions are indeed inconsistent. Two developments made it much friendlier:

  1. The tidyverse — a family of packages (dplyr, ggplot2, tidyr, readr, …) that share a consistent design, so that once you learn one, the others feel familiar.
  2. Modern IDEs (see Section 5) that offer auto-completion, inline help and error highlighting.

R also scales further than most beginners expect: it can talk to databases, to big-data engines such as Spark, use several CPU cores in parallel, and run deep-learning models through interfaces to Keras and Torch.

4. The R ecosystem

When people say “R” they often mean the language plus everything around it.

Component What it is
CRAN The Comprehensive R Archive Network: the official repository of R packages. It hosts more than twenty thousand packages, each checked automatically on several operating systems.
Bioconductor A repository of packages for bioinformatics and genomics.
The tidyverse A collection of packages for data science with a common grammar. You will use it from Lesson 9 onwards.
Quarto A publishing system that turns a document mixing text and code into HTML, PDF, Word, slides or a website. This course website is built with Quarto.
Shiny A package for building interactive web applications directly from R, without writing HTML or JavaScript.
webR R compiled to run inside a web browser. It powers the interactive exercises on this site.

You can try R right now, without installing anything: the box below runs real R in your browser.

5. Installing R and an IDE

You need to install two things, in this order:

  1. R itself — the engine that actually runs the code.
  2. An IDE (Integrated Development Environment) — the dashboard you use to write code, see results, plots and help. It is optional in principle, but in practice nobody works without one.

A useful analogy: R is the car engine; the IDE is the dashboard, steering wheel and seats.

Step 1 — install R

Go to the CRAN website, https://cran.r-project.org, and choose your operating system:

  • Windows: “Download R for Windows” → “base” → download the installer and run it, accepting the defaults.
  • macOS: “Download R for macOS” → pick the installer that matches your processor (Apple silicon, i.e. M-series chips, or Intel). Open the .pkg file and follow the prompts.
  • Linux: follow the instructions for your distribution (Ubuntu, Debian, Fedora, …) on the same page.

This course assumes R 4.5 or newer (the penguins dataset we use throughout was added to base R in version 4.5.0).

Step 2 — install an IDE

Choose one of the two free IDEs developed by Posit:

IDE Download Who it is for
RStudio Desktop https://posit.co/download/rstudio-desktop/ The classic, widely used IDE for R. Most tutorials and screenshots you find online use it. Recommended for beginners.
Positron https://positron.posit.co Posit’s newer IDE, built on the same open-source foundation as Visual Studio Code. It supports R and Python side by side.

Everything in this course works in both. When we describe menus we use RStudio’s names; Positron has equivalent commands.

NoteCheck your installation

Open your IDE and type R.version.string in the Console, then press Enter. You should see the version of R you just installed. If you see an older version, you probably have two R installations: uninstall the old one.

6. A tour of RStudio

When you open RStudio you see four panes. Their position can be changed (Tools → Global Options → Pane Layout), but by default they are:

Pane Position What it does
Source (editor) top-left Where you write and save scripts (.R) and Quarto documents (.qmd). Opens with File → New File.
Console bottom-left Where R actually runs. You can type commands here directly; results appear immediately. The > symbol is the prompt: R is waiting for you.
Environment / History top-right Environment lists the objects you have created (data, results). History lists the commands you have run.
Files / Plots / Packages / Help / Viewer bottom-right Browse files, see plots, manage installed packages, read help pages.

The most useful keyboard shortcut to learn on day one: Ctrl + Enter (Windows/Linux) or Cmd + Enter (macOS) sends the current line of your script to the Console and runs it.

TipA setting worth changing right now

In Tools → Global Options → General, untick “Restore .RData into workspace at startup” and set “Save workspace to .RData on exit” to Never. This forces you to keep everything important in scripts, which is the heart of reproducible work. We explain why in First steps.

7. Packages: installing and loading

Base R already contains thousands of functions, but sooner or later you will need a package. Using a package is a two-step process:

Step Function How often? Analogy
Install — download it from CRAN to your computer install.packages("name") once per computer (and after updating R) buying a book and putting it on your shelf
Load — make its functions available in the current session library(name) every time you start R taking the book off the shelf to read it
install.packages("ggplot2")   # quotes are required here
library(ggplot2)              # quotes are not needed here

This chunk is not run on the website (installing packages from a web page makes no sense), but you should run it on your own computer.

You can also install several packages at once:

install.packages(c("dplyr", "tidyr", "readr"))

To use a single function from a package without loading the whole package, write package::function(). For example, dplyr::glimpse(penguins). We use this style in the course when a package is needed only once.

The course installer

To save you time, the course provides a script that installs every package used in the lessons. Run this line once, in the Console:

source("https://raw.githubusercontent.com/gianluca-sottile/An-R-Tutorial-for-Beginners/master/scripts/install-packages.R")

source() runs an R script; here the script is read directly from the course’s GitHub repository. It may take a while the first time.

Where am I? sessionInfo()

When you ask for help (on a forum, or to your teacher), always say which version of R and of the packages you are using. One function prints all of it:

sessionInfo()
#> R version 4.6.1 (2026-06-24)
#> Platform: aarch64-apple-darwin23
#> Running under: macOS Tahoe 26.7
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
#> 
#> locale:
#> [1] it_IT.UTF-8/it_IT.UTF-8/it_IT.UTF-8/C/it_IT.UTF-8/it_IT.UTF-8
#> 
#> time zone: Europe/Rome
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.7.3        cli_3.6.6          knitr_1.52         rlang_1.3.0       
#>  [5] xfun_0.62          otel_0.2.0         generics_0.1.4     S7_0.2.2          
#>  [9] textshaping_1.0.5  jsonlite_2.0.0     glue_1.8.1         rprojroot_2.1.1   
#> [13] htmltools_0.5.9    ragg_1.5.2         scales_1.4.0       rmarkdown_2.32    
#> [17] grid_4.6.1         tibble_3.3.1       evaluate_1.0.5     fastmap_1.2.0     
#> [21] yaml_2.3.12        lifecycle_1.0.5    compiler_4.6.1     dplyr_1.2.1       
#> [25] RColorBrewer_1.1-3 pkgconfig_2.0.3    htmlwidgets_1.6.4  here_1.0.2        
#> [29] systemfonts_1.3.2  farver_2.1.2       digest_0.6.39      R6_2.6.1          
#> [33] tidyselect_1.2.1   pillar_1.11.1      magrittr_2.0.5     tools_4.6.1       
#> [37] gtable_0.3.6       ggplot2_4.0.3
TipHow to read this output
  • The first line is your R version (here R version 4.6.1 (2026-06-24)).
  • Platform and Running under describe your computer and operating system.
  • attached base packages are loaded automatically every time R starts (stats, graphics, utils, datasets, …). The datasets package is the reason why penguins, mtcars and iris are always available.
  • other attached packages (if any) are those you loaded with library(), with their version numbers.
WarningCommon pitfalls
  1. Installing only an IDE. RStudio and Positron need R to be installed separately. Install R first.
  2. Forgetting the quotes in install.packages(). install.packages(dplyr) fails because R looks for an object called dplyr. Use install.packages("dplyr").
  3. Putting install.packages() inside a script. Every time the script runs it downloads the package again. Install once from the Console; keep only library() in your scripts.
  4. “there is no package called …” when calling library(). It means the package is not installed (or the name is misspelled — R is case-sensitive: library(ggplot2), not library(GGplot2)).
  5. Saving the workspace (.RData) on exit. Old objects silently reappear in your next session and hide mistakes. Change the setting described in Section 6.

8. Check your understanding

R is the language and the engine that executes the code. RStudio (like Positron) is an IDE: a program that makes it comfortable to write R code, see results, plots and help. You can use R without RStudio, but not RStudio without R.

To load the package with library(dplyr). Installation happens once; loading must be done in every new session.

Because a script is a precise, re-runnable record of every step. You (or anyone else) can re-run the whole analysis on new data, check it for errors and see exactly how each result was obtained. This is reproducibility.

9. Exercises

Interactive (run in your browser)

★ Exercise 1 — R as a calculator. Use R to compute: (a) the number of seconds in a day; (b) the square root of 2; (c) the average of 12, 15 and 21. Replace each ______.

A day has 24 hours. For (c), divide the sum by how many numbers you added.

24 * 60 * 60
sqrt(2)
(12 + 15 + 21) / 3

A day has 86,400 seconds; \(\sqrt 2 \approx 1.414\); the average is 16. You could also write mean(c(12, 15, 21)).

★ Exercise 2 — which R am I running? Print the version of R that runs in your browser, and then the full session information.

The first is a variable called R.version.string. The second is the function described in Section 7.

R.version.string
sessionInfo()

The browser runs webR, an R build compiled for the web: the Platform line therefore mentions emscripten/wasm instead of Windows or macOS.

★★ Exercise 3 — your first look at data. The penguins dataset is always available in R ≥ 4.5. Use nrow() to count the penguins, head() to see the first rows and summary() to get an overview of every column.

All three functions take the dataset itself as their argument: penguins.

nrow(penguins)
head(penguins)
summary(penguins)

There are 344 penguins. In the summary() output, notice the NA's lines: some measurements are missing. You will learn how to deal with them in Missing data.

Practice on your own computer

★ Exercise 4 — set up your machine. (a) Install R and RStudio Desktop (or Positron). (b) In the Console, run R.version.string and check that the version is 4.5 or newer. (c) Install the ggplot2 package and load it. (d) Change the workspace settings described in Section 6.

R.version.string            # e.g. "R version 4.6.0 (...)"
install.packages("ggplot2")
library(ggplot2)            # no output means success

If library(ggplot2) prints nothing, it worked: in R, silence usually means success. Messages in red are not always errors — read them: lines starting with Error are errors, others are often just information.

★★ Exercise 5 — the course packages. Run the course installer from Section 7. When it finishes, run library(dplyr) and library(ggplot2) and then sessionInfo(). Find the two packages and their version numbers in the output.

source("https://raw.githubusercontent.com/gianluca-sottile/An-R-Tutorial-for-Beginners/master/scripts/install-packages.R")
library(dplyr)
library(ggplot2)
sessionInfo()

They appear under other attached packages, e.g. ggplot2_4.x.x and dplyr_1.x.x. If the installer reports that a package failed, try installing that one alone with install.packages() and read the error message carefully.

Summary

Idea / function What it means
R Free, open-source language for statistics, data analysis and graphics
RStudio Desktop, Positron IDEs: the environment where you write and run R code
CRAN Official repository of R packages
install.packages("pkg") Download and install a package (once)
library(pkg) Load an installed package (every session)
pkg::fun() Use one function without loading the package
sessionInfo(), R.version.string Which version of R and packages you are running
  • R is a language built for data: scripts make your analysis reproducible.
  • Install R first, then an IDE (RStudio Desktop or Positron).
  • Packages are installed once and loaded every session.
  • R cooperates with other tools; the skills you learn here transfer.
Back to top