What is a list?

A list is R’s most flexible data structure: it can store objects of different types in a single container (vectors, matrices, data frames, even other lists or functions).

Create a list

Use list() to create a list:

list(element_1, element_2, ...)

Example: vector + matrix + data frame

Step 1: Create a vector

vect <- 1:5
vect
## [1] 1 2 3 4 5

Step 2: Create a matrix

Your original example had inconsistent dimensions; here is a correct 2x5 matrix:

mat <- matrix(1:10, nrow = 2, ncol = 5)
dim(mat)
## [1] 2 5
mat
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10

Step 3: Create a data frame (from a built-in dataset)

df <- as.data.frame(EuStockMarkets)[1:10, ]
head(df)
DAX SMI CAC FTSE
1628.75 1678.1 1772.8 2443.6
1613.63 1688.5 1750.5 2460.2
1606.51 1678.6 1718.0 2448.2
1621.04 1684.1 1708.1 2470.4
1618.16 1686.6 1723.1 2484.7
1610.61 1671.6 1714.3 2466.8

Step 4: Put them into a list

It is best practice to name list elements so you can extract them by name later.

my_list <- list(
  vect = vect,
  mat  = mat,
  df   = df
)

my_list
## $vect
## [1] 1 2 3 4 5
## 
## $mat
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10
## 
## $df
##        DAX    SMI    CAC   FTSE
## 1  1628.75 1678.1 1772.8 2443.6
## 2  1613.63 1688.5 1750.5 2460.2
## 3  1606.51 1678.6 1718.0 2448.2
## 4  1621.04 1684.1 1708.1 2470.4
## 5  1618.16 1686.6 1723.1 2484.7
## 6  1610.61 1671.6 1714.3 2466.8
## 7  1630.75 1682.9 1734.5 2487.9
## 8  1640.17 1703.6 1757.4 2508.4
## 9  1635.47 1697.5 1754.0 2510.5
## 10 1645.89 1716.3 1754.3 2497.4
names(my_list)
## [1] "vect" "mat"  "df"

Select elements from a list

There are three common ways to extract elements:

  • my_list[1] returns a sub-list (still a list)
  • my_list[[1]] returns the element itself
  • my_list$name is shorthand for my_list[["name"]]

Example: single vs double brackets

my_list      # a list of length 1
## $vect
## [1] 1 2 3 4 5
## 
## $mat
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10
## 
## $df
##        DAX    SMI    CAC   FTSE
## 1  1628.75 1678.1 1772.8 2443.6
## 2  1613.63 1688.5 1750.5 2460.2
## 3  1606.51 1678.6 1718.0 2448.2
## 4  1621.04 1684.1 1708.1 2470.4
## 5  1618.16 1686.6 1723.1 2484.7
## 6  1610.61 1671.6 1714.3 2466.8
## 7  1630.75 1682.9 1734.5 2487.9
## 8  1640.17 1703.6 1757.4 2508.4
## 9  1635.47 1697.5 1754.0 2510.5
## 10 1645.89 1716.3 1754.3 2497.4
my_list[]    # the matrix itself
## $vect
## [1] 1 2 3 4 5
## 
## $mat
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10
## 
## $df
##        DAX    SMI    CAC   FTSE
## 1  1628.75 1678.1 1772.8 2443.6
## 2  1613.63 1688.5 1750.5 2460.2
## 3  1606.51 1678.6 1718.0 2448.2
## 4  1621.04 1684.1 1708.1 2470.4
## 5  1618.16 1686.6 1723.1 2484.7
## 6  1610.61 1671.6 1714.3 2466.8
## 7  1630.75 1682.9 1734.5 2487.9
## 8  1640.17 1703.6 1757.4 2508.4
## 9  1635.47 1697.5 1754.0 2510.5
## 10 1645.89 1716.3 1754.3 2497.4

Extract by name

my_list[["df"]]
DAX SMI CAC FTSE
1628.75 1678.1 1772.8 2443.6
1613.63 1688.5 1750.5 2460.2
1606.51 1678.6 1718.0 2448.2
1621.04 1684.1 1708.1 2470.4
1618.16 1686.6 1723.1 2484.7
1610.61 1671.6 1714.3 2466.8
1630.75 1682.9 1734.5 2487.9
1640.17 1703.6 1757.4 2508.4
1635.47 1697.5 1754.0 2510.5
1645.89 1716.3 1754.3 2497.4
my_list$df
DAX SMI CAC FTSE
1628.75 1678.1 1772.8 2443.6
1613.63 1688.5 1750.5 2460.2
1606.51 1678.6 1718.0 2448.2
1621.04 1684.1 1708.1 2470.4
1618.16 1686.6 1723.1 2484.7
1610.61 1671.6 1714.3 2466.8
1630.75 1682.9 1734.5 2487.9
1640.17 1703.6 1757.4 2508.4
1635.47 1697.5 1754.0 2510.5
1645.89 1716.3 1754.3 2497.4

Built-in dataset example (CSV)

This section shows how a CSV is read into a data frame, and how to select columns.

path <- "raw_data/prison.csv"

prison <- read.csv(path, stringsAsFactors = FALSE)

# Keep the first 5 columns (all rows)
prison_5 <- prison[, 1:5]
head(prison_5, 5)
x state year govelec black
1 1 80 0 0.2560
2 1 81 0 0.2557
3 1 82 1 0.2554
4 1 83 0 0.2551
5 1 84 0 0.2548
str(prison_5)
## 'data.frame':    714 obs. of  5 variables:
##  $ x      : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ state  : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ year   : int  80 81 82 83 84 85 86 87 88 89 ...
##  $ govelec: int  0 0 1 0 0 0 1 0 0 0 ...
##  $ black  : num  0.256 0.256 0.255 0.255 0.255 ...

Note: - prison[, 1:5] means “all rows, columns 1 to 5”. - If you use prison[1:5] without the comma, R interprets it as selecting columns too, but prison[, 1:5] is clearer and safer when teaching.

 

A work by Gianluca Sottile

gianluca.sottile@unipa.it