How to Create a List in R (With Examples)


You can create lists in R to hold many different data types in one object.

You can then easily access the values in a list by using subsetting.

Here are the two most common ways to create a list in R:

Method 1: Create a List

#create list
my_list <- list(c('A', 'B', 'C'), 14, 20, 23, 31, 'Thunder', 'Wizards')

This particular example creates a list named my_list that contains a variety of elements with different data types.

Method 2: Create a Named List

#create named list
my_list <- list(teams=c('Thunder', 'Wizards', 'Mavs'), points=c(15, 22, 27), games=50)

This particular example creates a named list called my_list that contains a variety of elements that can be accessed by using the names specified before the equal signs.

The following examples show how to use each of these methods to create a list and then access the various elements inside of the list.

Example 1: Create a List in R

The following code shows how to create a list in R that contains some elements with numeric data types and some elements with character data types:

#create list
my_list <- list(c('A', 'B', 'C'), 14, 20, 23, 31, 'Thunder', 'Wizards')

#view list
my_list

[[1]]
[1] "A" "B" "C"

[[2]]
[1] 14

[[3]]
[1] 20

[[4]]
[1] 23

[[5]]
[1] 31

[[6]]
[1] "Thunder"

[[7]]
[1] "Wizards"

We can see that this list contains 7 total elements.

If we’d like, we can use double brackets to access the values in a specific element.

For example, we can use the following syntax to access the values in the first element of the list:

#access values in first element of list
my_list[[1]]

[1] "A" "B" "C"

This returns each of the values in the first element of the list, which we specified to be a vector with values of “A”, “B” and “C”.

Also note that we can use single brackets to access a specific element inside of this vector.

For example, we can use the following syntax to extract the second value from the first element of the list:

#access second value from first element of list
my_list[[1]][2]

[1] "B"

Notice that this returns “B”, which represents the second value from the first element of this list.

You can use similar syntax to access any value that you would like within the list.

Example 2: Create a Named List in R

The following code shows how to create a named list in R:

#create named list
my_list <- list(teams=c('Thunder', 'Wizards', 'Mavs'), points=c(15, 22, 27), games=50)
#view named list
my_list

$teams
[1] "Thunder" "Wizards" "Mavs"   

$points
[1] 15 22 27

$games
[1] 50

We can see that this list contains 3 total elements and each of the elements have a specific name.

If we’d like, we can use the dollar sign symbol to access a specific element by name:

#access element named 'teams'
my_list$teams

[1] "Thunder" "Wizards" "Mavs"   

This returns each of the values in the element of the list with a name of teams.

We can also use single brackets to access an item in a specific location:

#access item in index position 3 of the element named 'teams'
my_list$teams[3]

[1] "Mavs"

This returns the item in index location 3 of the element with a name of teams.

You can use similar syntax to access any value that you would like within the named 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
How to Convert Data Frame Column to List in R
How to Count Number of Elements in List in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *