Lists, as opposed to vectors, can hold components of different types, just like your to-do list at home or at work. This intro to R chapter will teach you how to create, name and subset these lists.
Lists, why would you need them?
Lists, why would you need them? (2)
Creating a list
Creating a named list
Creating a named list (2)
Selecting elements from a list
Adding more movie information to the list
Lists, why would you need them? Congratulations! At this point in the course you are already familiar with:
Vectors (one dimensional array): can hold numeric, character or logical values. The elements in a vector all have the same data type. Matrices (two dimensional array): can hold numeric, character or logical values. The elements in a matrix all have the same data type. Data frames (two-dimensional objects): can hold numeric, character or logical values. Within a column all elements have the same data type, but different columns can be of different data type. Pretty sweet for an R newbie, right? ;-)
INSTRUCTIONS 70 XP Click ‘Submit Answer’ to start learning everything about lists!
Show Answer (-70 XP) HINT Just click the ‘Submit Answer’ button.Lists, why would you need them? (2) A list in R is similar to your to-do list at work or school: the different items on that list most likely differ in length, characteristic, and type of activity that has to be done.
A list in R allows you to gather a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other in any way.
You could say that a list is some kind super data type: you can store practically any piece of information in it!
INSTRUCTIONS 70 XP Click ‘Submit Answer’ to start the first exercise on lists.
Show Answer (-70 XP) HINT Click ‘Submit Answer’ to start the first exercise on lists.Creating a list Let us create our first list! To construct a list you use the function list():
my_list <- list(comp1, comp2 …) The arguments to the list function are the list components. Remember, these components can be matrices, vectors, other lists, …
INSTRUCTIONS 70 XP Construct a list, named my_list, that contains the variables my_vector, my_matrix and my_df as list components.
Show Answer (-70 XP) HINT Use the list() function with my_vector, my_matrix and my_df as arguments separated by a comma.Creating a named list Well done, you’re on a roll!
Just like on your to-do list, you want to avoid not knowing or remembering what the components of your list stand for. That is why you should give names to them:
my_list <- list(name1 = your_comp1, name2 = your_comp2) This creates a list with components that are named name1, name2, and so on. If you want to name your lists after you’ve created them, you can use the names() function as you did with vectors. The following commands are fully equivalent to the assignment above:
my_list <- list(your_comp1, your_comp2) names(my_list) <- c(“name1”, “name2”) INSTRUCTIONS 70 XP Change the code of the previous exercise (see editor) by adding names to the components. Use for my_vector the name vec, for my_matrix the name mat and for my_df the name df. Print out my_list so you can inspect the output. Show Answer (-70 XP) HINT The first method of assigning names to your list components is the easiest. It starts like this:
my_list <- list(vec = my_vector) Add the other two components in a similar fashion.Creating a named list (2) Being a huge movie fan (remember your job at LucasFilms), you decide to start storing information on good movies with the help of lists.
Start by creating a list for the movie “The Shining”. We have already created the variables mov, act and rev in your R workspace. Feel free to check them out in the console.
INSTRUCTIONS 70 XP Complete the code on the right to create shining_list; it contains three elements:
moviename: a character string with the movie title (stored in mov) actors: a vector with the main actors’ names (stored in act) reviews: a data frame that contains some reviews (stored in rev) Do not forget to name the list components accordingly (names are moviename, actors and reviews).
Show Answer (-70 XP) HINT shining_list <- list(moviename = mov) is only part of the solution; it’s your job to also add act and rev to the list, with the appropriate names.Selecting elements from a list Your list will often be built out of numerous elements and components. Therefore, getting a single element, multiple elements, or a component out of it is not always straightforward.
One way to select a component is using the numbered position of that component. For example, to “grab” the first component of shining_list you type
shining_list[[1]] A quick way to check this out is typing it in the console. Important to remember: to select elements from vectors, you use single square brackets: [ ]. Don’t mix them up!
You can also refer to the names of the components, with [[ ]] or with the $ sign. Both will select the data frame representing the reviews:
shining_list[[“reviews”]] shining_list$reviews Besides selecting components, you often need to select specific elements out of these components. For example, with shining_list[[2]][1] you select from the second component, actors (shining_list[[2]]), the first element ([1]). When you type this in the console, you will see the answer is Jack Nicholson.
INSTRUCTIONS 70 XP INSTRUCTIONS 70 XP Select from shining_list the vector representing the actors. Simply print out this vector. Select from shining_list the second element in the vector representing the actors. Do a printout like before. Show Answer (-70 XP) HINT To select the vector representing the actors, you can use \(actors. To select the third element in the vector representing the actors, you use shining_list\)actors[3]. What needs to change to select the second element?Adding more movie information to the list Being proud of your first list, you shared it with the members of your movie hobby club. However, one of the senior members, a guy named M. McDowell, noted that you forgot to add the release year. Given your ambitions to become next year’s president of the club, you decide to add this information to the list.
To conveniently add elements to lists you can use the c() function, that you also used to build vectors:
ext_list <- c(my_list , my_val) This will simply extend the original list, my_list, with the component my_val. This component gets appended to the end of the list. If you want to give the new list item a name, you just add the name as you did before:
ext_list <- c(my_list, my_name = my_val) INSTRUCTIONS 70 XP Complete the code below such that an item named year is added to the shining_list with the value 1980. Assign the result to shining_list_full. Finally, have a look at the structure of shining_list_full with the str() function. Show Answer (-70 XP) HINT Have a look at the example code in the exercise assignment. Maybe this can help you start:
shining_list <- c(shining_list, …) You still have to add some code where the three dots are.