3 Vectors
3.1 Creating a vector
<- c(-1,0,1,2,3,4)
vet1 vet1
## [1] -1 0 1 2 3 4
or
<- -1:4
vet2 vet2
## [1] -1 0 1 2 3 4
It is possible to create vectors with characters
<- c("AU", "TH", "Test - 1", "Test = 0")
vetc 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
2] vet2[
## [1] 0
Different positions in the same vector
c(1,3,4)] vet2[
## [1] -1 1 2
3.4 Indexing vectors with characters
<- c("One" = 0, "Two" = 3, "Three" = 5)
vet vet
## One Two Three
## 0 3 5
"Two"] vet[
## Two
## 3
OR
<- c(0,3,5)
vet names(vet) <- c("One","Two","Three")
vet
## One Two Three
## 0 3 5
"Two"] vet[
## Two
## 3
3.5 Indexing with Logic Vector
c(TRUE, TRUE, FALSE)] vet[
## One Two
## 0 3
3.6 Arithmetic operations
3.6.1 Operations with a constant
+ 2 vet1
## [1] 1 2 3 4 5 6
- 2 vet1
## [1] -3 -2 -1 0 1 2
/ 2 vet1
## [1] -0.5 0.0 0.5 1.0 1.5 2.0
* 2 vet1
## [1] -2 0 2 4 6 8
3.6.2 Operations between vectors
+ vet2 vet1
## [1] -2 0 2 4 6 8
- vet2 vet1
## [1] 0 0 0 0 0 0
/ vet2 vet1
## [1] 1 NaN 1 1 1 1
* vet2 vet1
## [1] 1 0 1 4 9 16