Biostatistics with R

Lists

A list is an ordered collection of objects known as its components . Different objects like arrays, matrices, vectors and dataframes can be included in a list. Each object inside a list and its elements in turn can be accessed by appropriate symbol and indexing.

Creating lists

To create a list of objects, we should call the list() function and pass the object names to it. This function returns a list of objects.


We now create five vectors of different data types and put them together as a list using "list" command as shown below.

> expt_name <- c("Experiment-A","Experiment-B","Experiment-C", "Experiment-D",
"Experiment-E") > sample_length <- c(12.5, 32.6, 16.7, 20.6, 7.5) > sample_weight <- c(1122, 1123, 1124, 1125, 1126) > sample_category <- c('A','S','P','K','G') > lab_name <- c("Our_LAB") > dtable <- data.frame(c(10,20,30,40),c(1.2,3.5,4.6,7.9),c(112,435,645,245)) > names(dtable) = c("index","value","order")

While creating a list with these objects, it is useful to give each one of them an explicit name. In the example below, we store the variable "expt_name" as "name", variable "sample_length" as "length" and the variable "sample_weight" as "weight" in the list:


> alis <- list(name=expt_name, length=sample_length, weight=sample_weight,
category=sample_category,lab=lab_name, table=dtable) > > alis
$name [1] "Experiment-A" "Experiment-B" "Experiment-C" "Experiment-D" "Experiment-E" $length [1] 12.5 32.6 16.7 20.6 7.5 $weight [1] 1122 1123 1124 1125 1126 $category [1] "A" "S" "P" "K" "G" $lab [1] "Our_LAB" $table index value order 1 10 1.2 112 2 20 3.5 435 3 30 4.6 645 4 40 7.9 245

To view the structure of a list

In case the structure is very large, it is better to view the structure of a list using str() command. This command also displays the number of elements in each object.


> str(alis)
List of 6 $ name : chr [1:5] "Experiment-A" "Experiment-B" "Experiment-C" "Experiment-D" ... $ length : num [1:5] 12.5 32.6 16.7 20.6 7.5 $ weight : num [1:5] 1122 1123 1124 1125 1126 $ category: chr [1:5] "A" "S" "P" "K" ... $ lab : chr "Our_LAB" $ table :'data.frame': 4 obs. of 3 variables: ..$ index: num [1:4] 10 20 30 40 ..$ value: num [1:4] 1.2 3.5 4.6 7.9 ..$ order: num [1:4] 112 435 645 245

Accessing the elements of a list by name

The individual components in a list can be accessed by attaching the object name and list name by '$' symbol:


> names(alis)
[1] "name" "length" "weight" "category" "lab"
> alis$name
[1] "Experiment-A" "Experiment-B" "Experiment-C" "Experiment-D" "Experiment-E"
> alis$category
[1] "A" "S" "P" "K" "G"

> alis$name[2:4]
[1] "Experiment-B" "Experiment-C" "Experiment-D"

> alis$table
  index value order
1    10   1.2   112
2    20   3.5   435
3    30   4.6   645
4    40   7.9   245

The individual columns of table element can be accessed by treating "alis$table$" as a list name:
> alis$name$index
[1] 10 20 30 40

In another format, the elements of a component can be printed by giving its name in square bracket after list name:


> alis["name"]
$name [1] "Experiment-A" "Experiment-B" "Experiment-C" "Experiment-D" "Experiment-E"


> alis["table"]
$table index value order 1 10 1.2 112 2 20 3.5 435 3 30 4.6 645 4 40 7.9 245

Accessing the components of a list by index

To access the components of a list by index, the index is placed inside double bracket. See below:


> alis[[1]]
[1] "Experiment-A" "Experiment-B" "Experiment-C" "Experiment-D" "Experiment-E"
> alis[[5]]
[1] "IBAB_LAB"

Now we will accesss the sixth element of alis through "alis[[6]]". This is a data frame.
> alis[[6]]
index value order 1 10 1.2 112 2 20 3.5 435 3 30 4.6 645 4 40 7.9 245

Once the object in a list is accessed by index in double bracket, the name of list with index in double bracket serves as a name of object. We can then use appropriate indexing to get its individual elements. For example, alis[[1]] is a vector. We can access its elements by indexing:


> alis[[1]][4]
[1] "Experiment-D"
> alis[[1]][2:5]
[1] "Experiment-B" "Experiment-C" "Experiment-D" "Experiment-E"

Since "alis[[6]]" is a data frame, we can access its second dcolumn through its name or the column index:
> alis[[6]]$value
[1] 1.2 3.5 4.6 7.9
> alis[[6]][,2]
[1] 1.2 3.5 4.6 7.9

To get the number of components in a list

The number of components in a list can be obtained using the length() function, as we did with vectors:

> length(alis)
[1] 5

To append a component to a list

A new element can be appended to a list by name. We create a list with 2 elements called "name1" and "name2.


> blis = list(name1 =c(1,2,3,4,5), name2="aaaa") > > blis
$name1 [1] 1 2 3 4 5 $name2 [1] "aaaa"

We will now append a new component called "name3" to the list:


> blis$name3 = "aaaaaaa" > > blis
$name1 [1] 1 2 3 4 5 $name2 [1] "aaaa" $name3 [1] "aaaaaaa"