Working With Lists

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:

list of music genres

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.

creating a list in R

I call this element music_event which will give me a list that looks like this.

The result of creating my list.

Naming Lists

Similar to naming elements in a vector, I can assign names to objects in a list using the names() function.

creating names for elements in my list.

Once again, I’ll call music_event and my list’s elements will have names.

Result of calling music_event list.

If you call the names() function in a list without assigned names, the function will return NULL. This also applies to vectors.

Indexing Lists

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:

indexing a list to return selected elements.

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.

other examples of indexing lists to return selected elements.

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:

indexing list to return a single element.

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.

other examples of indexing list to return single elements.
other examples of indexing list to return single elements.

To return a value contained in a list element, I could do this:

Return a value contained in list element.

Manipulating Lists

Modifying List Elements

I can index lists to change specific list elements. Recall the element in music_event_title:

replacing the title element of my list.

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.

The original elements in my performances list.

Let’s say I wanted to replace Demi Lovato with H.E.R. I would write the following:

replaced the list element Demi Lovato with HER.

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.

One way of adding an element to a list.

I could also do it this way and get the same result.

Another way of adding an element to a list.

Combining Lists

I can combine lists too. Let’s recall the two lists I made earlier.

my music_genres list
my music_event list

When I combine the lists, I can see that the lists objects are combined into one list.

Combination of the music_genres and music_event lists.
Combination of the music_genres and music_event lists continued.

Nested Lists

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:

Nested lists with the music_genres and music_event lists.
Nested lists with the music_genres and music_event lists continued.

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..