CoursesR Basics

The Console Is a Calculator

Lesson 1 of 8 · 12 min

What the console is

When you open R, or RStudio, you see a pane with a > sign at the left. That sign is the prompt, and it means R is waiting for one instruction. You type an expression, press Enter, R evaluates it and prints the answer on the next line. Nothing is saved and nothing runs in the background; it is a conversation, one line at a time, and every script you will ever write is a list of such lines.

2 + 3
#> [1] 5
7 * 6
#> [1] 42
10 / 4
#> [1] 2.5
2^10
#> [1] 1024
3 + 4 * 2
#> [1] 11
(3 + 4) * 2
#> [1] 14
10 %% 3
#> [1] 1
sqrt(144)
#> [1] 12
round(10 / 3, 2)
#> [1] 3.33

Three things in that block deserve a second look. First, the [1] at the start of every answer. R has no notion of a single number: every value is a vector, a row of values, and [1] tells you the line begins with element number one. When an answer wraps onto a second line, that line starts with the index of its first element. Second, operator precedence works as in school mathematics: * binds tighter than +, so 3 + 4 * 2 is 11, and parentheses override that. Third, ^ is the power operator and %% gives the remainder of a division.

sqrt() and round() are your first two functions: a name, an opening bracket, the values the function needs, a closing bracket. round(10 / 3, 2) passes two values, the number and how many decimals to keep. Almost everything else in R has this shape.

Keeping a result: assignment

A calculator forgets each answer. R lets you give a value a name with <-, a less-than sign followed by a minus, read as gets. The name goes on the left, the value on the right. When you assign, R prints nothing; the value is stored silently. To see it, type the name on its own.

price <- 4.5
quantity <- 12
price * quantity
#> [1] 54
total <- price * quantity
total
#> [1] 54
total * 0.85
#> [1] 45.9

Names may contain letters, digits, dots and underscores, and must start with a letter. R is case-sensitive, so Total and total are two unrelated objects, and a name that is assigned again is overwritten without any warning. The value stored is a copy: changing price afterwards does not change total, because total was computed at the moment of assignment.

The trap: the prompt that turns into +

Type (2 + 3 without the closing bracket and press Enter. R does not report an error; instead the prompt changes from > to +. That means R considers the line unfinished, because a bracket, a quotation mark or a trailing operator is still open, and it is waiting for the rest. The usual beginner reaction is to type the next command, which R glues onto the unfinished one, producing a confusing message about an unexpected symbol several lines later. The fix is to press Escape, which abandons the incomplete line and brings back >, and then retype the expression with its bracket closed.

A first glimpse of data

Single numbers are not what statistics is about. The function c(), for combine, puts several values under one name, and a function such as mean() then works on all of them at once.

temps <- c(21.5, 23.1, 19.8, 24.6, 22.0)
temps
#> [1] 21.5 23.1 19.8 24.6 22.0
mean(temps)
#> [1] 22.2

That is the pattern for the whole course: data into a vector, vector into a function, read the result. With this lesson alone you can type a handful of measurements into the console and get their average.

💡 The Up arrow recalls the previous line; edit it and press Enter again instead of retyping.

Where this goes next

Vectors explains c(), seq(), rep() and length(), so you can build data without typing every number. Arithmetic on Whole Vectors shows how one operation applies to every element and what recycling does when lengths differ. Picking Elements with [ ] covers getting at the third value, the last value, or everything except one. Calling Functions and Reading Help explains named arguments such as na.rm = TRUE and how to read ?mean. Logical Vectors and Comparison turns questions like which days were above 22 degrees into code. The First Real Summary brings in median(), sd(), quantile(), table() and summary(). Reading Errors and Warnings teaches you to decode the messages R prints when something goes wrong.

Knowledge check
You type total <- 4.5 * 12 and press Enter. What does the console show?

Sign in to answer and track your progress.

Sign in