Working With Lists

data science data types programming R

Working with lists in R.

Danielle Brantley https://gist.github.com/danielle-b
01-27-2020

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:

music_genres <- list("jazz", "reggae", "pop", "hip hop", "R&B", "rock", "country")
music_genres
[[1]]
[1] "jazz"

[[2]]
[1] "reggae"

[[3]]
[1] "pop"

[[4]]
[1] "hip hop"

[[5]]
[1] "R&B"

[[6]]
[1] "rock"

[[7]]
[1] "country"

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.

music_event_title <-c("The 2020 Grammys")
music_event_description<-c("Music's biggest night!")
music_event_location<-c("Los Angeles", "Staples Center")
music_event_day<-c("Sunday")
music_event_time <-c("8:00 pm Eastern", "5:00 pm Pacific")
big_categories<-c("Album of the Year", "Song of the Year", "Record of the Year", "Best New Artist")
winners<-c("Billie Ellish", "Nipsey Hussle", "Lizzo", "Tyler the Creator", "Beyonce", "PJ Morton")
performances<-c("Ariana Grande", "Demi Lovato", "Usher","Nipsey Hussle Tribute", "Gary Clark Jr", "Nas", "Lil Nas X" )
most_nominations<-c(12, 11, 10, 9)

music_event_meeting <-rbind(music_event_day, music_event_time)
music_event<-list(music_event_title, music_event_description, music_event_location, music_event_meeting, big_categories, winners, performances, most_nominations)

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

music_event
[[1]]
[1] "The 2020 Grammys"

[[2]]
[1] "Music's biggest night!"

[[3]]
[1] "Los Angeles"    "Staples Center"

[[4]]
                 [,1]              [,2]             
music_event_day  "Sunday"          "Sunday"         
music_event_time "8:00 pm Eastern" "5:00 pm Pacific"

[[5]]
[1] "Album of the Year"  "Song of the Year"   "Record of the Year"
[4] "Best New Artist"   

[[6]]
[1] "Billie Ellish"     "Nipsey Hussle"     "Lizzo"            
[4] "Tyler the Creator" "Beyonce"           "PJ Morton"        

[[7]]
[1] "Ariana Grande"         "Demi Lovato"          
[3] "Usher"                 "Nipsey Hussle Tribute"
[5] "Gary Clark Jr"         "Nas"                  
[7] "Lil Nas X"            

[[8]]
[1] 12 11 10  9

Naming Lists

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

music_event_names<-c("music_event_title", "music_event_description", "music_event_location", "music_event_meeting", "big_categories", "winners", "performances", "most_nominations")
names(music_event)<-music_event_names

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

music_event
$music_event_title
[1] "The 2020 Grammys"

$music_event_description
[1] "Music's biggest night!"

$music_event_location
[1] "Los Angeles"    "Staples Center"

$music_event_meeting
                 [,1]              [,2]             
music_event_day  "Sunday"          "Sunday"         
music_event_time "8:00 pm Eastern" "5:00 pm Pacific"

$big_categories
[1] "Album of the Year"  "Song of the Year"   "Record of the Year"
[4] "Best New Artist"   

$winners
[1] "Billie Ellish"     "Nipsey Hussle"     "Lizzo"            
[4] "Tyler the Creator" "Beyonce"           "PJ Morton"        

$performances
[1] "Ariana Grande"         "Demi Lovato"          
[3] "Usher"                 "Nipsey Hussle Tribute"
[5] "Gary Clark Jr"         "Nas"                  
[7] "Lil Nas X"            

$most_nominations
[1] 12 11 10  9

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:

For example, I want to extract the third object in my list. I would type the following:

music_event[3]
$music_event_location
[1] "Los Angeles"    "Staples Center"
typeof(music_event[3])
[1] "list"

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.

music_event["music_event_location"]
$music_event_location
[1] "Los Angeles"    "Staples Center"
music_event[c(1,3)]
$music_event_title
[1] "The 2020 Grammys"

$music_event_location
[1] "Los Angeles"    "Staples Center"

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:

music_event[[3]]
[1] "Los Angeles"    "Staples Center"
typeof(music_event[[3]])
[1] "character"

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.

music_event[["music_event_location"]]
[1] "Los Angeles"    "Staples Center"
music_event$"music_event_location"
[1] "Los Angeles"    "Staples Center"

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

music_event[[c(3,2)]]
[1] "Staples Center"

Manipulating Lists

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:

music_event[[1]] <- "The 62nd Annual Grammy Awards"
music_event
$music_event_title
[1] "The 62nd Annual Grammy Awards"

$music_event_description
[1] "Music's biggest night!"

$music_event_location
[1] "Los Angeles"    "Staples Center"

$music_event_meeting
                 [,1]              [,2]             
music_event_day  "Sunday"          "Sunday"         
music_event_time "8:00 pm Eastern" "5:00 pm Pacific"

$big_categories
[1] "Album of the Year"  "Song of the Year"   "Record of the Year"
[4] "Best New Artist"   

$winners
[1] "Billie Ellish"     "Nipsey Hussle"     "Lizzo"            
[4] "Tyler the Creator" "Beyonce"           "PJ Morton"        

$performances
[1] "Ariana Grande"         "Demi Lovato"          
[3] "Usher"                 "Nipsey Hussle Tribute"
[5] "Gary Clark Jr"         "Nas"                  
[7] "Lil Nas X"            

$most_nominations
[1] 12 11 10  9

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:

music_event[[c(7.2)]] <- "H.E.R."
music_event[7]
$performances
[1] "H.E.R."

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.

music_event_history <-c("Billie Ellish is the youngest person to take home awards in each of the four major categories.")
music_event[[9]] <- music_event_history
music_event
$music_event_title
[1] "The 62nd Annual Grammy Awards"

$music_event_description
[1] "Music's biggest night!"

$music_event_location
[1] "Los Angeles"    "Staples Center"

$music_event_meeting
                 [,1]              [,2]             
music_event_day  "Sunday"          "Sunday"         
music_event_time "8:00 pm Eastern" "5:00 pm Pacific"

$big_categories
[1] "Album of the Year"  "Song of the Year"   "Record of the Year"
[4] "Best New Artist"   

$winners
[1] "Billie Ellish"     "Nipsey Hussle"     "Lizzo"            
[4] "Tyler the Creator" "Beyonce"           "PJ Morton"        

$performances
[1] "H.E.R."

$most_nominations
[1] 12 11 10  9

[[9]]
[1] "Billie Ellish is the youngest person to take home awards in each of the four major categories."

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

music_event[["music_event_history"]] <-c("Billie Ellish is the youngest person to take home awards in each of the four major categories.")
music_event
$music_event_title
[1] "The 62nd Annual Grammy Awards"

$music_event_description
[1] "Music's biggest night!"

$music_event_location
[1] "Los Angeles"    "Staples Center"

$music_event_meeting
                 [,1]              [,2]             
music_event_day  "Sunday"          "Sunday"         
music_event_time "8:00 pm Eastern" "5:00 pm Pacific"

$big_categories
[1] "Album of the Year"  "Song of the Year"   "Record of the Year"
[4] "Best New Artist"   

$winners
[1] "Billie Ellish"     "Nipsey Hussle"     "Lizzo"            
[4] "Tyler the Creator" "Beyonce"           "PJ Morton"        

$performances
[1] "H.E.R."

$most_nominations
[1] 12 11 10  9

[[9]]
[1] "Billie Ellish is the youngest person to take home awards in each of the four major categories."

$music_event_history
[1] "Billie Ellish is the youngest person to take home awards in each of the four major categories."

Combining Lists

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

music_genres <- list("jazz", "reggae", "pop", "hip hop", "R&B", "rock", "country")

music_event<-list(music_event_title, music_event_description, music_event_location, music_event_meeting, big_categories, winners, performances, most_nominations)

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

music_night <-c(music_genres, music_event)
music_night
[[1]]
[1] "jazz"

[[2]]
[1] "reggae"

[[3]]
[1] "pop"

[[4]]
[1] "hip hop"

[[5]]
[1] "R&B"

[[6]]
[1] "rock"

[[7]]
[1] "country"

[[8]]
[1] "The 2020 Grammys"

[[9]]
[1] "Music's biggest night!"

[[10]]
[1] "Los Angeles"    "Staples Center"

[[11]]
                 [,1]              [,2]             
music_event_day  "Sunday"          "Sunday"         
music_event_time "8:00 pm Eastern" "5:00 pm Pacific"

[[12]]
[1] "Album of the Year"  "Song of the Year"   "Record of the Year"
[4] "Best New Artist"   

[[13]]
[1] "Billie Ellish"     "Nipsey Hussle"     "Lizzo"            
[4] "Tyler the Creator" "Beyonce"           "PJ Morton"        

[[14]]
[1] "Ariana Grande"         "Demi Lovato"          
[3] "Usher"                 "Nipsey Hussle Tribute"
[5] "Gary Clark Jr"         "Nas"                  
[7] "Lil Nas X"            

[[15]]
[1] 12 11 10  9

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:

grammy_night_list <- list(music_genres= music_genres, music_event = music_event)
grammy_night_list
$music_genres
$music_genres[[1]]
[1] "jazz"

$music_genres[[2]]
[1] "reggae"

$music_genres[[3]]
[1] "pop"

$music_genres[[4]]
[1] "hip hop"

$music_genres[[5]]
[1] "R&B"

$music_genres[[6]]
[1] "rock"

$music_genres[[7]]
[1] "country"


$music_event
$music_event[[1]]
[1] "The 2020 Grammys"

$music_event[[2]]
[1] "Music's biggest night!"

$music_event[[3]]
[1] "Los Angeles"    "Staples Center"

$music_event[[4]]
                 [,1]              [,2]             
music_event_day  "Sunday"          "Sunday"         
music_event_time "8:00 pm Eastern" "5:00 pm Pacific"

$music_event[[5]]
[1] "Album of the Year"  "Song of the Year"   "Record of the Year"
[4] "Best New Artist"   

$music_event[[6]]
[1] "Billie Ellish"     "Nipsey Hussle"     "Lizzo"            
[4] "Tyler the Creator" "Beyonce"           "PJ Morton"        

$music_event[[7]]
[1] "Ariana Grande"         "Demi Lovato"          
[3] "Usher"                 "Nipsey Hussle Tribute"
[5] "Gary Clark Jr"         "Nas"                  
[7] "Lil Nas X"            

$music_event[[8]]
[1] 12 11 10  9

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

Citation

For attribution, please cite this work as

Brantley (2020, Jan. 27). Data Sci Dani: Working With Lists. Retrieved from https://datascidani.com/posts/working_with_lists 01-27-20/

BibTeX citation

@misc{brantley2020working,
  author = {Brantley, Danielle},
  title = {Data Sci Dani: Working With Lists},
  url = {https://datascidani.com/posts/working_with_lists 01-27-20/},
  year = {2020}
}