Home » R » Operations on List in R

Operations on List in R

In this article, we will study the various manipulations that can be performed with list-objects in R programming

As we discussed in the previous article, lists are ordered collections of heterogeneous objects. A large number of operations can be performed on list objects, some of which are, 

Adding elements to a list in R

A list object is associated with a length parameter, which suggests the number of components present in it. Components with any data type can also be added to the list. The element can be added at the end of the list by using the indexing operator at the length+1 location and assigning it to the new element that is to be added. 

#declaring list 
list_obj <- list(10i,
                 c(2:-1),
                 factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
#printing list object
print("Initial List Object")
print(list_obj)

#adding an element at the end of the list
list_obj[length(list_obj)+1] <- "Added Element"
#printing list object
print("Final List Object")
print(list_obj)
The code produces the following output : 
[1] "Initial List Object"
[[1]]
[1] 0+10i

[[2]]
[1]  2  1  0 -1

[[3]]
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

[[4]]
[[4]][[1]]
[1] TRUE

[[4]][[2]]
[1] "FoxInfoTech"
[1] "Final List Object"
[[1]]
[1] 0+10i

[[2]]
[1]  2  1  0 -1

[[3]]
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

[[4]]
[[4]][[1]]
[1] TRUE

[[4]][[2]]
[1] "FoxInfoTech"


[[5]]
[1] "Added Element"

Deleting elements from the list

The elements from the list can be deleted by assigning the value at the particular location of the list to be equivalent to NULL. This removes the instance of the element from the list object in R.If we assign the entire list to NULL, the entire instance of the list object is made empty. 

#declaring list 
list_obj <- list(10i,
                 c(2:-1),
                 factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
#printing list object
print("Initial List Object")
print(list_obj)

#deleting the second element from the list 
list_obj[2] <- NULL
#printing list object
print("Final List Object")
print(list_obj)
The code produces the following output : 
[1] "Initial List Object"
[[1]]
[1] 0+10i

[[2]]
[1]  2  1  0 -1

[[3]]
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

[[4]]
[[4]][[1]]
[1] TRUE

[[4]][[2]]
[1] "FoxInfoTech"

[1] "Final List Object"
[[1]]
[1] 0+10i

[[2]]
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

[[3]]
[[3]][[1]]
[1] TRUE

[[3]][[2]]
[1] "FoxInfoTech"

Modifying the elements of the list

Instead of assigning the value to NULL, we assign the desired location in the list to the modified value. This changes that particular component of the list. The length of the list remains the same. 

#declaring list 
list_obj <- list(10i,
                 c(2:-1),
                 factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
#printing list object
print("Initial List Object")
print(list_obj)

#modifying the value of the second element from the list 
list_obj[2] <- "Study R programming"
#printing list object
print("Final List Object")
print(list_obj)
The code produces the following output : 
[1] "Initial List Object"
[[1]]
[1] 0+10i

[[2]]
[1]  2  1  0 -1

[[3]]
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

[[4]]
[[4]][[1]]
[1] TRUE

[[4]][[2]]
[1] "FoxInfoTech"
[1] "Final List Object"
[[1]]
[1] 0+10i

[[2]]
[1] "Study R programming"

[[3]]
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

[[4]]
[[4]][[1]]
[1] TRUE

[[4]][[2]]
[1] "FoxInfoTech"

Merging lists in R

The lists can be merged together using the c() method in R. The elements of the list appear in the order of their occurrence in the method. The input lists may or may not be of varying lengths. 

#declaring list 
list_obj1 <- list(10i,
                 c(2:-1))
list_obj2 <-  list(factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
print("Contents of List 1")
print(list_obj1)
print("Contents of List 2")
print(list_obj2)
#merged list 
merged <- c(list_obj1,list_obj2)
#printing list object
print("merged List Object")
print(merged)
The code produces the following output : 
[1] "Contents of List 1"
[[1]]
[1] 0+10i

[[2]]
[1]  2  1  0 -1

[1] "Contents of List 2"
[[1]]
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

[[2]]
[[2]][[1]]
[1] TRUE

[[2]][[2]]
[1] "FoxInfoTech"

[1] "merged List Object"
[[1]]
[1] 0+10i

[[2]]
[1]  2  1  0 -1

[[3]]
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

[[4]]
[[4]][[1]]
[1] TRUE

[[4]][[2]]
[1] "FoxInfoTech"