R – Lists

A list in R is a generic object consisting of an ordered collection of objects. Lists are one-dimensional, heterogeneous data structures. The list can be a list of vectors, a list of matrices, a list of characters and a list of functions, and so on.

A list is a vector but with heterogeneous data elements. A list in R is created with the use of list() function. R allows accessing elements of a list with the use of the index value. In R, indexing of a list starts with 1 instead of 0 like other programming languages.

Creating a List

To create a List in R you need to use the function called “list()”. In other words, a list is a generic vector containing other objects. To illustrate how a list looks, we take an example here. We want to build a list of employees with the details. So for this, we want attributes such as ID, employee name, and the number of employees.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to create a List
   
# The first attributes is a numeric vector 
# containing the employee IDs which is created 
# using the command here
empId = c(1, 2, 3, 4)
   
# The second attribute is the employee name 
# which is created using this line of code here
# which is the character vector 
empName = c("Debi", "Sandeep", "Subham", "Shiba")
   
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
   
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
   
print(empList)

chevron_right


Output:



[[1]]
[1] 1 2 3 4

[[2]]
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

[[3]]
[1] 4

Accessing components of a list

We can access components of a list in two ways.

  • Access components by names: All the components of a list can be named and we can use those names to access the components of the list using the dollar command.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # R program to access
    # components of a list
      
    # Creating a list by naming all its components
    empId = c(1, 2, 3, 4)
    empName = c("Debi", "Sandeep", "Subham", "Shiba")
    numberOfEmp = 4
    empList = list(
      "ID" = empId,
      "Names" = empName,
      "Total Staff" = numberOfEmp
      )
    print(empList)
      
    # Accessin components by names
    cat("Acessing name components using $ command\n")
    print(empList$Names)

    chevron_right

    
    

    Output:

    $ID
    [1] 1 2 3 4
    
    $Names
    [1] "Debi"    "Sandeep" "Subham"  "Shiba"  
    
    $`Total Staff`
    [1] 4
    
    Acessing name components using $ command
    [1] "Debi"    "Sandeep" "Subham"  "Shiba"  
    
  • Access components by indices: We can also access the components of the list using indices. To access the top-level components of a list we have to use a double slicing operator “[[ ]]” which is two square brackets and if we want to access the lower or inner level components of a list we have to use another square bracket “[ ]” along with the double slicing operator “[[ ]]“.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # R program to access 
    # components of a list
      
    # Creating a list by naming all its components
    empId = c(1, 2, 3, 4)
    empName = c("Debi", "Sandeep", "Subham", "Shiba")
    numberOfEmp = 4
    empList = list(
      "ID" = empId,
      "Names" = empName,
      "Total Staff" = numberOfEmp
      )
    print(empList)
      
    # Accessing a top level components by indices
    cat("Accessing name components using indices\n")
    print(empList[[2]])
      
    # Accessing a inner level components by indices
    cat("Accessing Sandeep from name using indices\n")
    print(empList[[2]][2])
      
    # Accessing another inner level components by indices
    cat("Accessing 4 from ID using indices\n")
    print(empList[[1]][4])

    chevron_right

    
    

    Output:

    $ID
    [1] 1 2 3 4
    
    $Names
    [1] "Debi"    "Sandeep" "Subham"  "Shiba"  
    
    $`Total Staff`
    [1] 4
    
    Accessing name components using indices
    [1] "Debi"    "Sandeep" "Subham"  "Shiba"  
    Accessing Sandeep from name using indices
    [1] "Sandeep"
    Accessing 4 from ID using indices
    [1] 4
    

Modifying components of a list

A list can also be modified by accessing the components and replacing them with the ones which you want.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to edit 
# components of a list
  
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
  "ID" = empId,
  "Names" = empName,
  "Total Staff" = numberOfEmp
)
cat("Before modifying the list\n")
print(empList)
  
# Modifying the top-level component
empList$`Total Staff` = 5
  
# Modifying inner level componet
empList[[1]][5] = 5
empList[[2]][5] = "Kamala"
  
cat("After modified the list\n")
print(empList)

chevron_right


Output:



Before modifying the list
$ID
[1] 1 2 3 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

$`Total Staff`
[1] 4

After modified the list
$ID
[1] 1 2 3 4 5

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"   "Kamala" 

$`Total Staff`
[1] 5

Concatenation of lists

Two lists can be concatenated using the concatenation function. So, when we want to concatenate two lists we have to use the concatenation operator.

Syntax:

list = c(list, list1)

list = the original list
list1 = the new list

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to edit 
# components of a list
  
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
  "ID" = empId,
  "Names" = empName,
  "Total Staff" = numberOfEmp
)
cat("Before concatenation of the new list\n")
print(empList)
  
# Creating another list
empAge = c(34, 23, 18, 45)
empAgeList = list(
  "Age" = empAge
)
  
# Concatenation of list using concatenation operator
empList = c(empList, empAgeList)
  
cat("After concatenation of the new list\n")
print(empList)

chevron_right


Output:

Before concatenation of the new list
$ID
[1] 1 2 3 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

$`Total Staff`
[1] 4

After concatenation of the new list
$ID
[1] 1 2 3 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

$`Total Staff`
[1] 4

$Age
[1] 34 23 18 45

Deleting components of a list

To delete components of a list, first of all, we need to access those components and then insert a negative sign before those components. It indicates that we had to delete that component.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to access 
# components of a list
  
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
  "ID" = empId,
  "Names" = empName,
  "Total Staff" = numberOfEmp
)
cat("Before deletion the list is\n")
print(empList)
  
# Deleting a top level components
cat("After Deleting Total staff components\n")
print(empList[-3])
  
# Deleting a inner level components
cat("After Deleting sandeep from name\n")
print(empList[[2]][-2])

chevron_right


Output:

Before deletion the list is
$ID
[1] 1 2 3 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

$`Total Staff`
[1] 4

After Deleting Total staff components
$ID
[1] 1 2 3 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

After Deleting sandeep from name
[1] "Debi"   "Subham" "Shiba" 



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.