Vectors are a sequence of values of the same type and the most basic type of data in R. Even a single value is treated as a vector of length 1. When all of the elements of the vector are numbers, the vector behaves as a mathematical vector and R contains many linear algebra tools and functions that allow us to perform basic linear algebra.
The most basic way to create a vector is to use th c
function to ‘c
ombine’ several values of the same type into a vector. Remember that all the values must be of the same type (numerical, logical, character) within a vector - if you mix up the variable types they will be changed to a common type in the vector
x <- c(1,2,3) ## numerical vector
y <- c("cat","dog") ## character vector
z <- c(TRUE,FALSE,FALSE,TRUE) ## logical vector
v <- c(1,"cat",FALSE) ## these will all be converted to character data
The c
function can also be used to combine vectors together:
c(x, c(10,9,8,7))
## [1] 1 2 3 10 9 8 7
R Help: c
R provides some simple functions for quickly creating numerical vectors.
:
We can use the colon :
operator to create integer sequences between two values and return the result as a vector:
x <- 1:9
x
## [1] 1 2 3 4 5 6 7 8 9
R Help: : operator
seq
The seq
function also generates a sequence between its two arguments from
and to
, but is more sophisticated than :
. We can dictate the length of the sequence by supplying the optional length
argument, or the step size in the sequence by passing a value to the by
argument. If we supply neither length
nor by
, then seq
gives an integer sequence like :
.
y <- seq(1,9)
y ## same as x
## [1] 1 2 3 4 5 6 7 8 9
seq(1,10,length=19) ## sequence of given length
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
## [16] 8.5 9.0 9.5 10.0
seq(15,45,by=3) ## sequence of given step
## [1] 15 18 21 24 27 30 33 36 39 42 45
R Help: seq
rep
R also allows you to easily create vectors by repeating elements and joining them together into a vector using the rep
function. We can also repeat entire vectors using rep
, or use a vector in the number of repetitions to repeat each element a different number of times.
rep(10, 5)
## [1] 10 10 10 10 10
rep(1:3, 6)
## [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
rep(1:3, 4:2)
## [1] 1 1 1 1 2 2 2 3 3
R Help: rep
Given a numerical vector (or a list of numbers), most of the basic arithmetic operations and mathematical functions can be applied to the whole vector and can be used to quickly perform a large number of calculations with a single command.
a <- c(1,2,3,4)
a
## [1] 1 2 3 4
a + 5
## [1] 6 7 8 9
log(a)
## [1] 0.0000000 0.6931472 1.0986123 1.3862944
(a + sqrt(a))/(exp(2)+1)
## [1] 0.2384058 0.4069842 0.5640743 0.7152175
We can apply standard operations to multiple vectors. The calculations will be performed element-by-element and the results returned as a new vector.
b <- a - 10
(a+3)/(sqrt(1-b)*2-1)
## [1] 0.7512364 1.0000000 1.2884234 1.6311303
When you do operations on vectors they are performed on an element by element basis. One ramification of this is that all of the vectors in an expression must be the same length. If the lengths of the vectors differ, then you may get an error message. However, R still returns an answer which it produces by repeating the elements of the shortest vector until it is of the correct length and then doing the computation. Obviously, if this is not what you intended then this can have unpredictable and dangerous results. In the example below, elements of a
are repeated until it is of length 4 and then added to b
:
a <- c(1,2,3)
b <- c(10,11,12,13)
a+b
## [1] 11 13 15 14
c(1,2,3,1)+c(10,11,12,13)
## [1] 11 13 15 14
length
{#length} returns the length of the vector length(a)
## [1] 3
sort
{#sort} returns a new vector obtained by sorting the elements of the argument vector into ascending order. Descending order can be obtained by setting the optional argument decreasing=TRUE
.a <- c(5,9,2,1,3)
sort(a)
## [1] 1 2 3 5 9
sort(a,decreasing=TRUE)
## [1] 9 5 3 2 1
rev
{#rev} reverse the order of the elements in the given vectorrev(a)
## [1] 3 1 2 9 5
unique
{#unique} returns the unique elements in the given vectorvals <- c(1,2,3,2,3,3,1,2,5,1,2,2,2,2,3,1,1)
unique(vals)
## [1] 1 2 3 5
table
{#table} constructs a table of counts for the occurrence of each unique element in the given vectortable(vals)
## vals
## 1 2 3 5
## 5 7 4 1
Given a vector of data one common task is to isolate and extract particular entries from the larger list. Here we show how to use R’s indexing notation to pick out specific items within a vector.
To extract an element from a known position within the vector, we use square brackets []
and state the index of the element we want within the brackets as follows:
a <- c(6,2,5,3,8,2)
a[3] ## third element of a
## [1] 5
To extract all the elements of the vector except for one element at a known position we can pass the index as a negative number into the square brackets
a[-3] ## all but the third element of a
## [1] 6 2 3 8 2
To extract multiple elements, we can supply a vector of indices to extract
a[c(1,3,5)]
## [1] 6 5 8
a[1:3]
## [1] 6 2 5
a[-(1:3)]
## [1] 3 8 2
We can also use any logical vector can be used as an index, which opens a wide range of possibilities. For example, you can remove or focus on entries that match specific criteria. For example, you might want to remove all entries that are above a certain value:
a<5
## [1] FALSE TRUE FALSE TRUE FALSE TRUE
a[a<5]
## [1] 2 3 2
a[a%%2 == 0] # only select even values
## [1] 6 2 8 2
R Help: [] for subsetting
which
is a function that takes a vector of logical (TRUE
or FALSE
) values and returns the indices of those which are true. While superifically this doesn’t seem very useful, if the vector of logical values was the result of a comparison or test of the values of another vector then which
will tell us which elements of the vector passed the test:
which(a<5)
## [1] 2 4 6