3 min read•august 9, 2024
Lists in R are versatile data structures that can hold various types of objects. They're perfect for organizing complex data, allowing you to store different data types together in one container. This flexibility makes lists a go-to choice for many R programmers.
In this section, we'll cover list basics, accessing and modifying lists, and advanced operations. You'll learn how to create, manipulate, and work with lists effectively, building a strong foundation for handling complex data structures in R.
[list()](https://www.fiveableKeyTerm:list())
function, allowing combination of various objects (, matrices, )names()
function or during list creation[unlist()](https://www.fiveableKeyTerm:unlist())
function, transforming complex structures into simpler formslist(name1 = value1, name2 = value2)
[$](https://www.fiveableKeyTerm:$)
operator or double square brackets with the element name as a stringNULL
names()
function to retrieve or modify the names of list elements[c()](https://www.fiveableKeyTerm:c())
function or by direct assignmentc()
or [append()](https://www.fiveableKeyTerm:append())
functions[lapply()](https://www.fiveableKeyTerm:lapply())
or [sapply()](https://www.fiveableKeyTerm:sapply())
for element-wise operationsunlist()
with appropriate arguments[]
to return a sublist[[[](https://www.fiveableKeyTerm:[[)]]
or the $
operator for named elementsmylist[[1]]
)mylist[[1]][[2]]
)mylist[c(TRUE, FALSE, TRUE)]
)NULL
or using negative indexingmylist[[1]][[2]] <- new_value
)[length](https://www.fiveableKeyTerm:length)()
to determine the number of top-level elements in a liststr()
function to display the structure and contents of a listis.list()
to check if an object qualifies as a listas.list()
to convert other data structures into list format[rapply()](https://www.fiveableKeyTerm:rapply())
for recursive application of functions to nested list elementslapply()
to apply a function to each element of a list, returning a listsapply()
for simplified output, attempting to return vectors or matrices when possiblemapply()
for multivariate functional application across multiple listslapply()
in combination with anonymous functionspurrr
package functions like map()
and reduce()
for advanced list operationsc()
or append()
functionsunlist()
, specifying recursive = TRUE
for deep nesting[split()](https://www.fiveableKeyTerm:split())
to divide a vector into a list based on a factor[purrr::transpose()](https://www.fiveableKeyTerm:purrr::transpose())
or base R equivalent[do.call()](https://www.fiveableKeyTerm:do.call())
to apply a function to a list of arguments, useful for dynamic function callsrapply()
for recursive application of functions to all elements, including those in nested listsrlist
package for advanced list manipulation and querying of complex list structures