The next topic I learned about in the DataQuest Data Analyst in R track is a list. I decided that in honor of music’s biggest night, also known the Grammys that took place last night, the examples in this post are going to be music focused. So without further ado, let’s get it started!
DataQuest defines lists as specialized vectors that contain multiple types of objects. These objects can be different data structures including single data elements, vectors, and matrices. So why lists? Storing objects in lists allow me to make use of some of R’s features for performing the same operation on each object in the list. The list is created using the list() function, like so:
Note that the numbers one through seven tell you the order of the objects stored in this list.
To give you a better idea of how lists in R work, I’ll give another example. In this example, I have some data gathered for the Grammys. I want to store this information in a list.
I call this element music_event which will give me a list that looks like this.
Similar to naming elements in a vector, I can assign names to objects in a list using the names() function.
Once again, I’ll call music_event and my list’s elements will have names.
If you call the names() function in a list without assigned names, the function will return NULL. This also applies to vectors.
Just as I did with vectors and matrices, I can also index lists. DataQuest tells me there are two different indexing operations used on lists:
- single brackets to return a list of selected elements 
- double brackets to return a single element []
For example, I want to extract the third object in my list. I would type the following:
Note that in this example, I use the typeof() function. This function allows me to check the data type of an object. When I checked the data type for this object, I see that it is a list.
I could also use the following methods to return a list of selected items.
Now I’m going to use double brackets to return a single element. Again, I want extract the third object in my list. I’ll do the following:
Note that when using the typeof() function to check the data type of this object, I see that it is a character data type.
I could also use the following ways to return a single element.
To return a value contained in a list element, I could do this:
Modifying List Elements
I can index lists to change specific list elements. Recall the element in music_event_title:
As you can see the “The 2020 Grammys” is the element in music_event_title. I want to change this element to the official title of the Grammys ceremony. I would write it like this:
I would first index the element I want to replace, then type its replacement. When I called the music_event list, you can see that the title has changed.
Let’s do another example. Let’s look at the performances element.
Let’s say I wanted to replace Demi Lovato with H.E.R. I would write the following:
Adding Elements to a List
I could also add elements to a list. Let’s say I wanted to add an historic event that took place at last night’s Grammys. I can create an element called music_event_history add it to my list. When I call the music_event list, the new element is added.
I could also do it this way and get the same result.
I can combine lists too. Let’s recall the two lists I made earlier.
When I combine the lists, I can see that the lists objects are combined into one list.
Finally for this post, I’ll cover nested lists. Nested Lists are lists inside of lists. Let’s take the previous example. What if instead of combining lists, I want to create a lists of lists. I would write:
This resulting list contains my two lists, music_genres and music_events as objects.
This concludes my lesson in lists in R! I had a lot of fun using Grammy data for this post. Until next time..