2.11 Lists

Lists are extremely important objects in R. You will have heard of the problems of ‘comparing apples and oranges’ or how two things are ‘as different as chalk and cheese’. You can think of lists as a way of getting around these problems. Here are four completely different objects: a numeric vector, a logical vector, a vector of character strings and a vector of complex numbers:

apples <- c(4,4.5,4.2,5.1,3.9)
oranges <- c(TRUE, TRUE, FALSE)
chalk <- c("limestone", "marl","oolite", "CaC03")
cheese <- c(3.2-4.5i,12.8+2.2i)

We cannot bundle them together into a dataframe, because the vectors are of different lengths:

Error in data.frame(apples, oranges, chalk, cheese) :
arguments imply differing number of rows: 5, 3, 4, 2

Despite their differences, however, we can bundle them together in a single list called items:

items <- list(apples,oranges,chalk,cheese)
[1] 4.0 4.5 4.2 5.1 3.9
[1] "limestone" "marl" "oolite" "CaC03"
[1] 3.2-4.5i 12.8+2.2i

Subscripts on vectors, matrices, arrays and dataframes have one set of square brackets [6], [3,4] or [2,3,2,1], but subscripts on lists have double square brackets [[2]] or [[i,j]]. If we want to extract the chalk from the list, we use subscript [[3]]:

[1] "limestone" "marl" "oolite" "CaC03"

If we want to extract the third element within chalk (oolite) then we use single subscripts after the double subscripts like this:

items[[3]][3] ...

Get The R Book, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.