Lists are a little different to other data structures you typically encounter in R. They can contain elements of different types, including other lists, and offer a large amount of flexibility.
Lists can be created with list()
and may be returned by some functions such as lapply()
.
Creating a list
The list()
function is the most common way to create a list object.
Accessing list elements
Our list object my_list
contains a character vector, a numeric vector, a list, and a logical vector.
We can access elements within our list using double square brackets [[ ]]
. To access the first element of my_list
we would use [[1]]
. As with all indexing in R, the index for list objects starts at 1.
The 2nd element of my_list
is a numeric vector, we can interact with it like any other vector by combining the list subsetting with a vector subset. We could use this approach to return the 2nd and 3rd element of the vector.
We can also perform actions over list elements.
The 3rd element of my_list
is a list itself, which we can refer to as a nested list. To access an element of the nested list we need to utilise the [[ ]]
syntax twice.
Named lists
We can also create named lists. Notice that we don’t wrap the list element names in quotations (" "
).
We can now access the elements of my_list
using the name and $
operator, though the index system still works.
We can also apply names to an existing list with the names()
function.
Appending to a list
append()
We can add to a list using append()
. Lets append a data.frame
object to my_list
.
Information: If you are coming to R from another language such as python, you need to be aware that append()
does not take care of assignment and doesn't modify my_list
in place, so you do need to assign the output of append()
to store it (i.e. my_list <- append(...
).
You should also be aware that with an object such as a data.frame
, you need to ensure it is within a list itself when being appended to an existing list. If we omit this step then the individual columns will be appended rather than the data.frame
as a whole. This is because beneath the surface, data.frames
are actually list objects.
c()
We can also append to a list with c()
.
Removing list elements
Removing list elements can be achieved with the NULL
keyword. Lets remove the data.frame
object we added earlier as the 5th element of my_list
.
We’re not limited to removing just elements from the end of the list. We can use the same technique to remove the 2nd element of my_list
.
Iterating over lists
There are functions, such as lapply()
that exist to iterate over lists which can help to provide optimised performance, though these will be covered elsewhere.
Iteration over a list with a loop works in much the same way as with other R objects.
Next steps
Lists are an important data structure within R and are particularly useful for solving some more advanced programming problems as well as for optimisation.
Their versatility makes them perfect for use in a variety of situations.
Try creating some lists consisting of different R objects and accessing the objects.