8 min read

List Basics-Part-01



View raw source for this post

Summary

This post deals with list basics.

Table of Contents

Introduction

For a thorough review of vectors and lists, I’d recommend starting with Hadley Wickham & Garret Grolemund, R for Data Science, Chapter 20.

Definitions

From the R Programming Manual 2.1.2: “Lists (“generic vectors”) are another kind of data storage. Lists have elements, each of which can contain any type of R object, i.e. the elements of a list do not have to be of the same type. List elements are accessed through three different indexing operations. Lists are vectors, and the basic vector types are referred to as atomic vectors where it is necessary to exclude lists.”

A data frame is considered to be a special kind of list. Again, from the R Programming Manual, “[a] data frame is a list of vectors, factors, and/or matrices all having the same length (number of rows in the case of matrices). In addition, a data frame generally has a names attribute labeling the variables and a row.names attribute for labeling the cases. A data frame can contain a list that is the same length as the other components. The list can contain elements of differing lengths thereby providing a data structure for ragged arrays. However, as of this writing, such arrays are not generally handled correctly.”

Key Terms

  • a named list is one where the name attribute is set whereas an unnamed list is where the name attribute is “NULL”.

  • a nested list is a list which is the child of another list like my.list <- list(a = list(b = 1)).

  • an atomic vector is the most basic building block in R. There are six types of atomic vectors: logical, integer, double, character, complex, and raw. Its expression may take the form of c(1, 2, 3, 4) or c(TRUE, FALSE). Each element of the vector is of the same type. Where the elements are mixed, i.e. c(1, "a"), they will be implicitly coerced into a character type.

  • a generic vector or list is one that can contain data of different types like list(1, "a", TRUE, 1 + 4i).

  • a homogenous vector is one where all elements of the vector are of the same type.

  • a heterogeneous vector is one where the elements are of different types and by definition must be a list.

  • a recursive vector is another name for a list. It is considered recursive because a list can contain another list.

Why Lists Are Important

  1. Because lists can contain a variety of data types, function results are often returned in a list. Examples include the function returns from ggplot, lm, and str_split.

  2. Importing data in chunks is often easier when appending to a list rather than a data frame.

  3. .json and .xml are scraped as lists.

Create a named list

# create
ll <- list(a = 1)
#print 
ll
$a
[1] 1

Create a nested list

#nested
ll <- list(list(b = 1))
#print
ll
[[1]]
[[1]]$b
[1] 1

Create an empty list

#empty
ll <- vector("list", length = 3)
#print
ll
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

Name a list

#explicit
ll <- list(a = 1, b = 2, c = 3)
$a
[1] 1

$b
[1] 2

$c
[1] 3

or

#via names
names(ll) <- letters[1:3]
$a
[1] 1

$b
[1] 2

$c
[1] 3

or

#via attr
attr(ll, which = "names") <- letters[4:6]
$names
[1] "d" "e" "f"

Subset/Extract list element

Here are five ways to subset a list or extract an element from a list.

Single bracket “[”

The single bracket [ extracts a sub-list. The result will always be a list.

ll[1]
$d
[1] 1

Double bracket “[[”

The double bracket [[ extracts a single component from a list. It removes a level of hierarchy from the list.

ll[[1]]
[1] 1

Dollar Sign “$”

The dollar sign $ is shorthand for extracting a named element of a list. It works similarly to [[ except that you don’t need to use quotes.

ll$e
[1] 2

Extract by name

A list can be extracted by name using the syntax: [["name"]].

ll[["e"]]
[1] 2

Extract by position

An element can be extracted by position using the syntax: [[1]].

ll[[3]]
[1] 3

Append a list

#append
ll <- append(ll, values = list(z = 6))
$d
[1] 1

$e
[1] 2

$f
[1] 3

$z
[1] 6

Convert list to vector

#unlist
unlist(ll)
d e f z 
1 2 3 6 

Review

#new list
ll <- list(list(list(named = 1, 2)))
[[1]]
[[1]][[1]]
[[1]][[1]]$named
[1] 1

[[1]][[1]][[2]]
[1] 2
# [[ and $ return value 
all.equal(
    ll[[1]][[1]][["named"]] |> class(),
    ll[[1]][[1]]$named |> class(),
    ll[[1]][[1]][[2]] |> class()
)
[1] TRUE
# return value 
all.equal(
    ll[[1]][[1]][["named"]] |> class(),
    ll[[1]][[1]]$named |> class(),
    ll[[1]][[1]][[2]] |> class()
)
[1] TRUE
# return list 
all.equal(
    ll[[1]][[1]]["named"] |> class(),
    ll[[1]][[1]][2] |> class()
)
[1] TRUE
## rename 
names(ll) <- "nest_level_1"
names(ll[[1]]) <- "nest_level_2"
names(ll[[1]][[1]]) <- c("name_1", "name_2")
$nest_level_1
$nest_level_1$nest_level_2
$nest_level_1$nest_level_2$name_1
[1] 1

$nest_level_1$nest_level_2$name_2
[1] 2
$nest_level_1
$nest_level_1$nest_level_2
$nest_level_1$nest_level_2$name_1
[1] 1

$nest_level_1$nest_level_2$name_2
[1] 2
## extract with $ 
ll$nest_level_1$nest_level_2$name_2
[1] 2

Conclusion

This post discussed what a list is and how it is described. You should be able to create a list; subset and extract list; append to a list; and convert a list to an array.

References

[1]
R Core Team, R: A language and environment for statistical computing. Vienna, Austria: R Foundation for Statistical Computing, 2022 [Online]. Available: https://www.R-project.org/
[2]
Y. Xie, C. Dervieux, and A. Presmanes Hill, Blogdown: Create blogs and websites with r markdown. 2022 [Online]. Available: https://CRAN.R-project.org/package=blogdown

Disclaimer

The views, analysis and conclusions presented within this paper represent the author’s alone and not of any other person, organization or government entity. While I have made every reasonable effort to ensure that the information in this article was correct, it will nonetheless contain errors, inaccuracies and inconsistencies. It is a working paper subject to revision without notice as additional information becomes available. Any liability is disclaimed as to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause. The author(s) received no financial support for the research, authorship, and/or publication of this article.

Reproducibility

─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.1.3 (2022-03-10)
 os       macOS Big Sur/Monterey 10.16
 system   x86_64, darwin17.0
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Chicago
 date     2022-04-21
 pandoc   2.14.1 @ /usr/local/bin/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.1.0)
 blogdown    * 1.9        2022-03-28 [1] CRAN (R 4.1.2)
 bookdown      0.25       2022-03-16 [1] CRAN (R 4.1.2)
 brio          1.1.3      2021-11-30 [1] CRAN (R 4.1.0)
 bslib         0.3.1.9000 2022-03-04 [1] Github (rstudio/bslib@888fbe0)
 cachem        1.0.6      2021-08-19 [1] CRAN (R 4.1.0)
 callr         3.7.0      2021-04-20 [1] CRAN (R 4.1.0)
 cli           3.2.0      2022-02-14 [1] CRAN (R 4.1.2)
 colorspace    2.0-3      2022-02-21 [1] CRAN (R 4.1.2)
 crayon        1.5.1      2022-03-26 [1] CRAN (R 4.1.0)
 DBI           1.1.2      2021-12-20 [1] CRAN (R 4.1.0)
 desc          1.4.1      2022-03-06 [1] CRAN (R 4.1.2)
 devtools    * 2.4.3      2021-11-30 [1] CRAN (R 4.1.0)
 digest        0.6.29     2021-12-01 [1] CRAN (R 4.1.0)
 dplyr         1.0.8      2022-02-08 [1] CRAN (R 4.1.2)
 ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
 evaluate      0.15       2022-02-18 [1] CRAN (R 4.1.2)
 fansi         1.0.3      2022-03-24 [1] CRAN (R 4.1.2)
 fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)
 fs            1.5.2      2021-12-08 [1] CRAN (R 4.1.0)
 generics      0.1.2      2022-01-31 [1] CRAN (R 4.1.2)
 ggplot2       3.3.5      2021-06-25 [1] CRAN (R 4.1.0)
 ggthemes    * 4.2.4      2021-01-20 [1] CRAN (R 4.1.0)
 glue          1.6.2      2022-02-24 [1] CRAN (R 4.1.2)
 gtable        0.3.0      2019-03-25 [1] CRAN (R 4.1.0)
 htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.0)
 jquerylib     0.1.4      2021-04-26 [1] CRAN (R 4.1.0)
 jsonlite      1.8.0      2022-02-22 [1] CRAN (R 4.1.2)
 knitr         1.38       2022-03-25 [1] CRAN (R 4.1.0)
 lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.0)
 magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.1.2)
 memoise       2.0.1      2021-11-26 [1] CRAN (R 4.1.0)
 munsell       0.5.0.9000 2021-10-19 [1] Github (cwickham/munsell@e539541)
 pillar        1.7.0      2022-02-01 [1] CRAN (R 4.1.2)
 pkgbuild      1.3.1      2021-12-20 [1] CRAN (R 4.1.0)
 pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
 pkgload       1.2.4      2021-11-30 [1] CRAN (R 4.1.0)
 prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.1.0)
 processx      3.5.3      2022-03-25 [1] CRAN (R 4.1.0)
 ps            1.6.0      2021-02-28 [1] CRAN (R 4.1.0)
 purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.0)
 remotes       2.4.2      2021-11-30 [1] CRAN (R 4.1.0)
 rlang         1.0.2      2022-03-04 [1] CRAN (R 4.1.2)
 rmarkdown     2.13       2022-03-10 [1] CRAN (R 4.1.2)
 rprojroot     2.0.3      2022-04-02 [1] CRAN (R 4.1.0)
 rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)
 sass          0.4.1      2022-03-23 [1] CRAN (R 4.1.2)
 scales        1.1.1      2020-05-11 [1] CRAN (R 4.1.0)
 sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.0)
 stringi       1.7.6      2021-11-29 [1] CRAN (R 4.1.0)
 stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.0)
 testthat      3.1.3      2022-03-29 [1] CRAN (R 4.1.2)
 tibble        3.1.6      2021-11-07 [1] CRAN (R 4.1.0)
 tidyselect    1.1.2      2022-02-21 [1] CRAN (R 4.1.2)
 usethis     * 2.1.5      2021-12-09 [1] CRAN (R 4.1.0)
 utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
 vctrs         0.4.0      2022-03-30 [1] CRAN (R 4.1.2)
 withr         2.5.0      2022-03-03 [1] CRAN (R 4.1.0)
 xfun          0.30       2022-03-02 [1] CRAN (R 4.1.2)
 yaml          2.3.5      2022-02-21 [1] CRAN (R 4.1.2)

 [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────