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)
Basic information about a vector that has been created can be retrieved by calling various functions with the vector in question as the input:
length()
for the number of elements in the vectorclass()
for the type of data of the elements in the
vectormin()
, max()
and range()
for
the minimum, maximum and range of the datax <- c(6, 2, 8, 3, 1, 8)
length(x)
class(x)
min(x)
max(x)
range(x)
## [1] 6
## [1] "numeric"
## [1] 1
## [1] 8
## [1] 1 8
To see what value is located at a specific position in a vector, use square brackets and a number to index it:
x <- c("Alfa", "Bravo", "Charlie", "Delta", "Echo")
# Returns the 2nd element
x[2]
## [1] "Bravo"
To see multiple specific elements, use a vector of the indexes you are interested in:
# Returns the 1st and 3rd elements
x[c(1, 3)]
## [1] "Alfa" "Charlie"
To see all elements from one to another, index the vector by using a
range of numbers (created by using a colon :
):
# Returns all elements from the 2nd to the 4th
x[2:4]
## [1] "Bravo" "Charlie" "Delta"
Use length()
to reference the last value in a
vector:
# Returns all elements from the 2nd to the last
x[2:length(x)]
## [1] "Bravo" "Charlie" "Delta" "Echo"
This is also useful for indexing from the end of a vector
(although you need to remember to include brackets around statements
that you want evaluated, eg length(x)-2
):
# Returns all elements from the 3rd-last to the end
x[(length(x)-2):length(x)]
## [1] "Charlie" "Delta" "Echo"
It’s usually easier to use the tail()
function
instead:
# Returns all elements from the 3rd-last to the end
tail(x, n=3)
## [1] "Charlie" "Delta" "Echo"
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" "Echo"
You can also use the mapvalues()
function from the
plyr
package to specify exactly what values you want to
overwrite:
library(plyr)
x <- plyr::mapvalues(
x,
c("Bravo", "Charlie"),
c("BRAVO", "CHARLIE")
)
print(x)
## [1] "ALFA" "BRAVO" "CHARLIE" "Delta" "Echo"
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[6] <- "Foxtrot"
print(x)
## [1] "ALFA" "BRAVO" "CHARLIE" "Delta" "Echo" "Foxtrot"
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] <- "Golf"
print(x)
## [1] "ALFA" "BRAVO" "CHARLIE" "Delta" "Echo" "Foxtrot" "Golf"
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