4 Vectors
4.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"
4.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
4.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
4.6 Arithmetic operations
4.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