R tutorial - How to Create & Name Lists in R

Learn how to create and name lists in R, a flexible data structure that holds different types of objects unlike vectors and matrices. The tutorial covers the list() function, the names() function for labeling elements, and nesting lists inside each other using a music-song example.
Chapters
Your R scales 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 a vector is a one dimensional array that can only hold elements of the same type. Similarly, matrices can also 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 structure. 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, vectors, and many more. All of this can be stored in a single list without R having to perform coercion to enforce the same type of. 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 you regularly record some new songs and keep some details for each of these songs. Your latest creation is called Rsometimes. It's 190 seconds long and should be the fifth 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 also pretty different from what you're used to. We can see that the first element in the list is the string rsometimes, 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 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 song, 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 the duration of a very similar but less catchy song that you've also recorded in the last weeks. Let's first create a list containing this similarsong. 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 start 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 you to some techniques to subset and extend lists. Have fun.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







