Lists in R

Lists in R

  • R
  • 3 mins read

In this article, we will study the list data structure in R in detail. We will also look into the basic operations on list-objects. 

A list object in R is used to store objects belonging to different data types. A list object is associated with a length attribute, which is used to specify the number of components in it. A list object may contain singular elements, arrays, matrices, vectors, or other lists as its elements. A list object can also be considered to be a generic vector.

Declaring Lists in R

The list in R can be declared and initialized using the list() method. The components of a list are separated by a comma. 

#declaring list 
list_obj <- list(10i,
                 c(2:-1),
                 factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
#printing the list object
print("List object : ")
print(list_obj)
The code produces the following output : 
[1] "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"

Length of list

The length of the list object can be specified by using the length() method in R. It is available in base R. The length() method takes as argument an object. 

#declaring list 
list_obj <- list(10i,
                 c(2:-1),
                 factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
#printing the list object
print("Length of List object : ")
print(length(list_obj))
The code produces the following output : 
[1] "Length of List object : "
[1] 4

Naming List Elements

The elements of the list can be assigned string names. These would help in enhancing the readability of the code. The names() method is assigned to a vector of names, with length equivalent to the length of the list to be named. 

#declaring list 
list_obj <- list(10i,
                 c(2:-1),
                 factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
#assigning names to the list object
names(list_obj) <- c("Component 1","Component 2","Component 3","Component 4")
#printing the list object
print("List object : ")
print(list_obj)
The code produces the following output  :
[1] "List object : "
$`Component 1`
[1] 0+10i

$`Component 2`
[1]  2  1  0 -1

$`Component 3`
[1] MALE   FEMALE MALE  
Levels: FEMALE MALE

$`Component 4`
$`Component 4`[[1]]
[1] TRUE

$`Component 4`[[2]]
[1] "FoxInfoTech"

Accessing components of a list

The indexing operator is used to access the specific components of the list. The components of the list can be retrieved using the [ ] operator. The integer specified in the [ ] is displayed as the output. 

#declaring list 
list_obj <- list(10i,
                 c(2:-1),
                 factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
#printing the fourth component of the list
print("Fourth component of List object : ")
print(list_obj[4])
The code produces the following output : 
[1] "List object : "
[[1]]
[[1]][[1]]
[1] TRUE

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

If you wish to access the second component of the fourth component of the list, it can be done by going inside the fourth component first, using the [[ ]] and then accessing the second component by using [ ] further. 

#declaring list 
list_obj <- list(10i,
                 c(2:-1),
                 factor(c("MALE","FEMALE","MALE")),
                 list(TRUE,"FoxInfoTech")
                 )
#printing the fourth component of the list
print("Second component of the Fourth component of List object : ")
print(list_obj[[4]][2])
The code produces the following output : 
[1] "Second component of the Fourth component of List object: "
[[1]]
[1] "FoxInfoTech"