Vectors
Featured in 5 main posts
In R, a vector is a group of elements of the same type, arranged in a specific order. The number of elements in a vector is called its length.
Vectors are the most basic data structures in R. There is no distinction between a vector and a single element. In R, a single element is simply a vector of length 1.
Common types of vectors
There are many types of vectors, but the most commonly used in this blog’s posts are the following:
-
numeric: Rational numbers. -
character: Strings of text. -
logical(also called Boolean): Can only take binary values. i.e.TRUEorFALSE(can also be coded asTorF).
Creating vectors
There are many ways to create a vector, depending on its type and how we want it to be. For example, for create an arithmetic sequence, we would use the appropriate functions. Here we’ll see the simplest case, creating a vector by combining elements.
Combine elements in a vector
To combine single, known elements into a vector, use the c function:
a <- c(2,7,3,6,4)
a
## [1] 2 7 3 6 4
This piece of code creates a variable named a and assigns to it a vector with the values [2,7,3,6,4] in this specific order.
The [1] shown on the left side is simply the index of the left-most element. This is useful for large vectors that may span more lines when displayed.
Vectors can be similarly constructed from smaller vectors:
b <- c(a,c(5,1,8))
b
## [1] 2 7 3 6 4 5 1 8
If we try to combine elements of different types, they are all coerced to the same type:
# Combine a logical, a numeric and a character element
d <- c(TRUE,3,"test")
# See the resulting vector's type
typeof(d)
## [1] "character"
In this case, all elements were coerced into character. This is because character is “highest” in the hierarchy between these three types. Let’s see how this vector looks:
d
## [1] "TRUE" "3" "test"
The hierarchy of these three types is: character > numeric > logical . For more information, check the R documentation by running help(c).
Accessing vector elements
To call a specific element within a, refer to its position in square brackets ([]).
# Show the 2nd element of a
a[2]
## [1] 7
Note: In R, indexing starts from 1 (in contrast to Python, where it starts from 0).
An element within the vector can be changed with a simple assignment, as below:
# Change the value of the 2nd element of a to 3
a[2] <- 3
a
## [1] 2 3 3 6 4
To exclude elements from a vector, use the - symbol as follows:
a[-1]
## [1] 3 3 6 4
Arithmetic operations with vectors
Vectors of the same length can be added, subtracted, multiplied, divided, or raised to the power of one another. The operations are performed element by element.
d <- c(4,2,8,5,3)
a + d
## [1] 6 5 11 11 7
Element recycling
If the vectors have different length, then the elements of the shorter ones are recycled, i.e. the 1st element is reused. That’s how the following addition works:
c(5,6,7) + 2
## [1] 7 8 9
The number 2 (vector of length 1) is added to [5,6,7] (vector of length 3). After 2 is added to the 1st element of the other vector (5), it is recycled and added to the 2nd element (6) and the 3rd element (7).