B.6 Named Vectors

B.6.1 Creating Named Vectors

Vectors in R can be named – each element can be assigned a string label.

##  a  b  c  d  e 
## 20 40 99 30 10

Other ways to create named vectors include:

## a b c 
## 1 2 3
## a b c 
## 1 2 3

For instance, the summary() function returns a named vector:

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    10.0    20.0    30.0    39.8    40.0    99.0

This gives the minimum, 1st quartile (25%-quantile), Median (50%-quantile), aritmetic mean, 3rd quartile (75%-quantile) and maximum.

Note that x is still a numeric vector, we can perform various operations on it as usual:

## [1] 199
##  a  b  c  d  e 
## 20 40 99 30 10

Names can be dropped by calling:

## [1] 20 40 99 30 10
## [1] 20 40 99 30 10

B.6.2 Subsetting Named Vectors with Character String Indices

It turns out that extracting elements from a named vector can also be performed by means of a vector of character string indices:

##  a  d  b 
## 20 30 40
## Median   Mean 
##   30.0   39.8