Lists

Lists are more powerful than vectors, because they can hold multiple types of things. A vector is a one-dimensional array of scalars, while a list is a one-dimensional array of objects. For example, this list contains a character vector, a numeric matrix, and a numeric vector:

> blah <- list(c("1","two","three","4"),matrix(c(1,2,3,4,5,6),nrow=2,ncol=3),c(1,7,17))
> blah
[[1]]:
[1] "1"     "two"   "three" "4"

[[2]]:
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[[3]]:
[1]  1  7 17
Selecting elements from a list requires two separate subscripts, unlike vectors (which need one) or matrices (which need a pair). The first subscript (in double brackets) tells which part of the list to select, and the second (in single brackets) tells which element of that part to select.

> blah[[1]]
"1"     "two"   "three" "4"
> blah[[1]][3]
[1] "three"
> blah[[2]][1:2,2]
[1] 3 4
Lists have a names attribute:

> names(blah) <- c("some.characters","a.matrix","some.numbers")
> blah
$some.characters:
[1] "1"     "two"   "three" "4"

$a.matrix:
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

$some.numbers:
[1]  1  7 17
Once you assign names to a list, you can select parts of the list using either the name or double brackets.

> blah$some.characters[4]
[1] "4"
> blah[[1]][4]
[1] "4"



Pantelis Vlachos
1/15/1999