Basics of Lists

[This article was first published on R for Public Health, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Lists are a data type in R that are perhaps a bit daunting at first, but soon become amazingly useful. They are especially wonderful once you combine them with the powers of the apply() functions. This post will be part 1 of a two-part series on the uses of lists. In this post, we will discuss the basics – how to create lists, manipulate them, describe them, and convert them. In part 2, we’ll see how using lapply() and sapply() on lists can really improve your R coding. Creating a list Let’s start with some basics: what lists are and how to create them. A list is a data structure that can hold any number of any types of other data structures. If you have vector, a dataframe, and a character object, you can put all of those into one list object like so: create three different classes of objects add all three objects to one list using list() function print list list1 We can also turn an object into a list by using the as.list() function. Notice how every element of the vector becomes a different component of the list. coerce vector into a list Manipulating a list We can put names on the components of a list using the names() function. This is useful for extracting components. We could have also named the components when we created the list. See below: name the components of the list list1 could have named them when we created list Extract components from a list (many ways): the first is using the [[ ]] operator (notice two brackets, not just one). Note that we can use the single [ ] operator on a list, but it will return a list rather than the data structure that is the component of the list, which is normally not what we would want to do. See what I mean here: extract 3rd component using -> this returns a *string* list1[[3]] print a list containing the 3rd component -> this returns a *list* list1[3] It is also possible to extract components using the operator or the components name. Again, be careful about the [ ] vs [[ ]] operator in the second way. You need the [[ ]] to return the data structure of the component. extract 3rd component using extract 3rd component using [[ ]] and the name of the component Subsetting a list – use the single [ ] operator and c() to choose the components subset the first and third components list1[c language=”(1,3)”][/c] We can also add a new component to the list or replace a component using the $ or [[ ]] operators. This time I’ll add a linear model to the list (remember we can put anything into a list). add new component to existing list using add a new component to existing list using Finally, we can delete a component of a list by setting it equal to NULL. delete a component of existing list list1 Notice how the 5th component doesn’t have a name because we didn’t assign it one when we added it in. Now if we want to extract the dataframe we have in the list, and just look at it’s first row, we would do list1[[2]][1,]. This code would take the second component in the list using the [[ ]] operator (which is the dataframe) and once it has the dataframe, it subsets the first row and all columns using only the [ ] operator since that is what is used for dataframes (or matrices). For help on subsetting matrices and dataframes, check out [this post]( extract first row of dataframe that is in a list list1[[2]][1,] Describing a list To describe a list, we may want to know the following: the class of the list (which is a list class!) and the class of the first component of the list. describe class of the whole list class(list1) describe the class of the first component of the list class(list1[[1]]) the number of components in the list – use the length function() find out how many components are in the list length(list1) a short summary of each component in the list – use str(). (I take out the model because the output is really long) take out the model from list and then show summary of what’s in the list str(list1) Initializing a list To initialize a list to a certain number of null components, we use the vector function like this: initialize list to have 3 null components and print list2 Converting list into matrix or dataframe Finally, we can convert a list into a matrix, dataframe, or vector in a number of different ways. The first, most basic way is to use unlist(), which just turns the whole list into one long vector: convert to one long string – use unlist unlist(list1) But often we have matrices or dataframes as components of a list and we would like to combind them or stack them into one dataframe. The following shows the two good ways I’ve found to do this (from [this StackOverflow]( page) using ldply() from the plyr package or rbind(). Here, we first create a list of matrices and then convert the list of matrices into a dataframe. create list of matrices and print mat.list convert to data frame 1. use ldply require(plyr) ldply(mat.list, data.frame) 2. use rbind, mat.list) Get ready for part 2 next time, when we’ll get to what we can use lists for and why we should use them at all.

To leave a comment for the author, please follow the link and comment on their blog: R for Public Health. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)