Lists



next up previous contents
Next: Constructing and modifying Up: Listsdata frames, Previous: Listsdata frames,

Lists

An S-PLUS list is an object consisting of an ordered collection of objects known as its components.

There is no particular need for the components to be of the same mode or type, and, for example, a list could consist of a numeric vector, a logical value, a matrix, a complex vector, a character array, a function, and so on.

Components are always numbered and may always be referred to as such. Thus if St is the name of a list with four components, these may be individually referred to as St[[1]], St[[2]], St[[3]] and St[[4]]. If, further, St[[3]] is a triply subscripted array then St[[3]][1,1,1] is its first entry and dim(St[[3]]) is its dimension vector, and so on.

If St is a list, then the function length(St) gives the number of (top level) components it has.

Components of lists may also be named, and in this case the component may be referred to either by giving the component name as a character string in place of the number in double square brackets, or, more conveniently, by giving an expression of the form

name$component_name

for the same thing.

This is a very useful convention as it makes it easier to get the right component if you forget the number. So if the components of St above had been named, and the names were x, y, coefficients and covariance they could be referred to as St$y, St$covariance and so on, (or indeed as St[["y"]], St[["covariance"]] ... but this form is rarely if ever needed).

It is very important to distinguish St[[1]] from St[1]. ``[[...]]'' is the operator used to select a single element, whereas ``[...]'' is a general subscripting operator. Thus the former is the first object in the list St, and if it is a named list the name is not included. The latter is a sublist of the list St consisting of the first entry only. If it is a named list, the name is transferred to the sublist.

The names of components may be abbreviated down to the minimum number of letters needed to identify them uniquely. Thus St$coefficients may be minimally specified as St$coe and St$covariance as St$cov.

The vector of names is in fact simply an attribute of the list like any other and may be handled as such. Other structures besides lists may, of course, similarly be given a names attribute also.



next up previous contents
Next: Constructing and modifying Up: Listsdata frames, Previous: Listsdata frames,



Erik Moledor
Tue Jan 31 21:02:18 EST 1995