Vectors are lists of data, eg a list of numbers or a list of strings.
The function that creates a vector is c()
, where ‘c’ is short for ‘concatenate’. You already met this function in the page on characters and strings.
# A vector of strings:
x <- c("Alfa", "Bravo", "Charlie")
# A vector of numbers:
x <- c(1, 2, 3)
# A vector of both:
x <- c("Alfa", "Bravo", 3, 4)
You can also use the vector()
function, which creates an empty vector of a certain type and length. In this example, it creates a vector of four integers (which, currently do not exist):
x <- vector("integer", 4)
To see what is located at a certain point in the vector, use square brackets to index it. For example, to see the second element of x = c('Alfa', 'Bravo', 'Charlie', 'Delta')
:
print(x[2])
## [1] "Bravo"
To see all elements from the second to the fourth:
print(x[2:4])
## [1] "Bravo" "Charlie" "Delta"
You can overwrite a value in a vector by indexing its position and setting it equal to something else:
x[1] <- "ALFA"
print(x)
## [1] "ALFA" "Bravo" "Charlie" "Delta"
You can also use the mapvalues()
function from the plyr
package to specify exactly what values you want to overwrite:
x <- plyr::mapvalues(
x,
c("Bravo", "Charlie"),
c("BRAVO", "CHARLIE")
)
print(x)
## [1] "ALFA" "BRAVO" "CHARLIE" "Delta"
To add an element to a vector, simple index one position after the last. For example, if a vector has four elements we need to index position five and set it equal to something:
x[5] <- "Echo"
print(x)
## [1] "ALFA" "BRAVO" "CHARLIE" "Delta" "Echo"
Be careful though because if you get the element number wrong you will overwrite something instead of appending. A way to guarantee that you will always index the correct position is by using the length()
function to get the number of elements in the vector. The length also happens to be the position of the last element, so by indexing one more than this value you will always be appending to the vector:
x[length(x) + 1] <- "Foxtrot"
print(x)
## [1] "ALFA" "BRAVO" "CHARLIE" "Delta" "Echo" "Foxtrot"
This trick ensures you are always appending not overwriting, no matter what vector you are using.
If a vector contains missing data you might want to remove it. This is done by indexing the elements that are not “na”. To do this you need to:
is.na()
function to find the elements that are nulls# Create a vector with missing data
x <- c(1, 2, 3, NULL, 5)
# Remove nulls
x <- x[!is.na(x)]
# Check that it has worked
print(x)
## [1] 1 2 3 5