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:

data.frame(apples,oranges,chalk,cheese)
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)
items
[[1]]
[1] 4.0 4.5 4.2 5.1 3.9
[[2]]
[1] TRUE TRUE FALSE
[[3]]
[1] "limestone" "marl" "oolite" "CaC03"
[[4]]
[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]]:

items[[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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.