4
LISTS
In contrast to a vector, in which all ele-
ments must be of the same mode, R’slist
structure can combine objects of different
types. For those familiar with Python, an R list
is similar to a Python dictionary or, for that matter, a
Perl hash. C programmers may find it similar to a C
struct. The list plays a central role in R, forming the
basis for data frames, object-oriented programming,
and so on.
In this chapter, we’ll cover how to create lists and how to work with
them. As with vectors and matrices, one common operation with lists is
indexing. List indexing is similar to vector and matrix indexing but with
some major differences. And like matrices, lists have an analog for the
apply()function. We’ll discuss these and other list topics, including ways
to take lists apart, which often comes in handy.
4.1 Creating Lists...............................................................
Technically, a list is a vector. Ordinary vectors—those of the type we’ve
been using so far in this book—are termedatomicvectors, since their