Vectors Everywhere
The single most important fact about R — there are no "scalars," only vectors. Once this clicks, the entire language makes sense.
If R has a "secret," this is it: everything is a vector. When
you write x <- 5, R does not store a single number — it stores
a vector of length one that happens to contain the number 5. There
are no scalars hiding under the surface. The vector is the
fundamental unit of data in R.
This is why R is so good at data work. Most data analysis is, at its core, "do something to every value in a column." R was built around that operation from day one.
Creating vectors with c()
The function c() (for combine) is how you build a vector by
hand:
The output starts with [1]. That [1] is the index of the first
element shown on this line. If the vector were long enough to
wrap across lines, you would see [1], then [8], then [15],
and so on — R is helpfully telling you where you are in the
vector.
Length-one vectors
The "scalar" you thought you were creating is just a vector of length 1:
There is no x that is "the number 42." There is only x, which
is a vector of length one whose first element is 42.
This is not a curiosity — it's the foundation of everything that
follows. When you learn mean(x), it works on length 1, length
100, or length 10 million. The same word, the same idea, every
time.
Sequences: : and seq()
Building vectors by typing every number would be miserable. R has shortcuts.
: builds an integer sequence. seq() is the general tool — you
either specify a step (by =) or a number of values
(length.out =). rep() repeats. Between these three you can
build almost any pattern you need.
Types of vectors
A vector has a type. All elements share one type. The basic ones:
| Type | Example |
|---|---|
| numeric (double) | c(1.0, 2.5, -3.7) |
| integer | c(1L, 2L, 3L) (the L makes it an integer) |
| character | c("apple", "banana") |
| logical | c(TRUE, FALSE, TRUE) |
If you try to mix types in c(), R will silently coerce them to
a common type:
The coercion order is: logical → integer → numeric → character.
This is sometimes surprising — c(1, "two") gives you the
string "1", not the number 1. It is one of R's well-known
gotchas. The fix is to be intentional: keep one type per vector,
and use a list (list()) when you genuinely need mixed types.
Indexing: get me element N (or N, M, P…)
You access elements with [ ]. The first element is [1], not
[0] like in Python or JavaScript.
Two things to notice:
- R uses 1-based indexing. The first element is 1. (This matches how mathematicians and statisticians have always counted things. Most "data people" find it natural.)
- A negative index doesn't mean "from the end" — it means
exclude this position. To get the last element, use
days[length(days)]ortail(days, 1).
Naming a vector's elements
You can give names to the slots in a vector. This is wildly useful for working with tabular-ish data without making a full data frame.
Many R functions return named vectors. When you compute summary statistics, you will see this constantly:
That Min., 1st Qu., etc., are the names of the result vector.
Why this matters: vectors are your data
In nearly every analysis you will ever do, your data lives as columns. A column is a vector. The "ages" of every patient. The "prices" of every transaction. The "temperatures" of every day.
When you learn to think in vectors, you are learning to think the way data analysts think: in terms of whole columns at once, never one element at a time.
The next page is about operating on vectors — and that is where R really starts to shine.
Test your understanding
What does length(c("a", "b", "c")) return?
1
2
3
"abc"
What is the value of c(1, "two", TRUE)?
A list with three different types
An error
A character vector: "1" "two" "TRUE"
A numeric vector: 1 2 1
Given x <- c(10, 20, 30, 40, 50), what does x[-2] return?
Hint: in R a negative index does not count from the end (as in some languages) — it drops that position.
20
40
10 30 40 50
50 40 30 10 (reversed without the second)
Mini challenge: pick out the weekdays
You're given a vector of all 7 days of the week. Use indexing to
produce a new vector weekdays that contains only Monday through
Friday.
Use indexing to assign weekdays to the first 5 elements of days.
With vectors in hand, we're ready for the magic trick: doing arithmetic on entire vectors at once.
Variables and Assignment
How R stores and retrieves values by name, why R uses the strange-looking `<-` arrow, and how the simple act of naming things is the most underrated skill in programming.
Vectorized Computation
Why R lets you write `prices * 1.08` to add tax to a thousand prices at once — and why that one idea changes how you think about programming.