Lists

Lists are a fundamental part of R.  Lists are simply vectors in which each element may be any other type of R object such as a scalar, vector, matrix, character string or another list.  Thus lists are said to be a recursive data structure.

Many R functions such as lm, glm, gam, stl, loess, etc. return lists.  Often these lists have a special class attributes which is recognized by the function base function print which causes the output to be printed in a more tidy fashion.  If you want to see the raw list output from these functions try using the function print.default.

Lists can be created using the list(...) function.  For example,

x <- list(squares=(1:5)^2, myname="Ian McLeod", mat=(1:5)%o%(5:1))

creates a list with three named components.  The components in this list are a vector, a character string and a matrix.

The second component can be extracted from the list as: x[[2]] or x$myname.  Note that x[2] returns a list of just one element whereas x[[2]] extracts the second element as a character string.