📢 I've joined EPX IT as their AI & Digital Transformation Lead. New AI and automation projects now run through EPX. Get in touch and I'll point you the right way.

Learn R: an interactive beginner's guide

R is a free programming language built for working with data and statistics. This guide runs a real copy of R right here in your browser, so you can read a bit, change the code, and press Run to see what happens. No installing anything, no setup. Just you and the language.

How this page works. Every grey box below is a live R console. Edit the code however you like, then press Run. The first time you run anything, your browser quietly downloads R (about 30 MB), so give it 10–20 seconds on that first go. After that it's instant. Everything runs on your own machine; nothing is sent anywhere.

R as a calculator

The simplest thing R can do is arithmetic. Type a sum, press Run, and R prints the answer. The [1] at the start of the answer is just R labelling the first item in its result. Ignore it for now.

Try changing the numbers below, then press Run. Everything here is editable.

R

Storing values in variables

Doing sums is handy, but the real power comes from giving values a name so you can reuse them. In R you assign a value to a name with the arrow <- (a less-than sign followed by a minus sign). Read it as "gets".

So price <- 20 means "the name price gets the value 20". After that, you can use price anywhere you'd use the number 20.

R

Notice that assigning a value doesn't print anything. Writing the name total on its own line is what asks R to show it. Try adding a line that applies a 10% discount: total * 0.9.

Vectors: many values at once

This is the idea that makes R click. Most languages make you loop over a list of numbers one at a time. R lets you hold a whole set of values in one place, called a vector, and do maths on all of them together.

You build a vector with c(), which stands for "combine". Then any operation you do applies to every value at once.

R

You can pull out individual values using square brackets. scores[1] gives the first score, scores[c(1, 3)] gives the first and third. R counts from 1, not 0, which trips up people coming from other languages.

Types of data

R works with a few basic types of value. Numbers you've seen. There's also text (called character data, always in quotes) and true/false values (called logical). Understanding which is which saves a lot of confusion later.

R

Functions and getting help

A function is a named command that does a job. You call it by writing its name followed by round brackets, with any inputs inside. You've already met c() and class(). R comes with hundreds ready to go.

R

Stuck on what a function does? In real R you'd type ?mean to open its help page. You can also write your own functions once you're comfortable, but the built-in ones cover a huge amount.

Data frames: data in tables

Real data usually comes as a table: rows and columns, like a spreadsheet. In R that's a data frame. R ships with a few built-in ones so you can practise without loading a file. Here's mtcars, data on 32 cars from a 1974 magazine.

R

You can filter a data frame too. mtcars[mtcars$mpg > 25, ] gives just the rows where miles-per-gallon is above 25. The comma matters: it means "these rows, all columns".

Summary statistics

This is what R was built for. Once your data's in a vector or a column, describing it is a one-liner. summary() is a great first look at any set of numbers: it gives you the smallest, the largest, the average, and the middle.

R

Your first plot

R draws charts as easily as it does sums, and they'll appear right below the code. hist() draws a histogram (how often each range of values comes up). plot() draws points. Press Run and watch the picture appear.

R

Now a scatter plot. Does a heavier car use more fuel? Each dot is one car.

R

Try it yourself

No new teaching here, just a blank console. Everything from the lessons above is loaded and ready. A few things to try:

  • Make a vector of your own numbers and find its mean().
  • Draw a histogram of mtcars$hp (horsepower).
  • Generate 1,000 random numbers with rnorm(1000) and plot them.
R

Where to go next

When you're ready to work on your own machine instead of in a browser, here's the well-trodden path:

  • Install R from CRAN, the official home of the language. It's free.
  • Install RStudio (now made by Posit). It's the friendly workbench nearly everyone writes R in.
  • R for Data Science is the best free book for beginners, and it reads well cover to cover.
  • swirl teaches you R inside R itself, one nudge at a time.

The browser R on this page is powered by WebR, the real R language compiled to run in a web browser.

Fancy a tool like this for your own site?

Interactive guides, calculators, live demos. If it runs in a browser, it can live on your website. That's the kind of thing I build.

Tell me what you need