Vectors

  • most important data structure in R
  • building block for more complicated structures
  • created with the function c()
  • think about a container where you can put in multiple things
  • vectors are atomic - each entry has to be from the same value type
  • each element has an index with which it can be accessed

R vector model
c(23, 1, 60, 21, 21, 5)
[1] 23  1 60 21 21  5
# vectors are atomic
c(23, 1, 60.5, 21, 21, 5)
[1] 23.0  1.0 60.5 21.0 21.0  5.0
# to actually use a vector we have to assign it to a variable
vec1 = c(23, 1, 60, 21, 21, 5)

Vectors and functions

Most functions in R either work with a vector (e.g. mean) or the function is “vectorized” (e.g. sqrt). Vectorized means that a functions is automatically applied to all the elements in a vector.

# function that works with a vector
mean(vec1)
[1] 21.83333
# function that is vectorised
sqrt(vec1)
[1] 4.795832 1.000000 7.745967 4.582576 4.582576 2.236068

Basic Calculations

R vector addition
vec1 = c(23, 1, 60, 21, 21, 5)
vec1 + 6
[1] 29  7 66 27 27 11
vec2 = c(5, 1000, 5, 1000, 5, 1000)
vec1 - vec2
[1]   18 -999   55 -979   16 -995

R vector multiplication, different length
# be careful with vectors of different length!
vec3 = c(1, 2, 1)
vec1 * vec3
[1] 23  2 60 21 42  5
vec4 = c(1, 2, 1, 2)
vec1 * vec4
[1] 23  2 60 42 21 10

Vector indexing

For many questions it is often required to access only parts of the data. This is one of the most common things you have to deal with in R.

To access single values of a vector, use square brackets [].

# get the third value of the vector
vec1[3]
[1] 60

If you want to access multiple values of a vector at once, you have to first create a new vector with c() that contain the index positions.

index_vector <- c(1,2)
vec1[index_vector]
[1] 23  1
## in short:
vec1[c(1,2)]
[1] 23  1

What is an easy way to access the first 10 elements of the vector?