Chapter 4 Vectors

Vectors are the most basic R data objects. Even when you write a single number in R, it becomes a vector of length one:

> 5
[1] 5

The preceding [1] that is printed to the console indicates the index of the vector. In this case, 5 is the first element in the vector and therefore has index 1.

A vector can have more than one element. Formally, it is a sequence of elements of the same type, for example numbers. Vectors can be created using the c() function:

> x <- c(2, 4, 6, 8, 10)

This function c() simply concatenates all arguments into a single vector. It can also coerce vectors and numbers or vectors and vectors:

> x <- c(2, 4, 6, 8, 10)
> c(-100, x, 100)
[1] -100    2    4    6    8   10  100
> c(c(2, 4, 6), c(8, 10))
[1]  2  4  6  8 10

The number of elements in the vector is obtained by the function length().

> x <- c(2, 4, 6, 8, 10)
> length(x)
[1] 5

4.0.1 Sequences

Sequences of numbers are used frequently, and R offers functions to create such sequences easily. For creating a vector of consecutive numbers, we can use the : operator:

> 1:10
 [1]  1  2  3  4  5  6  7  8  9 10
> x <- 10; y <- 5
> x:y
[1] 10  9  8  7  6  5
> (x-1):(y+2)
[1] 9 8 7

The function seq() offers a more general way to create sequences of numbers. The first two arguments specify the beginning and end of the sequence.

> seq(1, 10)
 [1]  1  2  3  4  5  6  7  8  9 10

The third argument can either be by, denoting the step size, or length.out, denoting the total length of the sequence:

> seq(-5, 5, by = 0.5)
 [1] -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0
[16]  2.5  3.0  3.5  4.0  4.5  5.0
> seq(-5, 5, length.out = 21)
 [1] -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0
[16]  2.5  3.0  3.5  4.0  4.5  5.0

The function rep() can be used for replicating an object in various ways.

> rep(5)
[1] 5
> rep(5, times=3)
[1] 5 5 5
> rep(5, length.out=3)
[1] 5 5 5

rep() can also replicate vectors with more than one element. When specifying times, it will replicate the entire vector this many times. times can also be a vector itself (of same length as x), in which case it will replicate the each element of x according to the corresponding element in times:

> x <- c(2, 4, 6, 8, 10)
> rep(x, times = 3)
 [1]  2  4  6  8 10  2  4  6  8 10  2  4  6  8 10
> rep(x, times = 1:5)
 [1]  2  4  4  6  6  6  8  8  8  8 10 10 10 10 10

When specifying length.out, it will replicate the elements of the vector as long as this length is achieved:

> x <- c(2, 4, 6, 8, 10)
> rep(x, length.out = 6)
[1]  2  4  6  8 10  2

When specifying each, it will repeat each elements this many times consecutively:

> x <- c(2, 4, 6, 8, 10)
> rep(x, each = 2)
 [1]  2  2  4  4  6  6  8  8 10 10

These arguments can be combined in many ways:

> x <- c(2, 4, 6, 8, 10)
> rep(x, length.out = 6, each = 2)
[1] 2 2 4 4 6 6
> rep(x, times = 3, each = 2)
 [1]  2  2  4  4  6  6  8  8 10 10  2  2  4  4  6  6  8  8 10 10  2  2  4  4  6
[26]  6  8  8 10 10

4.0.2 Accessing Elements of Vectors

Elements of a vector can be accessed by indexing. Indexing in R starts at 1, i.e. the first element in the vector has index 1. Most other programming languages, including python, start indexing at 0, so be aware of this! We can access the values inside a vector by declaring the index inside the square brackets []:

> x <- c(2, 4, 6, 8, 10)
> x[1]
[1] 2
> x[2]
[1] 4
> x[5]
[1] 10

We can access more than one element at once (this is called slicing) by providing a sequence of indices:

> x <- c(2, 4, 6, 8, 10)
> x[2:4]
[1] 4 6 8

In addition, this sequence of indices doesn’t have to be consecutive:

> x <- c(2, 4, 6, 8, 10)
> x[c(1, 3, 5)]
[1]  2  6 10

We can also provide negative indices. In this case, it just removes the element with this (absolute) index from the vector:

> x <- c(2, 4, 6, 8, 10)
> x[-3]
[1]  2  4  8 10
> x[-(2:4)]
[1]  2 10

Apart from just reading values, indexing can also be used to set values inside a vector:

> x <- c(2, 4, 6, 8, 10)
> x[1] <- 100
> x[3:5] <- 1:3
> x
[1] 100   4   1   2   3

4.0.3 Integer Vectors

Each vector in R is of a specific type. All examples so far were of type integer as they only contained integers. You can check the type using the function class().

> class(1)
[1] "numeric"
> class(-3:3)
[1] "integer"

The function integer() creates an integer vector of the specified length. The elements of the vector are all equal to 0.

> integer(10)
 [1] 0 0 0 0 0 0 0 0 0 0

In R, there are six atomic data types that are used to build atomic vectors:

  • character: e.g. 'a', "b", "hello"

  • numeric: e.g. -1, 3.46

  • logical: FALSE, TRUE

  • integer: e.g. 3L (the L for “long” tells R that this is an integer)

  • complex: 1+6i (complex numbers with real and imaginary parts)

  • raw: contains raw bytes; e.g. “hello” corresponds to 68 65 6c 6c 6f

In the following we will introduce the most frequently used types in more detail: numeric, logical, character and factors.

4.0.4 Exercises: Introduction to vectors

See Section 18.0.5 for solutions.

  1. Create a vector x with values -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 using c().

  2. Given x from above, append -6 in front of the vector and 6 at the end.

  3. Create vec1 with values -5, -4, …, 4, 5 and vec2 with values 6, 7, …, 10. Now concatenate them. What is the length of the resulting vector? What are its elements?

  4. Create the following sequences using the : operator: -5, -4, …, 4, 5 and 10, 9, …, 6.

  5. Create the following vectors with the function rep():

    • vec1 with values 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5
    • vec2 with values 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5
    • vec3 with values 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5
    • vec4 with values 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5
  6. Create the following vectors with the function seq():

    • vec1 with values 0.0, 0.5, 1.0, …, 5.0
    • vec2 with values 1.0, 2.4, 3.8, 5.2, 6.6, 8.0
    • vec3 with values 10.0, 9.2, 8.4, …, -0.4
  7. Create a vector x with values -5, -4, -3, …, 4, 5. Then, use indexing to access the element with value 2.

  8. Access all elements of x with even values.

  9. Set the last 3 elements of x to the values 30, 40, and 50, respectively.

  10. Create an empty vector of size 5 using numeric(). Then, use indices to set the first element to 1, the second to 2 etc.

  11. Given a vector x with values -5, -4, -3, …, 3, 4, 5, exclude all elements with even values.