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:
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:
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()
.
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:
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.
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()
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:
When specifying each
, it will repeat each elements this many times consecutively:
These arguments can be combined in many ways:
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 []
:
We can access more than one element at once (this is called slicing) by providing a sequence of indices:
In addition, this sequence of indices doesn’t have to be consecutive:
We can also provide negative indices. In this case, it just removes the element with this (absolute) index from the vector:
Apart from just reading values, indexing can also be used to set values inside a vector:
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()
.
The function integer()
creates an integer vector of the specified length. The elements of the vector are all equal to 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
(theL
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.
Create a vector
x
with values -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 usingc()
.Given
x
from above, append -6 in front of the vector and 6 at the end.Create
vec1
with values -5, -4, …, 4, 5 andvec2
with values 6, 7, …, 10. Now concatenate them. What is the length of the resulting vector? What are its elements?Create the following sequences using the
:
operator: -5, -4, …, 4, 5 and 10, 9, …, 6.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
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
Create a vector
x
with values -5, -4, -3, …, 4, 5. Then, use indexing to access the element with value 2.Access all elements of
x
with even values.Set the last 3 elements of
x
to the values 30, 40, and 50, respectively.Create an empty vector of size 5 using
numeric()
. Then, use indices to set the first element to 1, the second to 2 etc.Given a vector
x
with values -5, -4, -3, …, 3, 4, 5, exclude all elements with even values.