using lists in R

By Jen Richmond

June 27, 2022

One of my goals while on long service leave is to learn some new R things that have been on my radar for a while… the first of these is purrr. The purrr package allows you to iterate a function across different elements in a list or dataframe.

I have started to try and learn purr before ( see a list of resources here and I have copied other people’s purrr code a couple of times and used map to read in a LOT of .csv files and pwalk to output plots in different colour schemes, but when you copy purrr code from someone else and adjust it to suit you own problem… you can’t really say you know how to use purrr.

The first thing I think I need to get my head around in order to understand purrr is lists. Dataframes are the bread and butter of the tidyverse and up until now I have avoided them, or tried desperately to use as.dataframe() or unnest() to turn them into a data structure that i understand. Lists allow you to bundle together different kinds of data elements together, so now is the time to get my head around them.

how to make a list

This is a example I copied from a tutorial

myfirstlist <-  list(2, 
                     "hello", 
                     c(3,5,4), 
                     1:5, 
                     list(FALSE, 
                          c("this", "is","a","list"),
                          c(FALSE,TRUE,TRUE,TRUE,FALSE))) 

The list() function lets you put elements of all different types (and lengths) into a listy bundle; characters and numbers and logicals. One of the elements in this list of 5 items when others have 3 items or even 1. Another item is ANOTHER list made up of 3 items. Lists within lists— eeeekk. We can use the class() function to check that our list is a list and the str() function to get our head around what we are dealing with.

class(myfirstlist)
## [1] "list"
str(myfirstlist)
## List of 5
##  $ : num 2
##  $ : chr "hello"
##  $ : num [1:3] 3 5 4
##  $ : int [1:5] 1 2 3 4 5
##  $ :List of 3
##   ..$ : logi FALSE
##   ..$ : chr [1:4] "this" "is" "a" "list"
##   ..$ : logi [1:5] FALSE TRUE TRUE TRUE FALSE

This iconic image (which I think comes from a Jenny Bryan talk) is supposed to help me understand how to access elements of a list.

Lets see if I can unpack it.

Posted on:
June 27, 2022
Length:
2 minute read, 404 words
See Also: