Skip to content

R's list data structure

5 messages · Ajay Askoolum, Sarah Goslee, Bert Gunter

#
HI Ajay,
On Fri, Feb 17, 2012 at 3:20 PM, Ajay Askoolum <aa2e72e at yahoo.co.uk> wrote:
In your particular case, where all list components are the same length and are
associated with each other in order, a special type of list called a data frame
is easier to work with.

weekProfile<- data.frame(dow=dayOfWeekName,dowI=dayOfWeekOrdinal,dowW=dayOfWeekWorkDay)
dow dowI  dowW
1 Mon    1  TRUE
2 Tue    2  TRUE
3 Wed    3  TRUE
4 Thu    4  TRUE
5 Fri    5  TRUE
6 Sat    6 FALSE
7 Sun    0 FALSE

I'm not sure what kind of conditional you want, but this can easily be done
with subset() or [
dow dowI dowW
1 Mon    1 TRUE
2 Tue    2 TRUE
3 Wed    3 TRUE
4 Thu    4 TRUE
5 Fri    5 TRUE

A regular list is excellent for holding diverse kinds of data, for example 10
lm() objects, or a series of data frames.

In a list, the third element of component 1 may not have anything whatsoever
to do with the third element of component 2.

In a data frame, rows are related.
Data frame.

  
    
#
On Fri, Feb 17, 2012 at 3:37 PM, Ajay Askoolum <aa2e72e at yahoo.co.uk> wrote:
I gave one example: storing lm() objects.

Here's another: I'm doing a lot of spatial processing, and I read a single
multispectral image into a list. Each list component is a SpatialGridDataFrame.
That way each band from a single image is part of the same R object, and
I can use lapply() to perform an operation on each band in turn.

Using lists for things is a very Rish way of working, but it may take a
while to get the hang of it.
#
FWIW:

Lists are a fundamental, universal, recursive data structure. All
other data structures (i.e. r.e. sets) can be represented as lists.
Indeed, one of the earliest "high level" (non-machine instructions)
computer languages, McCarthy's LISP = List Processing, is based on
lists. R was designed to be LISP-like (= a functional programming
language) in some fundamentals ways. So it is no surprise that lists
are widely used within R.

Cheers,
Bert
On Fri, Feb 17, 2012 at 12:37 PM, Ajay Askoolum <aa2e72e at yahoo.co.uk> wrote: