R tutorial – How to Create & Name Lists in R

In this introduction to R course you will learn about the basics of R, as well as the most common data structures it uses to store data

Join DataCamp today, and start our interactive intro to R programming tutorial for free: https://www.datacamp.com/courses/free-introduction-to-r

Your R skills are growing by the minute, I can feel it! The most important data structures that we’ve covered up to now are vectors and matrices. Remember that the vector is a one dimensional array that can only hold elements of the same type. Similarly, matrices can only hold elements of the same type, but this time they’re in a two-dimensional array.

Vectors and matrices are great, but there are cases in which you want to store different data types in the same data structures. This is where lists come in. A list can contain all kinds of R objects, such as vectors and matrices, but also other R objects, such as dates, data frames, factors and many more. All of this can be stored in a single list without R having to perform coercion to enforce the same type. That’s pretty cool right? Because lists can contain practically anything you can think of in R terms, you do lose some functionality that vectors and matrices offered. Most importantly, performing calculus with lists is far less straightforward because there’s no predefined structure that lists have to follow.

Enough for the theory, let’s build some lists! Suppose that as a music artist on the rise to fame and fortune, you regularly record some new songs, and keep some details for each of these songs. Your latest creation is called “Rsome times”, is 190 seconds long and should be the 5th number on your record.

Trying to store this information in a vector using the `c()` function, …, Inevitably leads to coercion. However, you can also store this information in a list, using the `list()` function

This time, all the elements have kept their original type. The printout is pretty different from what you’re used to. We can see that the first element in the list is the string “Rsome times” for example, which actually is a character vector. To continue working with this list we’ll store the list in a new variable, `song`:

We can assert that this `song` variable is a list using the is-dot-list function:

Now, storing the song information like this, without any names, is not really clear, so let’s assign some labels with the names() function. To assign the names, you still use a character vector, even though we’re working with lists now:

Printing `song` again shows that the indices in double square brackets have changed to the names of the list elements, this looks much nicer! As was the case with vectors, you can also directly specify the names in a list at the time of creation. To create the exact same variable `songs`, you can use this command

You’ve already figured out that the standard way of printing the contents of a list is pretty bulky. I suggest you use the `str()` function for this: this function compactly displays the structure of the `song` list:

As I told you before, lists can contain practically anything. They can even contain other lists! Suppose you want to add a list containing the title and duration of a very similar but less catchy song that you’ve also recorded in the last weeks. Let’s first create a list to contain this information, `similar_song`

We can now create the `song` list again, as follows:

The structure of this list reveals that it is perfectly possible to store lists inside lists:

If you want to go totally crazy, you can even store a list inside a list and then store that list in another list, but let’s not make ourselves dizzy here. It’s time to work your way around some of the interactive exercises before I introduce some techniques to subset and extend lists. Have fun!

Simon says:

Data structure in R is why I hate R https://www.refsmmat.com/posts/2016-09-12-r-lists.html

Sarbajit Ghosh says:

I am trying to create a list of 100 vectors, length of each vector is 15 (each vector is a random sample of size 15 generated from uniform(0,1)). how to proceed?