6) Setting Vector Labels \ Names

This Blog entry is from the Data Structures section in Learn R.

Selecting from a chracter vector follows the same pattern,  in so far as the crieria sits inside [] square brackets and allows for the specifc selection of values or the specific exclusion of values.  Create a chracter vector by typing numbers,  hencforth ages:

Ages <- c(22,23,28)

Run the line of script to console:


It is possible to add labels to the entries in the vector using the names() function, similar to column headers in an Excel spreadsheet.   The label 22 is Tom’s Age, 23 is Harries Age and lastly 28 is Dicks Age.  To add labels to each Vector value, type:

names(Ages) <- c("Tom","Harry","Dick")

Run the line of script to console:


It can be observed that the Vector in the environment pane is now marked as being a ‘Named’ vector:


Outputting the Vector to console, type:


Run the line of script to console:


It can be observed that the vector more closely resembles the row of a spreadsheet.  The names function will be used more extensively when aggregating Vectors into a Matrix, for the time being however, it will be used to allow for the selection of just that individuals Age.