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.
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.
We can access individual elements in a list by using following ways:
L$salary # Returns the "name" from the list
## [1] 50000
L[["salary"]]
## [1] 50000
L[[1]]
## [1] "Pradeep"
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.
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"
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"
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
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"
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"
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[2]<-NULL
L
## $name
## [1] "Pradeep"
##
## $ratified
## [1] TRUE
##
## $Subjects_taught
## [1] "C Language" "SRP"
## [3] "Data warehousing and Mining"
L$Subjects_taught<-NULL
L
## $name
## [1] "Pradeep"
##
## $ratified
## [1] TRUE
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"
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
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"
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"
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"