Chapter 9 Lists

9.1 Introduction

Lists are very useful as they are heterogeneous i.e. they can contain different data types. If you remember, vectors and matrices are homogeneous i.e. they can contain only one type of data. If you include different data types, they will all be coerced to the same type. With lists, it is different. In this chapter, we will delve deeper into lists.

  • how to create lists
  • access list elements
  • name list elements
  • coerce other R objects to list
  • coerce list to other R objects

9.2 Creating Lists

To create a list, we use the list() function. Let us create a simple list to demonstrate how they can contain different data types.

## [[1]]
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## [[2]]
## [1] "Jack" "John" "Jill"
## 
## [[3]]
## [1]  TRUE FALSE
## 
## [[4]]
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

If you observe the output, all the elements of the list retain their data types. Now let us learn how to access the elements of the list.

9.3 Access List Elements

You can access the elements of a list using the following operators:

  • [[
  • [
  • $

Let us try them one by one.

##  [1]  1  2  3  4  5  6  7  8  9 10
## [[1]]
##  [1]  1  2  3  4  5  6  7  8  9 10

[[ returns a vector while [ returns a list. The $ operator can be used only when we have named elements in the list. Let us add names to the elements. Use the names() function to add names to the list.

## $vect1
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## $vect2
## [1] "Jack" "John" "Jill"
## 
## $vect3
## [1]  TRUE FALSE
## 
## $mat
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
##  [1]  1  2  3  4  5  6  7  8  9 10
##  [1]  1  2  3  4  5  6  7  8  9 10
## $vect1
##  [1]  1  2  3  4  5  6  7  8  9 10

9.4 Coerce other objects

You can coerce other objects to list using as.list().

## [[1]]
## [1] 1
## 
## [[2]]
## [1] 2
## 
## [[3]]
## [1] 3
## 
## [[4]]
## [1] 4
## 
## [[5]]
## [1] 5
## 
## [[6]]
## [1] 6
## 
## [[7]]
## [1] 7
## 
## [[8]]
## [1] 8
## 
## [[9]]
## [1] 9
## 
## [[10]]
## [1] 10
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 2
## 
## [[3]]
## [1] 3
## 
## [[4]]
## [1] 4
## 
## [[5]]
## [1] 5
## 
## [[6]]
## [1] 6
## 
## [[7]]
## [1] 7
## 
## [[8]]
## [1] 8
## 
## [[9]]
## [1] 9

9.5 Coerce list to other objects

Use unlist() to coerce a list to vector.

##  vect11  vect12  vect13  vect14  vect15  vect16  vect17  vect18  vect19 
##     "1"     "2"     "3"     "4"     "5"     "6"     "7"     "8"     "9" 
## vect110  vect21  vect22  vect23  vect31  vect32    mat1    mat2    mat3 
##    "10"  "Jack"  "John"  "Jill"  "TRUE" "FALSE"     "1"     "2"     "3" 
##    mat4    mat5    mat6    mat7    mat8    mat9 
##     "4"     "5"     "6"     "7"     "8"     "9"
##  [1] "1"     "2"     "3"     "4"     "5"     "6"     "7"     "8"    
##  [9] "9"     "10"    "Jack"  "John"  "Jill"  "TRUE"  "FALSE" "1"    
## [17] "2"     "3"     "4"     "5"     "6"     "7"     "8"     "9"