--6-Lists
#1-Lists, why would you need them?
#Within a column all elements have the same data type,
#but different columns can be of different data type.#2-Lists, why would you need them? (2)
#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#3-Creating a list
#Construct a list, named my_list, that contains
#the variables my_vector,
#my_matrix and my_df as list components.
# Vector with numerics from 1 up to 10
my_vector <- 1:10
# Matrix with numerics from 1 up to 9
my_matrix <- matrix(1:9, ncol = 3)
# First 10 elements of the built-in data frame mtcars
my_df <- mtcars[1:10,]
# Construct list with these different elements:
my_list <- list(my_vector,my_matrix,my_df)
my_list#4-Creating a named list
#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.
5-Creating a named list (2)
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).
#6-Selecting elements from a list
Select from
shining_listthe vector representing the actors. Simply print out this vector.Select from
shining_listthe second element in the vector representing the actors. Do a printout like before.
#7-Adding more movie information to the list
Complete the code below such that an item named
yearis added to theshining_listwith the value 1980. Assign the result toshining_list_full.Finally, have a look at the structure of
shining_list_fullwith thestr()function.
Last updated