How to Create a Named List in R (With Example)


When creating lists in R, you can provide names to each of the elements in the list so that you can extract specific elements from the list by name instead of by position.

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

#create list
my_list <- list(1, 2, 3)

#give names to elements of list
names(my_list) <- c('A', 'B', 'C') 

This particular example uses the list() function to first create a list named my_list with three elements.

Then, it uses the names() function to provide names for each element in the list.

Note that the number of names provided by the names() function should match the number of elements in the list or else some list elements will have NA as the name.

The following example shows how to create a named list in R in practice.

Example: How to Create a Named List in R

Suppose that we create a list named my_list that contains five elements:

#create list
my_list<- list(1, 2, 3, 4, 5)

#view list
my_list
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

Notice that the list currently has no names and each element in the list simply has an index position represented by an integer.

We can use the names() function with the following syntax to give each element in the list a specific name instead:

#create list
my_list<- list(1, 2, 3, 4, 5)

#give names to elements of list
names(my_list) <- c('A', 'B', 'C', 'D', 'E')

#view list
my_list

$A
[1] 1

$B
[1] 2

$C
[1] 3

$D
[1] 4

$E
[1] 5

We can see that each element in the list now has a specific letter name.

Once we have given each element a name, we can then extract the value from a specific element by referencing its name.

For example, we can use the following syntax to access the element in the list named C:

#access element in list named 'C'
my_list$C
[1] 3

This returns the value 3, which represents the value stored in the element of the list named C.

Note that we can also use subset notation to extract specific elements from a named list.

For example, suppose we create the following list that has multiple values in each element of the list:

#create list
my_list<- list(c(1, 2, 3), c(4, 5))

#give names to elements of list
names(my_list) <- c('A', 'B')

#view list
my_list

$A
[1] 1 2 3

$B
[1] 4 5

Now suppose that we would like to retrieve the second value stored in the element named B in the list.

We can use the following syntax to do so:

#retrieve second value of element named 'B' in list
my_list$B[2]

[1] 5

Notice that this returns the second value stored in the element named B in the list, which turns out to be 5.

We can even use the following syntax to retrieve both the first and second values stored in the element named B in the list:

#retrieve first and second values of element named 'B' in list
my_list$B[1:2]

[1] 4 5

Feel free to use similar syntax to retrieve any values that you would like from your 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 Create a List of Lists in R

Leave a Reply

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