How to Name the Values in Your Vectors in R
Often the elements of vectors are referred to by their positions — that is, x refers to the fifth element in vector x. One very powerful feature in R, however, gives names to the elements of a vector, which allows you to refer to the elements by name.
You can use these named vectors in R to associate text values (names) with any other type of value. Then you can refer to these values by name in addition to position in the list. This format has a wide range of applications — for example, named vectors make it easy to create lookup tables.
How R named vectors work
To illustrate named vectors, take a look at the built-in dataset islands, a named vector that contains the surface area of the world’s 48 largest land masses (continents and large islands). You can investigate its structure with str(), as follows:
> str(islands) Named num [1:48] 11506 5500 16988 2968 16 ... - attr(*, "names")= chr [1:48] "Africa" "Antarctica" "Asia" "Australia" ...
R reports the structure of islands as a named vector with 48 elements. In the first line of the results of str(), you see the values of the first few elements of islands. On the second line, R reports that the named vector has an attribute containing names and reports that the first few elements are "Africa", "Antarctica", "Asia", and "Australia".
Because each element in the vector has a value as well as a name, now you can subset the vector by name. To retrieve the sizes of Asia, Africa, and Antarctica, use the following:
> islands[c("Asia", "Africa", "Antarctica")] Asia Africa Antarctica 16988 11506 5500
You use the names() function to retrieve the names in a named vector:
> names(islands)[1:9]  "Africa" "Antarctica" "Asia"  "Australia" "Axel Heiberg" "Baffin"  "Banks" "Borneo" "Britain"
This function allows you to do all kinds of interesting things. Imagine you wanted to know the names of the six largest islands. To do this, you would retrieve the names of islands after sorting it in decreasing order:
> names(sort(islands, decreasing=TRUE)[1:6])  "Asia" "Africa" "North America"  "South America" "Antarctica" "Europe
Create and assign named vectors in R
You use the assignment operator (<-) to assign names to vectors in much the same way that you assign values to character vectors.
Imagine you want to create a named vector with the number of days in each month. First, create a numeric vector containing the number of days in each month. Then use the built-in dataset month.name for the month names, as follows:
> month.days <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) > names(month.days) <- month.name > month.days January February March April 31 28 31 30 May June July August 31 30 31 31 September October November December 30 31 30 31
Now you can use this vector to find the names of the months with 31 days:
> names(month.days[month.days==31])  "January" "March" "May"  "July" "August" "October"  "December"
This technique works because you subset month.days to return only those values for which month.days equals 31, and then you retrieve the names of the resulting vector.
The double equal sign (==) indicates a test for equality. Make sure not to use the single equal sign (=) for equality testing. Not only will a single equal sign not work, but it can have strange side effects because R interprets a single equal sign as an assignment. In other words, the operator = in many cases is the same as <-.