# 3 Vectors

## 3.1 Creating a vector

vet1 <- c(-1,0,1,2,3,4)
vet1
## [1] -1  0  1  2  3  4

or

vet2 <- -1:4
vet2
## [1] -1  0  1  2  3  4

It is possible to create vectors with characters

vetc <- c("AU", "TH", "Test - 1", "Test = 0")
vetc
## [1] "AU"       "TH"       "Test - 1" "Test = 0"

## 3.2 Combining vectors

c(vet1, vet2)
##  [1] -1  0  1  2  3  4 -1  0  1  2  3  4

## 3.3 Accessing vector elements

Only the element in position 2

vet2[2]
## [1] 0

Different positions in the same vector

vet2[c(1,3,4)]
## [1] -1  1  2

## 3.4 Indexing vectors with characters

vet <- c("One" = 0, "Two" = 3, "Three" = 5)
vet
##   One   Two Three
##     0     3     5
vet["Two"]
## Two
##   3

OR

vet <- c(0,3,5)
names(vet) <- c("One","Two","Three")
vet
##   One   Two Three
##     0     3     5
vet["Two"]
## Two
##   3

## 3.5 Indexing with Logic Vector

vet[c(TRUE, TRUE, FALSE)]
## One Two
##   0   3

## 3.6 Arithmetic operations

### 3.6.1 Operations with a constant

vet1 + 2
## [1] 1 2 3 4 5 6
vet1 - 2
## [1] -3 -2 -1  0  1  2
vet1 / 2
## [1] -0.5  0.0  0.5  1.0  1.5  2.0
vet1 * 2
## [1] -2  0  2  4  6  8

### 3.6.2 Operations between vectors

vet1 + vet2
## [1] -2  0  2  4  6  8
vet1 - vet2
## [1] 0 0 0 0 0 0
vet1 / vet2
## [1]   1 NaN   1   1   1   1
vet1 * vet2
## [1]  1  0  1  4  9 16