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

Instruction

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 <- # 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) msg = "Do not remove or change the definition of the variables `my_vector`, `my_matrix` or `my_df`!" test_object("my_vector", undefined_msg = msg, incorrect_msg = msg) test_object("my_matrix", undefined_msg = msg, incorrect_msg = msg) test_object("my_df", undefined_msg = msg, incorrect_msg = msg) test_object("my_list", incorrect_msg = "It looks like `my_list` does not contain the correct elements. Make sure to pass the variables `my_vector`, `my_matrix` and `my_df` to the `list()` function, separated by commas, in this order.") success_msg("Wonderful! Head over to the next exercise.")

Use the list() function with my_vector, my_matrix and my_df as arguments separated by a comma.

Previous: 6-2 | Lists, why would you need them? (2)

Next: 6-4 | Creating a named list

Back to Main