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.

lists in R
lists 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
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
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]]
[1] 1.34

[[2]]
[1] "Bob"  "John"

[[3]]
[1] TRUE

[[4]]
[1] 1+2i

Every component of a list is a vector. In the above example, the double bracket [[1]] is the first component of the list having one element and [[2]] 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)
[1] 4
# display the mode of list1
mode(list1)  
[1] "list"
is.list(list1)
[1] 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] 1.34

$names
[1] "Bob"  "John"

[[3]]
[1] TRUE

[[4]]
[1] 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] 1 2 3 4

$B
[1] "Male"   "Female"

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

$P
[1] 1 2 3 4 5

$Q
[1]  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] 1 2 3 4

$B
[1] "Male"   "Female"

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

$P
[1] 1 2 3 4 5

$Q
[1]  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[[2]] or
  • its name like list2$name or
  • its name like list2[["name"]]

The element of component is referred by

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

[[2]]
[1] TRUE
list2[-1]
$names
[1] "Bob"  "John"

[[2]]
[1] TRUE

[[3]]
[1] 1+2i
# display 1st element from 2nd component of list2
list2[[2]][1]
[1] "Bob"
# display all elements of 2nd component
list2[2]
$names
[1] "Bob"  "John"
# display all elements of 2nd component
list2[[2]]  
[1] "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)
[1] 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
[1] 10 20 25
# check whether x is list
is.list(x)
[1] FALSE
# convert x to list
y <- as.list(x)
y
[[1]]
[1] 10

[[2]]
[1] 20

[[3]]
[1] 25
# check whether y is list
is.list(y)
[1] 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.

Learn more about data structures in R refer to the following tutorials:

Hope you enjoyed learning list in R. The content is more than sufficient to understand list in R.

Leave a Comment