In the R language, a list is an object that consists of an ordered collection of objects known as its components. A list in R Language is structured data that can have any number of modes (types) or other structured data. That is, one can put any kind of object (like vector, data frame, character object, matrix, and/ or array) into one list object.
Table of Contents
Lists in R are versatile data structures that can store elements of different types (numbers, strings, vectors, and even other lists). Unlike vectors, lists can hold mixed data types, This makes them essential for data manipulation, statistical modeling, and handling complex nested data (like JSON).
Key Features of Lists
The following are key features of lists. The lists in R
✅ Can store mixed data types (numbers, strings, vectors, data frames, etc.)
✅ Support named elements (like dictionaries in Python)
✅ Allow nesting (lists inside lists)
✅ Used by many R functions (e.g., lm()
, t.test()
) to return multiple outputs
Example of list in R Language
One can create a list in R by using the list()
function to create a list. An example of a list in R language is as follows
x <- list(c(1,2,3,5), c("a", "b", "c", "d"), c (T, T, F, T, F), matrix(1:9, nr = 3) )
The list $x$ contains 4 components, three of which are vectors (numeric, string, and logical) and one of which is a matrix.
Converting Objects to Lists in R Language
An object can also be converted to a list by using the as.list( ) function. For a vector, the disadvantage is that each element of the vector becomes a component of that list. For example,
as.list(1: 10)
Named Lists (Key-Value Pairs)
One can assign names to elements of a list for easy access:
# Creating a named list in R employee <- list( name = "Imdad", age = 40, skills = c("R", "Python", "SQL"), is_manager = FALSE ) print(employee) ## OUTPUT $name [1] "Imdad" $age [1] 40 $skills [1] "R" "Python" "SQL" $is_manager [1] FALSE
Extract Components from a list
The operator [[ ]] (double square bracket) is used to extract the components of a list. To extract the second component of the list, one can write at R prompt,
list[[2]]
Using the [ ] operator returns a list rather than the structured data (the component of the list). The components of the list need not be of the same mode. The components are always numbered. If x1 is the name of a list with four components, then individual components may be referred to as x1[[1]], x1[[2]], x1[[3]], and x1[[4]].
If components of a list are defined then these components can be extracted by using the names of components. For example, a list with named components is
x1 <- list(a = c(1,2,3,5), b = c("a", "b", "c", "d"), c = c(T, T, F, T, F), d = matrix(1:9, nr = 3) )
To extract the component a, one can write
x1$a x1["a"] x1[["a"]]
To extract more than one component, one can write
# Extracting elements of a list x[c(1,2)]Â Â Â #extract component one and two x[-1]Â Â Â Â Â Â Â Â #extract all component except 1st x[[c(2,2)]] #extract 2nd element of component two x[[c(2:4)]] #extract all elements of component 2 to 4
Useful List Functions
The following are some useful functions for lists in R Language.
Function | Description | List Example in R |
---|---|---|
length() | Number of elements | length(my_list) |
names() | Get/set names | names(person) |
unlist() | Convert list to vector | unlist(my_list) |
lapply() | Apply function to each element | lapply(my_list, class) |
When to Use Lists vs. Vectors/ Data Frames in R
✔ Use lists when:
- Storing mixed data types (e.g., strings + numbers).
- Working with nested/hierarchical data (e.g., JSON-like structures).
- Functions return multiple outputs (e.g.,
lm()
results).
✔ Use vectors/ data frames when:
- All elements are of the same type.
- Working with tabular data.
Lists are powerful for handling complex and heterogeneous data in R. Mastering them helps in data wrangling, API responses, and machine learning workflows.