Your first R
What R is for, the two ways to print something, how the arrow stores a value, and the arithmetic every lesson after this one is built on.
A language built by people with data to look at
R grew up in the statistics world, where the job is to take a table of numbers, ask it questions, and show the answers. That origin shows up everywhere in it: a whole column of numbers is worked on in one step, a table of data is a first-class idea rather than an add-on, and the tools for describing and modelling data ship with the language itself.
The sandbox on this page runs the real R interpreter, compiled to WebAssembly and executing inside this page. The first Run downloads it, which takes a few seconds; every Run after that is immediate. There is no way to read input from a keyboard here, so lessons print their answers instead of asking for them.
Two ways to say something
cat writes the text you give it, exactly as you give it. print shows a value the way R would show it at its own prompt, which is why print(40 + 2) comes back as [1] 42 rather than as 42. That little prefix is not part of the number: it is R numbering the values it is showing, and it is the first thing every beginner asks about.
Most lessons here print with cat and paste0, because that gives back the text you typed with nothing added. Reach for print when you want to see what a value really is.
The arrow, and the equals sign
scores <- c(72, 88) stores a value under a name. The arrow is R's own assignment and it is the form R code is written in, so you will see it on nearly every line of every script you read.
The equals sign means something narrower in R: it names an argument inside a call, as in mean(x = scores). Both work in both places, but convention has kept them apart for decades, and following it makes your code read like the code around it.
The two prints, and the arrow
cat("Hello, R!\n")
# print shows the value itself, index prefix and all.
print(40 + 2)
# The arrow stores a value under a name.
name <- "Ada"
cat(paste0("Hello, ", name, "!\n"))
# The equals sign names an argument instead.
scores <- c(72, 88, 95)
cat(paste0("mean: ", mean(scores), "\n"))
cat(paste0("mean, named: ", mean(x = scores), "\n"))
# Arithmetic: plus, minus, star for times, slash to divide.
cat(paste0("2 to the 8th is ", 2 ^ 8, "\n"))Run it, then make it yours
Press Run and read each line. Then change the name, add a line that prints a sum of your own, and try mean() on a small vector you build with c().
The [1] in front of a printed value means "this is value one". When a vector is long enough that the line wraps, the next line starts with the number where it picked up, so you always know which element you are reading.
What the session remembers
R reads a script from top to bottom and keeps every name it meets in an environment. Run the file again and the names are built again in the same order, which is why a line near the top cannot use a value that is only created near the bottom. When a name is missing, R says object 'x' not found rather than guessing an empty value.
Names are case-sensitive: Total, total and TOTAL are three different objects, and Mean is not the function mean. A name may hold letters, digits, dots and underscores, but it cannot begin with a digit, and a name that begins with a dot is hidden from ls() by default.
A comment starts with # and runs to the end of the line. Comments are for the reason a line exists, not for repeating what the line says; the code already says what it does.
Every operator is a call
40 + 2 looks like arithmetic and is really a function call: R parses it as +(40, 2). The same is true of [ for indexing, $ for a column and even <- for assignment. That is why the help page is called Arithmetic, and why Reduce("+", values) can pass the plus sign around like any other function.
The disguise also explains precedence. 2 + 3 * 4 is 14 because * binds tighter than +; when the order matters to a reader, parentheses are cheaper than an explanation. And when a line fails, R stops at the first error rather than carrying on with a half-built script, so read the top of the message first.
Three habits prevent most first-day errors. R is case-sensitive, so Mean(x) is not mean(x). Text needs quotes, so cat(hello) looks for an object called hello instead of printing the word. And inside a call the equals sign names an argument — mean(x = scores) is right, while mean(x <- scores) also stores the vector as a side effect.
Try these next
- Work out 17 minutes in hours and minutes with
%/%and%%, then print the answer in a sentence. - Print the same value with
catand withprint, and say what the[1]prefix means. - Store a price and a quantity, compute the total, and print it with two decimal places using
sprintf("%.2f", total).
A small invoice, with the arithmetic checked
Run it and check the subtotal and the tax against the lines above them. Then change a quantity, predict the new total, and add a line for a third item.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
R printed [1] 42. What is the [1] part?
