Recall & Review
beginner
What is a list in R?
A list in R is a collection that can hold elements of different types and sizes, like numbers, strings, or even other lists.
Click to reveal answer
beginner
What is a vector in R?
A vector in R is a sequence of elements of the same type, like all numbers or all characters, stored in a single structure.
Click to reveal answer
beginner
How do you convert a list to a vector in R?
You can use the function
unlist() to flatten a list into a vector by combining all elements into one sequence.Click to reveal answer
intermediate
What happens if the list contains elements of different types when using
unlist()?R will coerce all elements to a common type, usually converting everything to character if types differ, to create a single vector.
Click to reveal answer
beginner
Write the R code to convert the list
my_list <- list(1, 2, 3) to a vector.Use
my_vector <- unlist(my_list). This will create a numeric vector c(1, 2, 3).Click to reveal answer
Which R function converts a list to a vector?
✗ Incorrect
The function
unlist() flattens a list into a vector.What type of elements does a vector in R contain?
✗ Incorrect
Vectors contain elements all of the same type, such as all numbers or all characters.
If a list contains numbers and characters, what will
unlist() produce?✗ Incorrect
R coerces all elements to a common type, usually character, when types differ.
What does the following code return?
unlist(list(1, 2, 3))✗ Incorrect
The list is flattened into a numeric vector with elements 1, 2, 3.
Which of these is NOT true about
unlist()?✗ Incorrect
unlist() does not preserve the list structure; it flattens it.Explain how to convert a list to a vector in R and what happens if the list has mixed types.
Think about how R handles different data types when combining.
You got /4 concepts.
Describe the difference between a list and a vector in R with examples.
Consider how you store different items in a shopping bag vs. a row of identical boxes.
You got /4 concepts.