Nested lists let you store lists inside other lists. This helps organize related data in a simple way.
Nested lists in R Programming
nested_list <- list( element1, list( subelement1, subelement2 ), element3 )
You create a nested list by putting a list inside another list.
Use list() to create lists in R.
empty_nested_list <- list()single_element_nested_list <- list(list(42))
mixed_nested_list <- list( "name" = "Alice", "scores" = list(90, 85, 88), "passed" = TRUE )
deep_nested_list <- list( list( list(1, 2), list(3, 4) ), 5 )
This program creates a nested list with a person's info and scores. It prints the scores, adds a new score, then prints the updated scores.
nested_list <- list( "person" = list( "name" = "Bob", "age" = 30 ), "scores" = c(95, 88, 92), "passed" = TRUE ) print("Before adding new score:") print(nested_list$scores) # Add a new score nested_list$scores <- c(nested_list$scores, 100) print("After adding new score:") print(nested_list$scores)
Access nested list elements using the $ operator or double square brackets [[ ]].
Time complexity to access an element is usually fast, but depends on depth.
Common mistake: forgetting to use list() inside list() to nest properly.
Use nested lists when you need to group related data hierarchically instead of flat vectors.
Nested lists store lists inside other lists to organize complex data.
Use list() to create nested lists and access elements with $ or [[ ]].
They help keep related data grouped and easy to manage.
Practice
Solution
Step 1: Understand list structure in R
Lists in R can hold different types of elements, including other lists.Step 2: Define nested list
A nested list is a list where some elements are themselves lists, allowing complex data grouping.Final Answer:
A list that contains other lists as elements -> Option BQuick Check:
Nested list = list inside list [OK]
- Confusing nested lists with vectors
- Thinking nested lists must be numeric
- Mixing up matrices and lists
Solution
Step 1: Identify list creation syntax
Use list() to create lists; nested lists are lists inside list() calls.Step 2: Check each option
nested <- list(a = 1, b = list(c = 2, d = 3)) uses list() with a nested list inside, which is correct syntax for nested lists.Final Answer:
nested <- list(a = 1, b = list(c = 2, d = 3)) -> Option AQuick Check:
list() with list() inside = nested list [OK]
- Using c() instead of list() for nested lists
- Confusing data.frame or matrix with lists
- Missing nested list syntax
nested <- list(x = 10, y = list(a = 5, b = 15)) nested$y$a
Solution
Step 1: Understand list structure
nested is a list with element y, which is itself a list containing a=5 and b=15.Step 2: Access nested element
nested$y$a accesses element 'a' inside the nested list 'y', which is 5.Final Answer:
5 -> Option DQuick Check:
nested$y$a = 5 [OK]
- Accessing nested elements with single brackets
- Confusing element names
- Expecting the outer element value
nested <- list(a = 1, b = list(c = 2, d = 3)) value <- nested$b[c]
Solution
Step 1: Understand list element access
To access a single element by name in a list, use double brackets [[ ]] or $; single brackets [ ] return a sublist.Step 2: Analyze the code
nested$b[c] uses single brackets with a character index, which returns a sublist, not the element value.Final Answer:
Using single brackets instead of double brackets to access list element -> Option CQuick Check:
Use [[ ]] or $ for single element access [OK]
- Using [ ] returns sublist, not element
- Confusing list and vector indexing
- Forgetting to use quotes for names
data <- list(group1 = list(scores = c(10, 20)), group2 = list(scores = c(15, 25))), how do you create a new list with the average score of each group?Solution
Step 1: Access nested scores vectors
Use data$group1$scores and data$group2$scores to get numeric vectors for each group.Step 2: Calculate mean for each group
Apply mean() function to each scores vector to get average scores.Step 3: Create new list with averages
Assign the calculated means to a new list with same group names.Final Answer:
averages <- list(group1 = mean(data$group1$scores), group2 = mean(data$group2$scores)) -> Option AQuick Check:
mean() on nested scores = averages list [OK]
- Using sum() instead of mean()
- Accessing scores incorrectly
- Confusing length with average
Ready to prove it?
Pass the test and this pattern moves unknown
