18 C
London
Thursday, July 24, 2025
HomeStatistics TutorialRHow to Create a List of Lists in R (With Example)

How to Create a List of Lists in R (With Example)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to create a list of lists in R:

#define lists
list1 #create list of lists
list_of_lists 

The following example shows how to use this syntax in practice.

Example: Create List of Lists in R

The following code shows how to create a list that contains 3 lists in R:

#define lists
list1 #create list of lists
list_of_lists #view the list of lists
list_of_lists

[[1]]
[[1]]$a
[1] 5

[[1]]$b
[1] 3


[[2]]
[[2]]$c
[1] "A"

[[2]]$d
[1] "B" "C"


[[3]]
[[3]]$e
[1] 20  5  8 16

We can then use single brackets [ ] to access a specific list.

For example, we can use the following syntax to access the second list:

#access second list
list_of_lists[2]

[[1]]
[[1]]$c
[1] "A"

[[1]]$d
[1] "B" "C"

We can also use double brackets [[ ]] and the dollar sign operator $ to access a specific element within a specific list.

For example, we can use the following syntax to access element d within the second list:

#access element 'd' within second list
list_of_lists[[2]]$d

[1] "B" "C"

You can use similar syntax to access any element within any list.

Additional Resources

The following tutorials explain how to perform other common tasks with lists in R:

How to Convert a List to a Data Frame in R
How to Append Values to List in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories