R Introduction
R is a programming language built for working with data. You give it numbers, tables and text, and it helps you summarise them, test ideas with statistics and draw clear charts. Researchers, analysts, doctors, economists and students use it every day.
In this lesson you will learn what R is, where it is used, how it compares with Python, and you will run your first R code without installing anything.
What is R?
R is a language and environment for statistical computing and graphics. "Language" means you write instructions in it, like any programming language. "Environment" means it also comes with a console to type commands, built-in data sets, a help system and tools for drawing charts.
A few facts about R:
- It was first written by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, in the 1990s.
- It is an implementation of the S language, which John Chambers and colleagues created at Bell Labs. Much old S code still runs in R.
- It is free and open source under the GNU General Public License, and runs on Windows, macOS and Linux.
- It is extended through packages: add-on bundles of functions. Thousands of them are published on CRAN, the Comprehensive R Archive Network.
- Today it is developed by the R Core Team. The current release is R 4.6.2, published on 29 October 2026.
Where is R used?
R is strongest wherever data meets statistics:
- Research and universities: theses, journal papers and survey analysis in economics, psychology, agriculture and public health.
- Medicine and biology: clinical trial analysis and genetics. The Bioconductor project offers many packages for biology data.
- Business analytics: sales reports, customer surveys, forecasting and A/B tests.
- Government and official statistics: survey data, census tables and reports.
- Data journalism: many news charts are drawn with R.
If you want to do statistics properly, R gives you almost every method in one place, often written by the statisticians who invented the method.
Your first R code
R works like a powerful calculator. You type an expression and R prints the result. Here Meera's marks in five subjects are stored in a vector, which is R's name for a list of values of the same type. Click "Try it" to run it, then change the marks.
# Meera's marks in five subjects
marks <- c(78, 85, 91, 66, 88)
mean(marks)
max(marks)
marks >= 80[1] 81.6 [1] 91 [1] FALSE TRUE TRUE FALSE TRUE
Here is what each line does:
- A line starting with
#is a comment. R ignores it; it is a note for humans. c()combines values into a vector, and<-stores that vector in a variable calledmarks.mean()andmax()are functions: named tools that take input and return a result.marks >= 80compares every mark with 80 at once and givesTRUEorFALSEfor each one. Working on a whole vector at once is called vectorisation, and it is the heart of R.
The [1] at the start of each output line is not part of the answer. It tells you that the first value on that line is element number 1. You will see why it matters in The Console & Scripts.
Data sets come built in
R ships with dozens of small practice data sets. mtcars holds data on 32 car models from a 1974 American magazine, iris holds flower measurements, and airquality holds daily air readings from New York in 1973. You will use them throughout this course because everyone has them.
# mtcars is a small data set that comes with R
head(mtcars, 4)
summary(mtcars$mpg)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 Min. 1st Qu. Median Mean 3rd Qu. Max. 10.40 15.43 19.20 20.09 22.80 33.90
head() shows the first rows of a table, and summary() gives the smallest value, the quartiles, the median, the mean and the largest value of the mpg (miles per gallon) column. One line of code gives a statistical summary; that is typical of R.
Your first chart
Charts are built into R too. This code draws a histogram, a bar chart that shows how often values fall into each range.
# Daily maximum temperature (degrees F) in New York, May to September 1973
hist(airquality$Temp,
main = "Daily temperature, New York 1973",
xlab = "Temperature (F)",
col = "lightblue")When you run it, a light blue histogram appears below the editor. The bars are tallest in the middle, around 75 to 85 degrees, and get shorter towards the very cool and very hot days. You will learn to draw and style charts in Base Graphics.
R and Python
Learners often ask whether to learn R or Python. Both are free and both are excellent for data work. The honest answer is that many analysts use both.
| R | Python | |
|---|---|---|
| Designed for | Statistics and data analysis | General-purpose programming |
| Statistics | Built in; huge range of methods | Through libraries such as SciPy and statsmodels |
| Charts | Base graphics and ggplot2 | matplotlib, seaborn and others |
| Tables of data | Data frames are built in | Through the pandas library |
| Popular editor | RStudio, Positron | VS Code, Jupyter, Positron |
If your work is mostly statistics, research or reports, R is a natural first choice. If you also want to build web apps or automation scripts, Python is broader. The ideas you learn here, such as vectors, data frames and vectorised code, carry over to both.
Common mistakes
- Typing the
[1]or the output into your code. The[1]is only a position marker printed by R. - Expecting R to be only a calculator or only a stats tool. It is a full programming language with variables, loops and functions.
- Mixing up capital letters. R is case-sensitive:
Mean(marks)gives an error because the function ismean.
Key points
- R is a free, open-source language and environment for statistics, data analysis and graphics.
- It grew out of the S language from Bell Labs and is extended with packages from CRAN.
- R works on whole vectors at once, so short code does a lot of work.
- R comes with practice data sets such as
mtcars,irisandairquality. - The current release is R 4.6.2; the browser examples use R 4.6.0 through webR.
Exercise
Open the first example. Replace Meera's marks with the marks of Kabir: 55, 72, 64, 90 and 47. Print his average mark, his lowest mark with min(), and check which marks are below 50.
Show solution
marks <- c(55, 72, 64, 90, 47)
mean(marks)
min(marks)
marks < 50[1] 65.6 [1] 47 [1] FALSE FALSE FALSE FALSE TRUE
mean() and min() summarise the whole vector, and marks < 50 compares each mark, so only the last one gives TRUE.
Quick Quiz
1. What was R mainly designed for?
R is a language and environment for statistical computing and graphics.
2. What does c(78, 85, 91) create?
c() combines values into a vector.
3. What does the [1] at the start of an output line mean?
[1] is a position marker, not part of the result.
4. Where are thousands of R packages published?
CRAN is the official network of sites that hosts R and its packages.
Learn R Programming with a mentor
Free lessons get you started. Our Data Science & Machine Learning training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
