0
0
R Programmingprogramming~20 mins

List to vector conversion in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
List to Vector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of unlist on nested list
What is the output of the following R code?
R Programming
my_list <- list(a = 1:3, b = list(4, 5))
result <- unlist(my_list)
print(result)
A
a1 a2 a3 b1 b2
1  2  3  4  5
B1 2 3 4 5
Clist(1, 2, 3, 4, 5)
DError: cannot unlist nested lists
Attempts:
2 left
💡 Hint
Remember that unlist flattens nested lists and preserves names.
Predict Output
intermediate
2:00remaining
Result of as.vector on a list
What is the output of this R code?
R Programming
my_list <- list(10, 20, 30)
result <- as.vector(my_list)
print(result)
A[1] list(10, 20, 30)
BError: cannot convert list to vector
C[[1]] 10 [[2]] 20 [[3]] 30
D[1] 10 20 30
Attempts:
2 left
💡 Hint
as.vector tries to simplify the list to a vector if possible.
🔧 Debug
advanced
3:00remaining
Why does this unlist produce unexpected output?
Consider this code snippet: my_list <- list(c(1, 2), c('a', 'b')) result <- unlist(my_list) print(result) Why does the output contain characters instead of numbers?
R Programming
my_list <- list(c(1, 2), c('a', 'b'))
result <- unlist(my_list)
print(result)
ABecause the list contains mixed types, unlist throws an error.
BBecause unlist converts all elements to the most flexible type, here character, to hold all values.
CBecause unlist only works on numeric lists and fails silently.
DBecause unlist converts all elements to numeric, but characters cause NA.
Attempts:
2 left
💡 Hint
Think about how R handles vectors with mixed types.
🧠 Conceptual
advanced
2:00remaining
Length of vector after unlist
If you have a list like this: list(1:3, 4:6, 7:9), what is the length of the vector after applying unlist()?
R Programming
my_list <- list(1:3, 4:6, 7:9)
result <- unlist(my_list)
length(result)
A9
B3
C1
DError: length undefined
Attempts:
2 left
💡 Hint
Count all elements inside all sublists combined.
📝 Syntax
expert
3:00remaining
Which code produces a numeric vector from a list of numeric vectors?
Given a list of numeric vectors, which R code correctly converts it into a single numeric vector?
R Programming
my_list <- list(c(1,2), c(3,4), c(5,6))
Aresult <- vector(my_list)
Bresult <- as.vector(my_list)
Cresult <- c(my_list)
Dresult <- unlist(my_list)
Attempts:
2 left
💡 Hint
Check which function flattens nested lists properly.