Named List in R Programming

A list is an object in R Language which consists of heterogeneous elements. A list can even contain matrices, data frames, or functions as its elements. The list can be created using list() function in R. Named list is also created with the same function by specifying the names of the elements to access them. Named list can also be created using names() function to specify the names of elements after defining the list. In this article, we’ll learn to create named list in R using two different methods and different operations that can be performed on named lists.

Syntax: names(x) <- value

Parameters:
x: represents an R object
value: represents names that has to be given to elements of x object

Creating a Named List

A Named list can be created by two methods. The first one is by allocating the names to the elements while defining the list and another method is by using names() function.

Example 1:
In this example, we are going to create a named list without using names() function.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a list with names
x <- list(mt = matrix(1:6, nrow = 2),
          lt = letters[1:8],
          n = c(1:10))
  
# Print whole list
cat("Whole List:\n")
print(x)

chevron_right


Output:

Whole List:
$mt
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

$lt
[1] "a" "b" "c" "d" "e" "f" "g" "h"

$n
 [1]  1  2  3  4  5  6  7  8  9 10

Example 2:
In this example, we are going to define the names of elements of the list using names() function after defining the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining list
x <- list(matrix(1:6, nrow = 2),
          letters[1:8],
          c(1:10))
  
# Print whole list
cat("Whole list:\n")
print(x)

chevron_right


Output:

Whole list:
[[1]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[[2]]
[1] "a" "b" "c" "d" "e" "f" "g" "h"

[[3]]
 [1]  1  2  3  4  5  6  7  8  9 10

Accessing components of Named List

Components of a named list can be easily accessed by $ operator.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a list with names
x <- list(mt = matrix(1:6, nrow = 2),
          lt = letters[1:8],
          n = c(1:10))
  
# Print list elements using the names given
# Prints element of the list named "mt"
cat("Element named 'mt':\n")
print(x$mt)
cat("\n")
  
# Print element of the list named "n"
cat("Element named 'n':\n")
print(x$n)

chevron_right


Output:

Element named 'mt':
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Element named 'n':
 [1]  1  2  3  4  5  6  7  8  9 10

Modifying components of Named List

Components of named list can be modified by assigning new values to them.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a named list
lt <- list(a = 1,
           let = letters[1:8],
           mt = matrix(1:6, nrow = 2))
  
cat("List before modifying:\n")
print(lt)
  
# Modifying element named 'a'
lt$a <- 5
  
cat("List after modifying:\n")
print(lt)

chevron_right


Output:

List before modifying:
$a
[1] 1

$let
[1] "a" "b" "c" "d" "e" "f" "g" "h"

$mt
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

List after modifying:
$a
[1] 5

$let
[1] "a" "b" "c" "d" "e" "f" "g" "h"

$mt
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Deleting components from Named List

To delete elements from named list, we’ll use within() function and the result will be assigned to the named list itself.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a named list
lt <- list(a = 1,
           let = letters[1:8],
           mt = matrix(1:6, nrow = 2))
  
cat("List before deleting:\n")
print(lt)
  
# Modifying element named 'a'
lt <- within(lt, rm(a))
  
cat("List after deleting:\n")
print(lt)

chevron_right


Output:

List before deleting:
$a
[1] 1

$let
[1] "a" "b" "c" "d" "e" "f" "g" "h"

$mt
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

List after deleting:
$let
[1] "a" "b" "c" "d" "e" "f" "g" "h"

$mt
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.