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
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
= c(23, 1, 60, 21, 21, 5) vec1
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
= c(23, 1, 60, 21, 21, 5)
vec1 + 6 vec1
[1] 29 7 66 27 27 11
= c(5, 1000, 5, 1000, 5, 1000)
vec2 - vec2 vec1
[1] 18 -999 55 -979 16 -995
# be careful with vectors of different length!
= c(1, 2, 1)
vec3 * vec3 vec1
[1] 23 2 60 21 42 5
= c(1, 2, 1, 2)
vec4 * vec4 vec1
[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
3] vec1[
[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.
<- c(1,2)
index_vector vec1[index_vector]
[1] 23 1
## in short:
c(1,2)] vec1[
[1] 23 1
What is an easy way to access the first 10 elements of the vector?