Vectors
Vectors are used to store multiple values of the same data type for later use. Unlike other programming languages, vectors in R are not zero-indexed; they start from 1.
Vector Types
Atomic vectors are made with the ‘c()’ function to combine different elements of the same type together:
atomic_vector <- c(element_1, element_2, ... element_N)
Recursive vectors can have elements of any data type and is created with the list()
function:
recursive_vector <- list(element_1, element_2, ... element_N)
Examples
The following atomic vectors are created with the c()
function:
# Character vectornames <- c('Adam', 'John', 'Walter')# Integer vectormarks <- c(90, 87, 100, 79, 91)# Logical vectorbooleans <- c(TRUE, FALSE, TRUE)
Apart from the ‘c()’ function, the colon operator (:
) can be used to create a vector containing a range of numbers:
# This vector contains all the numbers between 1 and 10numbers <- 1:10numbers# Output: [1] 1 2 3 4 5 6 7 8 9 10
In R, character vectors can contain alphanumeric values and special characters.
alphanumerics <- c('water', 'fire', 'ice', 90)alphanumerics# Output: [1] "water" "fire" "ice" "90"
Accessing Vector Elements
The most common way to access vector elements is by index using the subscript operator []
:
numbers <- c(65, 49, 55, 36, 126)numbers[2]numbers[4]
The output would look like this:
[1] 49[1] 36
Vector elements can also be accessed by logical indexing:
# Accessing vector elements using logical indexing.numbers <- c(1, 2, 3, 4, 5)numbers[numbers < 4]
This would produce the following output:
[1] 1 2 3
Updating a Vector
Vector elements can be updated by specifying the index with the subscript operator ([]
) and replacing with the new value:
numbers <- c(2, 5, 1, 7, 9)cat("Original vector: ", numbers, "\n")numbers[3] = 8cat("Third element changed: ", numbers, "\n")numbers[1] = 0cat("First element changed: ", numbers)
To start, the third element in numbers
is updated to 8. Next, the first element is updated to 0. This will produce the following output:
Original vector: 2 5 1 7 9Third element changed: 2 5 8 7 9First element changed: 0 5 8 7 9
Vector Recycling
Vector recycling is a process in which two vectors of different lengths are operated upon such that the elements of the shorter vector are repeated to match the length of the longer vector.
Example
The following example presents two cases of vector recycling using addition, which requires the two vectors to be equal in length.
Case 1 is when the two vector lengths are equal:
numbers_1 <- c(1, 2, 3, 4, 5)numbers_2 <- c(3, 4, 5, 6, 7)sum <- numbers_1 + numbers_2sum# Output: [1] 4 6 8 10 12
Case 2 is when the vector lengths are unequal:
numbers_1 <- c(1, 2, 3, 4, 5, 6)numbers_2 <- c(3, 4)sum <- numbers_1 + numbers_2sum# Output: [1] 4 6 6 8 8 10
Here, the first two elements of numbers_1
are added to the first, and only, two elements of numbers_2
. Then, to complete the addition operation, R repeats (or recycles) the elements of the smaller numbers_2
vector (3
and 4
) until the end of numbers_1
is reached.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn R on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn R
Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.Beginner Friendly14 hours