We use square brackets to access specific elements of a vector. For the vector codes
we defined above, we can access the second element using:
codes[2]
#> canada
#> 124
You can get more than one entry by using a multi-entry vector as an index:
codes[c(1,3)]
#> italy egypt
#> 380 818
The sequences defined above are particularly useful if we want to access, say, the first two elements:
codes[1:2]
#> italy canada
#> 380 124
If the elements have names, we can also access the entries using these names. Below are two examples.
codes["canada"]
#> canada
#> 124
codes[c("egypt","italy")]
#> egypt italy
#> 818 380