It shouldn’t come as a surprise that you create a list in R with the list() function. You can use the list() function in two ways: to create an unnamed list or to create a named list. The difference is small; in both cases, think of a list as a big box filled with a set of bags containing all kinds of different stuff. If these bags are labeled instead of numbered, you have a named list.

Creating an unnamed list

Creating an unnamed list is as easy as using the list() function and putting all the objects you want in that list between the (). You can work with the matrix baskets.team, containing the number of baskets Granny and Geraldine scored this basketball season. If you want to combine this matrix with a character vector indicating which season you’re talking about here, try:

> baskets.list <- list(baskets.team, "2010-2011")

If you look at the object baskets.list, you see the following output:

> baskets.list
[[1]]
     1st 2nd 3rd 4th 5th 6th
Granny   12  4  5  6  9  3
Geraldine  5  4  2  4 12  9
[[2]]
[1] "2010-2011"

The object baskets.list contains two components: the matrix and the season. The numbers between the [[ ]] indicate the “bag number” of each component.

Creating a named list

In order to create a labeled, or named, list, you simply add the labels before the values between the () of the list() function, like this:

> baskets.nlist <- list(scores = baskets.team, season = "2010-2011")

And that shouldn’t surprise you, because data frames are, in fact, a special kind of named list.

If you look at the named list baskets.nlist, you see the following output:

> baskets.nlist
$scores
     1st 2nd 3rd 4th 5th 6th
Granny   12  4  5  6  9  3
Geraldine  5  4  2  4 12  9
$season
[1] "2010-2011"

Now the [[ ]] moved out and made a place for the $ followed by the name of the component. In fact, this begins to look a bit like a data frame.

Data frames are nothing but a special type of named list, so all of these tricks can be applied to data frames as well.

Playing with the names of components

Just as with data frames, you access the names of a list using the names() function, like this:

> names(baskets.nlist)
[1] "scores" "season"

This means that you also can use the names() function to add names to the components or change the names of the components in the list in much the same way you do with data frames.

Getting the number of components

Data frames are lists, so it’s pretty obvious that the number of components in a list is considered the length of that list. So, to know how many components you have in baskets.list, you simply do the following:

> length(baskets.list)
[1] 2

About This Article

This article is from the book:

About the book authors:

Andrie de Vries is a leading R expert and Business Services Director for Revolution Analytics. With over 20 years of experience, he provides consulting and training services in the use of R. Joris Meys is a statistician, R programmer and R lecturer with the faculty of Bio-Engineering at the University of Ghent.

This article can be found in the category: