Lists are the R objects which can contain elements of different types like numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements. List is created using list() function.

C programmers may find it similar to a C struct.

1. Creating a List

Let’s create our first list.

L<-list(name="Pradeep",salary=50000,ratified=T,Subjects_taught=c("C Language","SRP","Data warehousing and Mining"))
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

The component names-“name”,“salary”,“ratified” are called tags . You can create a list even without tags. But,using tags gives good readability.

2.Accessing elements in a list

We can access individual elements in a list by using following ways:

2.1 Accessing elements in a list using $

L$salary # Returns the "name" from the list
## [1] 50000

2.2 Accessing elements in a list using double square brackets

L[["salary"]]
## [1] 50000

2.3 Accessing elements in a list using the index in double square brackets

L[[1]]
## [1] "Pradeep"

2.4 Que: What is the difference between double square brackets [[]] and single square brackets [] for accessing elements in a list?

By using single square brackets [], we get a sublist from a list. and not the element. For example,

L["name"]
## $name
## [1] "Pradeep"
class(L["name"]) # the class will be list
## [1] "list"

In this case, we got a sublist from list L and not the element.

Now, Let’s consider double brackets.In this case, we get the actual element from the list.

L[["name"]]
## [1] "Pradeep"
class(L[["name"]]) # The class will be numeric
## [1] "character"

By using [[]] we can access only single element from the list.

2.5 C multiple elements from the list

L[c(2,4)] # Returns 2nd and 4th elements from the list.
## $salary
## [1] 50000
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

3. Adding and removing elements from the list

We can add, delete and update list elements as shown below. Let’s consider the List “L”.

L<-list(name="Pradeep",salary=50000,ratified=T,Subjects_taught=c("C Language","SRP","Data warehousing and Mining")) # Consider a list
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

3.1 Adding an element to the list

L<-list(name="Pradeep",salary=50000,ratified=T,Subjects_taught=c("C Language","SRP","Data warehousing and Mining")) #Consider a List 
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

Now,Let’s add a column “years_of_experience” to the list L using “$”sign.

L$years_of_experience<-8.6 # Adding years of experiance to the list
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"
## 
## $years_of_experience
## [1] 8.6

3.2 Another way of adding elements to the list

Let’s add a column “Home_town” to list L using double square brackets“[[]]”.

L<-list(name="Pradeep",salary=50000,ratified=T,Subjects_taught=c("C Language","SRP","Data warehousing and Mining")) #Consider a List
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"
L[["Home_town"]]<-"Hyderabad"
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"
## 
## $Home_town
## [1] "Hyderabad"

3.3 Excluding an element from the list

L<-list(name="Pradeep",salary=50000,ratified=T,Subjects_taught=c("C Language","SRP","Data warehousing and Mining")) #Consider a List 
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

Say, You want to exclude ( Not delete ) 2nd element from the list.

L[-2] 
## $name
## [1] "Pradeep"
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

3.4 Removing elements from the list.

L<-list(name="Pradeep",salary=50000,ratified=T,Subjects_taught=c("C Language","SRP","Data warehousing and Mining")) #Consider a List 
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

3.4.1 Removing the elements from the list using []. (Remove the 2nd element)

L[2]<-NULL
L
## $name
## [1] "Pradeep"
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

3.4.2 Removing the elements from the list using $

L$Subjects_taught<-NULL
L
## $name
## [1] "Pradeep"
## 
## $ratified
## [1] TRUE

3.5 Modifying the elements in the list

L<-list(name="Pradeep",salary=50000,ratified=T,Subjects_taught=c("C Language","SRP","Data warehousing and Mining")) #Consider a List 
L
## $name
## [1] "Pradeep"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

Let’s change the name. (Modify the list)

L$name<-"Suresh"
L
## $name
## [1] "Suresh"
## 
## $salary
## [1] 50000
## 
## $ratified
## [1] TRUE
## 
## $Subjects_taught
## [1] "C Language"                  "SRP"                        
## [3] "Data warehousing and Mining"

3.6 Merging 2 lists / Recursive Lists

Let’s consider, 2 lists,

a<-list(1,"pradeep",c(1,2,3,4))
b<-list(2,"Suresh",c(5,6,7,8))
c<-list(a,b) # Combining the 2 lists.
c
## [[1]]
## [[1]][[1]]
## [1] 1
## 
## [[1]][[2]]
## [1] "pradeep"
## 
## [[1]][[3]]
## [1] 1 2 3 4
## 
## 
## [[2]]
## [[2]][[1]]
## [1] 2
## 
## [[2]][[2]]
## [1] "Suresh"
## 
## [[2]][[3]]
## [1] 5 6 7 8

3.7 Converting lists to vectors: Unlist() function.

a<-list(1,"pradeep",c(1,2,3,4)) # Consider a list
a
## [[1]]
## [1] 1
## 
## [[2]]
## [1] "pradeep"
## 
## [[3]]
## [1] 1 2 3 4
class(a) # The class will be list
## [1] "list"
r<-unlist(a)# For converting lists to vectors
r
## [1] "1"       "pradeep" "1"       "2"       "3"       "4"
class(r) # The class is character.
## [1] "character"

3.8 Applying functions on list. lapply() and sapply()

3.8.1 lapply() function

lapply function is applied for operations on list objects and returns a list object of same length of original set. lapply function in R, returns a list of the same length as input list object, each element of which is the result of applying function to the corresponding element of list.

Let’s take a list:

m<-matrix(1:6,ncol = 2)
l<-list(1,3,c(4,5,4),m[,1],"hi")
res<-lapply(l, mean)
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
res
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 3
## 
## [[3]]
## [1] 4.333333
## 
## [[4]]
## [1] 2
## 
## [[5]]
## [1] NA
class(res) # the class is list.
## [1] "list"

3.8.2 sapply() function

sapply function is similar to lapply but it will return either vector or matrix or a list. Let’s take the same example.

m<-matrix(1:6,ncol = 2)
l<-list(1,3,c(4,5,4),m[,1],"hi")
res1<-(sapply(l, mean))
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
res1
## [1] 1.000000 3.000000 4.333333 2.000000       NA
class(res1) # Class is not a list.
## [1] "numeric"