Creating a sequence of numbers

1:100
c(1, 2, 3, 4)
c("abc", "def", "ghi")
c(TRUE, FALSE, TRUE)
View Interactive Version

In R, even a single value is considered a vector. Creating a vector of one element is as simple as typing its value:

Input
4
Output
[1] 4

To create a sequence of numeric values we can use the : operator, which takes two numbers and outputs a vector of all whole numbers in that range:

Input
2:11
Output
 [1]  2  3  4  5  6  7  8  9 10 11

The : operator creates a vector from the number on the left-hand side to the number on the right-hand side. Therefore, the order of numbers is important. If we define the previous example the other way around, we get a vector of descending numbers, instead of ascending:

Input
11:2
Output
 [1] 11 10  9  8  7  6  5  4  3  2

The : operator comes handy when we need a vector of every whole number in a given range. However, if we need a vector where the numbers aren’t linear, we require something different.

Combine values into a vector