Page 2 of 3
The basic List indexing works by allowing you to use a range of R data structures to extract a sub-List. For example if i is a numeric vector (see later) the if x is the list:
and i is the vector composed of the numeric elements 2,3 i.e.:
is the List containing elements 2 and 3 of x i.e. y and z.
R has a shorthand m:n for the vector containing all the integers from m to n. So you could write:
for the list composed of the second and third elements of lis.
is simply a list consisting of the second element of lis. What if you don't want a list but the actual element?
R uses the [] indexing operator to return an element. Hence:
is the string y which should be compared to lis which the list with the single element "y".
Because R tends to convert data types without worrying too much the difference between lis[[i]] and lis[i] usually doesn't matter and beginner often only know bout lis[i].
From here we can move on to look at the more advanced indexing options and the functions that can be used to work with Lists but this is all fairly standard stuff. For example if you want the number of elements in a list use length(lis) and to concatenate Lists you would use the c or concatenation function:
To change an element of a list you simply assign to the indexed element as in:
and as already mentioned you can also write
x<- "new string"
To extend a list (or any other indexed data structure you would simply assign to the new elements. For example
adds a fourth element to lis. When adding a new element all of the intermediate indexed elements that don't exist are created and set to Null.
All objects in R have an Attribute list and it is this that makes the R List user friendly and have some of the properties of a type based object system. That is, every List in R has an associated Attribute list that is in fact a slightly specialised form of the List - a parlist. Basically Attributes are lists of name value pairs rather than just a simple indexed List. Attributes serve to extend the way Lists work. There are a standard set of Attributes but you can add to this if you need to.
For example the names attribute can be used to store a set of strings used to label the elements of a List. To set an attribute you can assign to the the attr(object,attribute) function although R usually provides simpler and more natural ways of achieving the same result.
assigns the names first, second and third to each of the elements of the list. A more natural way of achieving the same result is:
You can see the attributes assigned to a list using the attributes function
 "first" "second" "third"
However attributes are often built into the natural behavior of functions and other facilities in R. For example if a List has a Names attribute you can use the names with the indexing operator - for example:
In this case the Names attribute is searched for an item that matches and the corresponding element of the List is returned.
As well as the Names attribute there are a small number of others that extend the behaviour of the simple List.
Of these the most important to deal with before moving on to consider other data structures is the class attribute.
This stands in for a lack of a proper object-oriented type system. Put simply you can use the class attribute to list the classes that the object inherits from. The first item in the class list is also taken to be the objects actual class. Given that object in R aren't really objects and there is no inheritance or class formalism this might seem all a bit strange. However, if you view the class idea as being a way of specifying that a List statistics contain constraints and hence can be treated as an example of a particular type of List, then it make more sense. It makes even better sense after an example and this is given in the next section on the vector.
In most cases the class attribute is set automatically when a data structure is created by its creation function but you can access and modify the class attribute as with any attribute. There is also the class function which returns the class of the data structure.