# Lists in R

In this tutorial, you will learn about what is lists in R?, how to create list in R?, and how to access variable(s) and/or observation(s ) from a list?

## What is list in R?

Lists are the more general class of data storage. Lists are generic vectors where each element can be any type of object; e.g., a vector (of any mode), a matrix, a data frame or a function. Because of this flexibility, lists are the basis for most complex objects in R.

## How to create list in R?

In R, list can be created using the list() function.

### Creating an empty list in R

An empty list can be created using list() function without any arguments.

List_01 <- list()
List_01
list()

Another way to create an empty list with specific length is to use vector() function with list as first argument and length as second argument.

List_02 <- vector("list", length = 4)
List_02
[]
NULL

[]
NULL

[]
NULL

[]
NULL

Like vectors, lists group the data into a one-dimension set. In vector we can store similar kind of objects, but in list we can store all kind of objects.

### Creating a list in R

The list in R can be created using list() function by specifying the different components (either named or unnamed) of list.

The function to create list in R is

list(name_1 = object_1,
name_2 = object_2,
...,
name_m = object_m)

If names are omitted while creating a list, the component of lists are numbered only.

list1 <- list(1.34, c("Bob", "John"), TRUE, 1 + 2i)
list1
[]
 1.34

[]
 "Bob"  "John"

[]
 TRUE

[]
 1+2i

Every component of a list is a vector. In the above example, the double bracket [] is the first component of the list having one element and [] is the second component of a list having two elements.

Note that the list function works like concatenation function c(). The c() function combine the objects to create a vector. The list() function combines the objects to create a list.

# display the no. of top level components
length(list1)
 4
# display the mode of list1
mode(list1)  
 "list"
is.list(list1)
 TRUE

### Creating Named List in R

Named list can be created using list() function by specifying the names to each component of a list.

list2 <- list(value = 1.34,
names = c("Bob", "John"), TRUE, 1 + 2i)
list2
$value  1.34$names
 "Bob"  "John"

[]
 TRUE

[]
 1+2i

### Combining two or more lists

Two or more lists can be combined using concatenation function c().

List01 <- list(
A = 1:4,
B = c("Male", "Female"),
C = matrix(1:4, nrow = 2)
)
List02 <- list(P = 1:5, Q = c(TRUE, FALSE))
New_List01 <- c(List01, List02)
New_List01
$A  1 2 3 4$B
 "Male"   "Female"

$C [,1] [,2] [1,] 1 3 [2,] 2 4$P
 1 2 3 4 5

$Q  TRUE FALSE Two or more lists can also be combined using append() function. The append() function is used to add elements to a vector but it can also be used for list too. New_List02 <- append(List01, List02) New_List02 $A
 1 2 3 4

$B  "Male" "Female"$C
[,1] [,2]
[1,]    1    3
[2,]    2    4

$P  1 2 3 4 5$Q
  TRUE FALSE

## Accessing elements of list

### Accessing Components of a list

The components in a list are given numbers or given names. Hence the components may be referred by its number or name.

The component of a list is referred by

• its number like list2[] or
• its name like list2$name or • its name like list2[["name"]] The element of component is referred by • list2[] or • list2$name.
list2
$names  "Bob" "John" list2[]   "Bob" list2[["value"]]  1.34 # display component whose name is 'names' list2$names    
 "Bob"  "John"
# display 1st element of component whose name is 'names'
list2$names   "Bob" list2[c(1, 3)] $value
 1.34

[]
 TRUE
list2[-1]
$names  "Bob" "John" []  TRUE []  1+2i # display 1st element from 2nd component of list2 list2[]  "Bob" # display all elements of 2nd component list2 $names
 "Bob"  "John"
# display all elements of 2nd component
list2[]  
 "Bob"  "John"

## Membership and Coercion function for list

The membership and coercion functions for lists are as follows:

• is.list(x) : Check whether x is lists
• as.list(x) : Convert x to lists

Earlier we have created a list New_List01. To check whether it is a list or not, we can use is.list() function.

is.list(New_List01)
 TRUE

Suppose we have a numeric vector x. To convert a numeric vector to list, we can use as.list() function.

x <- c(10, 20, 25)
x
 10 20 25
# check whether x is list
is.list(x)
 FALSE
# convert x to list
y <- as.list(x)
y
[]
 10

[]
 20

[]
 25
# check whether y is list
is.list(y)
 TRUE

## Endnote

In this tutorial you learned about what is list in R, how to create list in R and how to access elements of list using different methods.