Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Operations on Lists in R Programming

  • Last Updated : 25 Aug, 2020

Lists in R language, are the objects which comprise elements of diverse types like numbers, strings, logical values, vectors, list within a list and also matrix and function as its element.

A list is generated using list() function. It is basically a generic vector that contains different objects. R allows its users to perform various operations on lists which can be used to illustrate the data in different forms.

Creating a List

Lists in R can be created by placing the sequence inside the list() function.

R




# Creating a list.
Geek_list <- list("Geek", "RList”, c(65, 21, 80), TRUE, 27.02, 10.3)
print(Geek_list)

Output:



[[1]]
[1] "Geek"

[[2]]
[1] "RList"

[[3]]
[1] 65 21 80

[[4]]
[1] TRUE

[[5]]
[1] 27.02

[[6]]
[1] 10.3

Naming the elements of a list

Name can be assigned to the elements of the list and those names can be used to access the elements.

R




# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector"
                      "This_is_a_Matrix"
                      "This_is_a_listwithin_the_list")
   
# Printing the list
print(Geek_list)

Output:

$This_is_a_vector
[1] "Geeks" "For"   "Geeks"

$This_is_a_Matrix
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"

$This_is_a_listwithin_the_list[[2]]
[1] 12.3

Accessing elements of a List

In order to access the list elements, use the index number, and in case of named list, elements can be accessed using its name also.

R




# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector"
                      "This_is_a_Matrix"
                      "This_is_a_listwithin_the_list")
  
# To access the first element of the list.      
print(Geek_list[1])
    
#  To access the third element.      
print(Geek_list[3])
     
# To access the list element using the name of the element.      
print(Geek_list$This_is_a_Matrix)  

Output:

$This_is_a_vector
[1] "Geeks" "For"   "Geeks"

$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"

$This_is_a_listwithin_the_list[[2]]
[1] 12.3


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

Adding, Deleting, and Updating elements of a list

In R, a new element can be added to the list, the existing element can be deleted or updated.

R






# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector"
                      "This_is_a_Matrix"
                      "This_is_a_listwithin_the_list")
  
#  To add a new element.
Geek_list[4] <- "New element"
print(Geek_list)
    
# To remove the last element.     
Geek_list[4] <- NULL
    
# To print the 4th Element.     
print(Geek_list[4])
     
# To update the 3rd Element.     
Geek_list[3] <- "updated element"
print(Geek_list[3])

Output:

$This_is_a_vector
[1] "Geeks" "For"   "Geeks"

$This_is_a_Matrix
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"

$This_is_a_listwithin_the_list[[2]]
[1] 12.3


[[4]]
[1] "New element"

$
NULL

$This_is_a_listwithin_the_list
[1] "updated element"

Merging elements of a List

Many lists can be merged in one list by which all the list elements are placed inside one list.

R




# Firstly, create two lists.     
list1 <- list(1, 2, 3, 4, 5, 6, 7)
list2 <- list("Geeks", "For", "Geeks")
    
# Then to merge these two lists.     
merged_list <- c(list1, list2)
print(merged_list)

Output:

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6

[[7]]
[1] 7

[[8]]
[1] "Geeks"

[[9]]
[1] "For"

[[10]]
[1] "Geeks"

Converting a list to vector

In order to perform arithmetic operations, lists should be converted to vectors using unlist() function.

R




# Firstly, create lists.     
list1 <- list(1:5)
print(list1)
list2 <-list(11:15)
print(list2)
  
# Now,  convert the lists to vectors.      
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
    
# Now add the vectors    
result_vector <- v1+v2
print(result_vector)

Output:

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

[[1]]
[1] 11 12 13 14 15

[1] 1 2 3 4 5
[1] 11 12 13 14 15
[1] 12 14 16 18 20



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!