What's a List and how to use them - R Basics

In this article, we will learn how to use Lists in R, the most versatile data structure.

·

5 min read

Hey everyone! Happy New Year! Wishing everyone a wonderful year of learning and growth ahead!

Today, we are going to talk about the FINAL basic data structure in R called Lists. What is a List? Lists allows us to store multiple types of data structures in a single variable. Before this, we learnt about vectors and how they can only store one data type (numeric / logical / character) in it, we also learnt about matrices and how they are 2-dimensional vectors which means they also can only store one data type. Lastly, we also learnt about Data Frames (Part 1 & Part 2) which is the main data structure you'll be using most of the time and it can store multiple data types within it. Now, we are going to learn how to store multiple data structures into a single variable using lists.

Creating a list

To create a list, we use the list() function in R. First, let's create a few data structures as examples for our list.

#Creating a vector
> v <- c('a','b','c','d','e')
> v
[1] "a" "b" "c" "d" "e"

#Creating a matrix
> mat <- matrix(1:10, nrow = 2)
> mat
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

#Assigning a data frame
> df <- women
> df
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

#Putting all 3 into a single list
> li <- list(v,mat,women)
> li
[[1]]
[1] "a" "b" "c" "d" "e"

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

[[3]]
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

As you can see, all 3 data structures appeared with their index number e.g. [[1]] when I called out the list variable ('li'). When you try to extract an element in a list using single brackets, it will show you the items that are within the element. In the case below, the first (and only) item in our 2nd element of our list was the matrix.

> li[2]
[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

To actually grab the matrix itself from the list, we will have to use double brackets instead. We can also do nested indexing too.

> li[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10
> li[[2]][1,]
[1] 1 3 5 7 9

Assigning Names to List elements

Using index number will reduce the readability of your code, not just for you when you have to come back to it in the future, but also when you are working on a project with someone else. So to solve this issue, we can assign names to the elements in our list.

> li <- list(sample_vec = v, sample_mat = mat, sample_df = df)
> li
$sample_vec
[1] "a" "b" "c" "d" "e"

$sample_mat
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

$sample_df
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

Combining lists

Just when you thought lists could not be more versatile, you can also have a list within a list (which is basically combining multiple lists into a single variable). We can do this by using the combine function c().

> duplicate_list <- c(li,li)
> duplicate_list
$sample_vec
[1] "a" "b" "c" "d" "e"

$sample_mat
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

$sample_df
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

$sample_vec
[1] "a" "b" "c" "d" "e"

$sample_mat
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

$sample_df
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

Good news! That's pretty much it for Lists in R and we are finally done with the basic data structures. The foundation has been set and we are ready for the real "programming" aspect. Woohoo~~~ This is where the real fun begins! So stay tuned for the next post where we talk about Logical Operators (quite similar to Comparison Operators with some additional bits) so it'll be a good refresher as well.

Thank you for reading and that's all for today! See you next time!