0
0
R Programmingprogramming~20 mins

Accessing elements ([], [[]], $) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
R List Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of list element access with single brackets
What is the output of this R code?
my_list <- list(a = 1:3, b = 4:6)
result <- my_list["a"]
print(result)
R Programming
my_list <- list(a = 1:3, b = 4:6)
result <- my_list["a"]
print(result)
AVector 1 2 3
BList of 1 element named 'a' containing 1 2 3
CError: object of type 'closure' is not subsettable
DNULL
Attempts:
2 left
💡 Hint
Remember that single brackets return a sublist, not the element itself.
Predict Output
intermediate
2:00remaining
Output of list element access with double brackets
What is the output of this R code?
my_list <- list(a = 1:3, b = 4:6)
result <- my_list[["a"]]
print(result)
R Programming
my_list <- list(a = 1:3, b = 4:6)
result <- my_list[["a"]]
print(result)
AVector 1 2 3
BList of 1 element named 'a' containing 1 2 3
CError: subscript out of bounds
DNULL
Attempts:
2 left
💡 Hint
Double brackets extract the element itself, not a sublist.
Predict Output
advanced
2:00remaining
Accessing data frame columns with $ operator
What is the output of this R code?
df <- data.frame(x = 1:3, y = 4:6)
result <- df$y
print(result)
R Programming
df <- data.frame(x = 1:3, y = 4:6)
result <- df$y
print(result)
AList with element y containing 4 5 6
BData frame with column y only
CError: object 'y' not found
DVector 4 5 6
Attempts:
2 left
💡 Hint
The $ operator extracts a column as a vector.
Predict Output
advanced
2:00remaining
Difference between single and double brackets on nested list
What is the output of this R code?
nested_list <- list(a = list(b = 10))
result1 <- nested_list["a"]
result2 <- nested_list[["a"]]
print(result1)
print(result2)
R Programming
nested_list <- list(a = list(b = 10))
result1 <- nested_list["a"]
result2 <- nested_list[["a"]]
print(result1)
print(result2)
Aresult1 is a list with element 'a' containing list(b=10); result2 is list(b=10)
Bresult1 is list(b=10); result2 is a list with element 'a' containing list(b=10)
CBoth result1 and result2 are list(b=10)
DBoth result1 and result2 are lists with element 'a' containing list(b=10)
Attempts:
2 left
💡 Hint
Single brackets keep the outer list structure; double brackets extract the inner element.
🧠 Conceptual
expert
3:00remaining
Why does my_list$a and my_list[["a"]] differ from my_list["a"]?
Consider this R list:
my_list <- list(a = 1:3, b = 4:6)

Which statement best explains the difference between my_list$a, my_list[["a"]], and my_list["a"]?
AAll three return the same vector 1 2 3 but with different names.
B<code>my_list$a</code> returns NULL, <code>my_list[["a"]]</code> returns a list, and <code>my_list["a"]</code> returns the vector 1 2 3.
C<code>my_list$a</code> and <code>my_list[["a"]]</code> return the element itself (vector 1 2 3), while <code>my_list["a"]</code> returns a sublist containing that element.
D<code>my_list$a</code> returns a list, <code>my_list[["a"]]</code> returns NULL, and <code>my_list["a"]</code> returns an error.
Attempts:
2 left
💡 Hint
Think about how $ and [[ ]] extract elements versus [ ] returning sublists.