TExcelTAD
Home › R › R Intro

R Intro

R is a programming language built for working with data. Statisticians created it in the 1990s as a free alternative to the S language, and today it is used by data analysts, scientists, economists and researchers to clean data, run statistical tests, fit models and draw charts.

Where many languages treat numbers one at a time, R treats a vector of values as the basic unit. Adding one to a whole column of numbers, finding its average or drawing its histogram are all one-liners.

r · try it yourself
temperatures <- c(18.5, 21.0, 19.2, 23.8, 22.1)
temperatures + 1
mean(temperatures)
summary(temperatures)
[1] 19.5 22.0 20.2 24.8 23.1
[1] 20.92
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  18.50   19.20   21.00   20.92   22.10   23.80

Notice that there is no print() in that example. When a line produces a value and you don't store it, R prints it automatically. The [1] at the start of each line is the position of the first value shown on that line; it becomes useful when a long vector wraps over several lines.

What is R used for?

Area Typical tasks
Statistics descriptive statistics, hypothesis tests, regression, ANOVA
Data analysis importing, cleaning, reshaping and summarising tables
Visualisation scatter plots, histograms, box plots, publication-quality charts
Research reproducible reports, bioinformatics, epidemiology, finance
Machine learning classification, clustering, forecasting

Why learn R?

  • It speaks statistics natively. Means, medians, standard deviations, t-tests and linear models are built in, with no imports.
  • Data frames are part of the language. A table of rows and columns is a first-class object.
  • Plots are one function call away. plot(x, y) already gives a useful chart.
  • It ships with practice data. Datasets such as mtcars, iris and airquality are always available.
  • It is free and open source, with thousands of add-on packages on CRAN.

Here is a taste: the built-in mtcars table describes 32 cars from a 1974 magazine. In two lines we look at the first rows and compute the average fuel economy for each number of cylinders.

r · try it yourself
head(mtcars, 4)
tapply(mtcars$mpg, mtcars$cyl, mean)
                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4      21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag  21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710     22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
       4        6        8
26.66364 19.74286 15.10000

How R runs on this site

Every ▶ Run button in this course runs real R (version 4.6) compiled to WebAssembly with webR, entirely inside your browser. Nothing is sent to a server. The playground behaves like running a script with Rscript:

  • values on a line of their own are printed automatically,
  • message() and cat() output appears where it happens,
  • warnings are shown after the statement that raised them,
  • the first error stops the program,
  • charts drawn with base graphics appear as images under the text output.

The first run takes a few seconds while R downloads; after that it is fast.

Note: Only "base R" is available here: the packages that come with every R installation (base, stats, graphics, utils, datasets, …). Popular add-ons like ggplot2 and dplyr are introduced in the Tidyverse lesson as code you can run on your own computer.

Your first chart

Because graphics are built in, a chart takes a single call. The text output below lists the values; the bar chart appears under it when you run the example.

r · try it yourself
sales <- c(Mon = 12, Tue = 15, Wed = 9, Thu = 20, Fri = 17)
barplot(sales, col = "steelblue", main = "Coffees sold")
sales
Mon Tue Wed Thu Fri
 12  15   9  20  17

Test yourself

What does R print for a line that only contains mean(c(2, 4, 6))?

  1. Nothing, you need print()
  2. [1] 4
  3. 4.0
  4. mean = 4

What is the basic unit of data in R?

  1. The single number
  2. The vector
  3. The class
  4. The pointer

Which of these is a built-in dataset you can use without loading anything?

  1. ggplot2
  2. mtcars
  3. dplyr
  4. CRAN