R Fundamentals

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("IBAB_LAB")

While creating a list with these objects, it is useful to give each one of them an explicit name:


> alis <- list(name=expt_name, length=sample_length, weight=sample_weight,
category=sample_category,lab=lab_name) > > 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] "IBAB_LAB"
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 5 $ 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 "IBAB_LAB"
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"

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"
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"

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"
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"