A list is an ordered collection of objects of different type,
i.e. scalars, matrices, string variables. You can find all the following
examples in
XLGmatrix09.xpl
.
We create a list with the function
list
, which takes as
arguments the ordered elements of the list. The commands
seller1 = list("Ludwig","Beethoven",20) seller1create a list, with the string "Ludwig" as its first element, "Beethoven" as its second element, and the number 20 as its third element. This list is stored in the variable seller1, the content of seller1 is displayed as follows:
Contents of seller1.el1 [1,] "Ludwig" Contents of seller1.el2 [1,] "Beethoven" Contents of seller1.el3 [1,] 20The three components of the list are displayed. The default label of the
seller1.el2
Contents of el2 [1,] "Beethoven"However, we could give a label to the components of a list. Consider the following list:
age = 35 seller2 = list("Claudio","Arrau",age) seller2Since the third component of the list seller2 is the assigned variable age, this element is labeled age:
Contents of seller2.el1 [1,] "Claudio" Contents of seller2.el2 [1,] "Arrau" Contents of seller2.age [1,] 35
A list can be a component of another list: We define the list sellers, which contains the lists seller1 and seller2:
sellers = list(seller1,seller2) sellersreturns
Contents of sellers.seller1.el1 [1,] "Ludwig" Contents of sellers.seller1.el2 [1,] "Beethoven" Contents of sellers.seller1.el3 [1,] 20 Contents of sellers.seller2.el1 [1,] "Claudio" Contents of sellers.seller2.el2 [1,] "Arrau" Contents of sellers.seller2.age [1,] 35In that case, we access the element age of the variable seller2 of the list sellers by using twice the dot operator:
sellers.seller2.age = 36 sellers.seller2.agewhich returns
Contents of sellers.seller2.age [1,] 36
You may use any kind of object as an element of a list, e. g. strings,
matrices or even lists. To insert an object within a list one can use
the
insert
function.
name = "Andrew" insert(sellers, 1, name) sellersinserts a new element before el1 .This element contains the name "Andrew". Unfortunately, the element is not assigned the object name but the name according to the position notation (el1 in the example). Because the former first element retains its old name el1 we now have two elements with the same name but different contents.
Contents of sellers.el1 [1,] Andrew Contents of sellers.el1 [1,] 1.000000 Contents of sellers.age [1,] 29.000000
Using the dot operator (seller.el1) we will only get the value of the first object with this name ("Andrew"). We can, however, access the elements of a list not only by its name but also by its number by using the braces ({}) function. So,
sellers{1}gives
Contents of el1 [1,] "Andrew"
As well as inserting an object we can delete it by using the
function
delete
.
delete(sellers, 1)deletes the second element (sellers.el1="Andrew") from the list. Furthermore, we can
sales = list(100, 200, 75) append(sellers, sales) sellersappends the list object sales as a new element to the list object seller:
Contents of sellers.seller1.el1 [1,] "Ludwig" Contents of sellers.seller1.el2 [1,] "Beethoven" Contents of sellers.seller1.el3 [1,] 20 Contents of sellers.seller2.el1 [1,] "Claudio" Contents of sellers.seller2.el2 [1,] "Arrau" Contents of sellers.seller2.age [1,] 36 Contents of sellers.sales.el1 [1,] 100 Contents of sellers.sales.el2 [1,] 200 Contents of sellers.sales.el3 [1,] 75
In contrast to the
insert
function the
append
function assigns the object name of the appended object to the new
element name of the list unless it is only a temporary object
(expression). If the first parameter is not a list, i.e. not a composed object, then
a list is generated by the
append
function, where the first parameter becomes both the name of the
resulting list and the name of its first component.
The function
names
gives the names of all
components of a list object. The resulting object is a vector, such
that one can access single values using the square brackets operator
[].
names(sellers)[2:3]shows
Contents of names(sellers)[2:3] [1,] "seller2" [2,] "sales"Note that the output of the third element of the list gives only the name of the list object sales itself but not the names of its components.
The
size
function gives the number of elements
that are contained in a list.
For example,
li = list(sellers, matrix(3, 4)) size(li)gives
Contents of numcomp [1,] 2As before, only the elements of the respective list are counted, no matter what type they are or whether they have subobjects.
Finally,
comp
checks whether a list
contains a certain component. For example
comp(li,"sellers")returns
Contents of comp [1,] 1whereas
comp(li,"seller1")returns
Contents of comp [1,] 0The false value 0 tells us that the list li does not contain a component named seller1.