Lists

Creating a list to store the name, salary, and a Boolean indicating union membership.

j <- list(name="Joe", salary=55000, union=T)
j
## $name
## [1] "Joe"
## 
## $salary
## [1] 55000
## 
## $union
## [1] TRUE

Storing the salary for the employee database.

j$salary
## [1] 55000

Storing the name for employee database.

j$name
## [1] "Joe"

Storing the Boolean to indicate union membership.

j$union
## [1] TRUE

General List Operations

List Indexing

Several ways to access a list component.

j$salary
## [1] 55000
j[["salary"]]
## [1] 55000

List components by their numerical indices.

j[[2]]
## [1] 55000

Adding and Deleting List Elements New components can be added after a list is created.

z <- list(a="abc",b=12)
z
## $a
## [1] "abc"
## 
## $b
## [1] 12

Added a c component.

z$c <- "sailing" # add a c component
z
## $a
## [1] "abc"
## 
## $b
## [1] 12
## 
## $c
## [1] "sailing"

Added components via a vector index.

z[[4]] <- 28
z[5:7] <- c(FALSE,TRUE,TRUE)
z
## $a
## [1] "abc"
## 
## $b
## [1] 12
## 
## $c
## [1] "sailing"
## 
## [[4]]
## [1] 28
## 
## [[5]]
## [1] FALSE
## 
## [[6]]
## [1] TRUE
## 
## [[7]]
## [1] TRUE

Deleted a list component by setting it to NULL.

z$b <- NULL
z
## $a
## [1] "abc"
## 
## $c
## [1] "sailing"
## 
## [[3]]
## [1] 28
## 
## [[4]]
## [1] FALSE
## 
## [[5]]
## [1] TRUE
## 
## [[6]]
## [1] TRUE

Also, concatenate lists.

c(list("Joe", 55000, T),list(5))
## [[1]]
## [1] "Joe"
## 
## [[2]]
## [1] 55000
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] 5

Getting the Size of a List Obtained the number of components in a list via length().

length(j)
## [1] 3
length(z)
## [1] 6

Accessing List Components and Values

names(j)
## [1] "name"   "salary" "union"

Unlist, to obtain the values.

ulj <- unlist(j)
ulj
##    name  salary   union 
##   "Joe" "55000"  "TRUE"
class(ulj)
## [1] "character"

Since it starts with numbers, i ended up with numbers.

z <- list(a=5,b=12,c=13)
y <- unlist(z)
class(y)
## [1] "numeric"
y
##  a  b  c 
##  5 12 13
w <- list(a=5,b="xyz")
wu <- unlist(w)
class(wu)
## [1] "character"
wu
##     a     b 
##   "5" "xyz"

Applying Functions to Lists

Using the lapply() and sapply() Functions

R applied median() to 1:3 and to 25:29, returning a list consisting of 2 and 27.

lapply(list(1:3,25:29),median)
## [[1]]
## [1] 2
## 
## [[2]]
## [1] 27

Using sapply(), rather than applying the function directly, gave us the desired matrix form in the output.

sapply(list(1:3,25:29),median)
## [1]  2 27

Extended Example: Back to the Abalone Data

The lapply() function expects its first argument to be a list. Here it was a vector, but lapply() will coerce that vector to a list form.

g <- c("M","F","F","I","M","M","F")
lapply(c("M","F","I"),function(gender) which(g==gender))
## [[1]]
## [1] 1 5 6
## 
## [[2]]
## [1] 2 3 7
## 
## [[3]]
## [1] 4

Recursive Lists

Lists can be recursive, meaning that you can have lists within lists.

b <- list(u = 5, v = 12)
c <- list(w = 13)
a <- list(b,c)
a
## [[1]]
## [[1]]$u
## [1] 5
## 
## [[1]]$v
## [1] 12
## 
## 
## [[2]]
## [[2]]$w
## [1] 13
length(a)
## [1] 2

This code makes a into a two-component list, with each component itself also being a list. The concatenate function c() has an optional argument recursive, which controls whether flattening occurs when recursive lists are combined.

c(list(a=1,b=2,c=list(d=5,e=9)))
## $a
## [1] 1
## 
## $b
## [1] 2
## 
## $c
## $c$d
## [1] 5
## 
## $c$e
## [1] 9

In the first case, we accepted the default value of recursive, which is FALSE, and obtained a recursive list, with the c component of the main list itself being another list. In the second call, with recursive set to TRUE, we got a single list as a result

c(list(a=1,b=2,c=list(d=5,e=9)),recursive=T)
##   a   b c.d c.e 
##   1   2   5   9