Lists
A list is a generic vector that is allowed to include different types of objects, even other lists.
It is useful for its flexibility. For example, the result of a linear model fit in R is basically a list object that contains rich results of a linear regression such as linear coefficients (numeric vectors), residuals (numeric vectors), QR decomposition (a list containing a matrix and other objects), and so on.
It is very handy to extract the information without calling different functions each time because these results are all packed into a list.
Creating a list
We can use list()
to create a list, as the function name suggests. Different types of objects can be put into one list. For example, the following code creates a list that contains a single-element numeric vector, a two-entry logical vector, and a character vector of three values:
l0 <- list(1, c(TRUE, FALSE), c("a", "b", "c")) l0 ## [[1]] ## [1] 1 ## ## [[2]] ## [1] TRUE FALSE ## ## [[3...