## Vectors

A vector in R is simply a sequence of data elements, usually numbers. A vector can be created using the command c(). The elements are held between the brackets and separated by a comma as follows:

#1) A numeric vector:

x <- c(2,3,6)

#2) A character vector:

y <- c("peter", "paul", "mary")

#3) a logical vector

z <- c(TRUE, TRUE, FALSE)

x
##  2 3 6
y
##  "peter" "paul"  "mary"
z
##   TRUE  TRUE FALSE

Note that the elements of a character vector and held between quotation marks " ". Despite TRUE and FALSE being words, they are logical elements not character elements in R. They are not held between quotation marks.

## Vector Arithmetic

Notice how we can quickly apply operations “element-wise” on vectors:

vec.1 <- c(2, 3, 4)
vec.2 <- c(3, -2, 1)

#to do so, use #

#here is some simple vector arithmetic:

vec.1 + vec.2
vec.1 - vec.2
vec.1*vec.2
vec.1/vec.2
vec.1 + 7
vec.1*7
vec.1/7

Here are the results in order:

##  5 1 5
##  -1  5  3
##   6 -6  4
##   0.6666667 -1.5000000  4.0000000
##   9 10 11
##  14 21 28
##  0.2857143 0.4285714 0.5714286

## Generating Vector Sequences

There are a few tricks for quickly generating sequences which you will find helpful:

rep(4, 7)
seq(1,21, by = 2)
seq(0, 10, len = 5)
seq(0, 10, len = 10)
1:20

Here are the results in order:

##  4 4 4 4 4 4 4
##    1  3  5  7  9 11 13 15 17 19 21
##   0.0  2.5  5.0  7.5 10.0
##   0.00  1.25  2.50  3.75  5.00  6.25  7.50  8.75 10.00
##    1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

## Functions of Vectors

Functions can be applied element-wise to vectors. Here are some examples:

vec.3 <- 1:5

vec.3^2
sqrt(vec.3)
1/vec.3
sin(vec.3)
exp(vec.3)
vec.3^(1/3)

Here are the results in order:

##   1  4  9 16 25
##  1.000000 1.414214 1.732051 2.000000 2.236068
##  1.0000000 0.5000000 0.3333333 0.2500000 0.2000000
##   0.8414710  0.9092974  0.1411200 -0.7568025 -0.9589243
##    2.718282   7.389056  20.085537  54.598150 148.413159
##  1.000000 1.259921 1.442250 1.587401 1.709976

## Interrogating Vectors

Given a vector, we can ask R lots of useful questions about it. We will look at: Common Functions, Searching a vector for a Condition and Indexing a vector.

#### Common Functions

vec.4 <- c(2,2,3,3,3,4,5,6,7,7,7,7,8,9,10,10,10)

#common functions:
length(vec.4)
unique(vec.4)
sum(vec.4)
prod(vec.4)
mean(vec.4)
var(vec.4)
sd(vec.4)
quantile(vec.4)

Here are the results in order:

##  17
##   2  3  4  5  6  7  8  9 10
##  103
##  2.240421e+12
##  6.058824
##  8.058824
##  2.838807
##   0%  25%  50%  75% 100%
##    2    3    7    8   10

#### Searching for a Condition

Look at the following code using the which() function. What do you think the results will be?

vec.4 <- c(2,2,3,3,3,4,5,6,7,7,7,7,8,9,10,10,10)

#searching for a condition:
which(vec.4 == 3)
which(vec.4 < 7)
which(vec.4 <= 7)
which((vec.4 < 3) | (vec.4 > 8))
which((vec.4 > 3) & (vec.4 < 8))

The results are the positions of elements within vec.4 for which the stated condition holds:

##  3 4 5
##  1 2 3 4 5 6 7 8
##    1  2  3  4  5  6  7  8  9 10 11 12
##   1  2 14 15 16 17
##   6  7  8  9 10 11 12

#### Indexing a Vector

We can easily look up an element of a vector in a given position. For example, to ask R for the fifth element of vec.4, we simply type vec.4. This is known as indexing the vector. We index a vector using square brackets as in the following code:

vec.4 <- c(2,2,3,3,3,4,5,6,7,7,7,7,8,9,10,10,10)

#in the following, notice that we "index" a vector using square brackets.

#the element in position 8:
vec.4

#the element in position 9:
vec.4

#the elements in the positions specified by a vector of indices:
index <- c(1,5,8,11)
vec.4[index]

The results are the elements to be found in the positions specified:

##  6
##  7
##  2 3 6 7

## Manipulating Vectors

We can search and subsequently alter vectors. Notice that sometimes we might want to keep both versions of the vector and other times we might want to over-write the original vector.

#### example 1

The $$8^{th}$$ element is altered and the vector is over-written:

vec.4 <- 76
vec.4
##    2  2  3  3  3  4  5 76  7  7  7  7  8  9 10 10 10

#### example 2

All the elements of vec.4 which are equal to $$7$$ are replaced with $$21$$ and the vector is over-written:

vec.4[which(vec.4 == 7)] <- 21
vec.4
##    2  2  3  3  3  4  5 76 21 21 21 21  8  9 10 10 10

#### example 3

All elements less than $$10$$ are set to $$0$$ and the result is stored as a new vector:

vec.5 <- vec.4
vec.5[which(vec.5 < 10)] <- 0
vec.4
##    2  2  3  3  3  4  5 76 21 21 21 21  8  9 10 10 10
vec.5
##    0  0  0  0  0  0  0 76 21 21 21 21  0  0 10 10 10

Make sure you understand the logic of this code. which(vec.5 < 10) is a vector of indices providing the positions of elements in vec.5 which are less than $$10$$. vec.5[which(vec.5 < 10)] then picks out the elements of vec.5 indexed by which(vec.5 < 10). Finally, these elements are assigned value $$0$$ using <- 0.

This whole process could have been split up to make this logic a little more clear in the code:

vec.5 <- vec.4
#I gives the positions of elements in vec.5 which are less than 10.
I <- which(vec.5 < 10)
I
##   1  2  3  4  5  6  7 13 14

We now use this index set to assign the values in these positions to $$0$$:

vec.5 <- vec.4
#I gives the positions of elements in vec.5 which are less than 10.
vec.5[I] <- 0
vec.5
##    0  0  0  0  0  0  0 76 21 21 21 21  0  0 10 10 10