Lists in R

Although the R vector is a list of items, it suffers from the constraint that all elements in a vector must be the same data type. The list data type is a one dimensional list of items, where each item can be a different data type. Items in a list can be anything, including vectors, matrices and even other lists. A list can be created using the list() function:

> x = list(42, T, "wibble", matrix(1:10,5,2))
> x
[[1]]
[1] 42
[[2]]
[1] TRUE
[[3]]
[1] "wibble"
[[4]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

The list x contains integer, logical, character and matrix data types. To refer to an element in the list, we need to use the double-bracket notation, so x[[1]] is the first element in the list, which is a vector with a single element (42). The element x[[4]] is the 5×2 matrix shown.

Note the difference between the following two objects:

> intList = list(1:10)
> intVector = 1:10
> intList
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10
> intVector
 [1]  1  2  3  4  5  6  7  8  9 10

The object intList is a list containing a single element which is the vector of integers from 1 to 10. The object intVector is a vector with 10 elements. To get the number 4 out of intList, we’d need to say intList[[1]][4] while for intVector we say intVector[4].

The typeof() function returns ‘list’ when applied to a list, no matter what that list contains. As ‘list’ isn’t a numeric type, we can’t use any of the mathematical operations on a list.

We can name the elements of a list by using the names() function. For our list x above we could say:

> names(x) = c("number", "logical", "string", "matrix")
> x
$number
[1] 42
$logical
[1] TRUE
$string
[1] "wibble"
$matrix
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

Notice that the name of each element is prefixed by a $. We can use the $ notation to refer to list elements:

> x$matrix
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

We can also use the notation x[["matrix"]] to get the same element. (Vector elements can also be named, but we can’t use the $ notation to refer to vector elements; we must use vec["name"].)

Apart from that, there’s not a lot that can be done with lists at the top level. Their main use is as a storage container for other objects, and as the basis for the data frame, which is a much more commonly used data type.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Trackbacks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: