R - Lists

Introduction

Lists are the objects which contain elements of different types like numbers, strings, vectors, data frames and another list inside it. A list can also contain a matrix or even function as its elements. List is created using list() function. Lists are typically used for organizing data rather than processing it.

Creating a List

Lists are created using the list() function. Following is an example to create a list containing strings, numbers, vectors and a logical values.
# Create a list containing strings, numbers, vectors and a logical
# values.
> list_data <- list(mtcars[1:5,], c('A', 'Sample', 'Vector'), c(21,32,11), TRUE, 51.23, 119.1)
> list_data
[[1]]
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

[[2]]
[1] "A"      "Sample" "Vector"

[[3]]
[1] 21 32 11

[[4]]
[1] TRUE

[[5]]
[1] 51.23

[[6]]
[1] 119.1

>
As you can see, each item in the list is associated with an index number that is shown as [[1]], [[2]]. We can also assign names to these elements.

Naming List Elements

The list elements can be given names and they can be accessed using these names.
> # Create a list containing a vector, a matrix and a list.
> list_data <- list(df = mtcars[1:5,], vec1 = c('A', 'Sample', 'Vector'), vec2 = c(21,32,11), bln = TRUE, num1 = 51.23, num2 = 119.1)
> list_data
$df
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

$vec1
[1] "A"      "Sample" "Vector"

$vec2
[1] 21 32 11

$bln
[1] TRUE

$num1
[1] 51.23

$num2
[1] 119.1

>
You can check for the names in a list using
> names(list_data)
[1] "df"   "vec1" "vec2" "bln"  "num1" "num2"
Now we can also assign names to these objects
> names(list_data) <- c("Data Frame", "Vector 1", "Vector 2", "Boolean", "Number 1", "Number 2")
>
This updates the names of the list elements
> list_data
$`Data Frame`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

$`Vector 1`
[1] "A"      "Sample" "Vector"

$`Vector 2`
[1] 21 32 11

$Boolean
[1] TRUE

$`Number 1`
[1] 51.23

$`Number 2`
[1] 119.1

>

Accessing List Elements

Elements of the list can be accessed by the index of the element in the list. In case of named lists it can also be accessed using the names.
> list_data[1]
$`Data Frame`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

> 

> list_data[1]
$`Data Frame`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

> 
> list_data$`Vector 1`
[1] "A"      "Sample" "Vector"
>

Manipulating List Elements

We can add, delete and update list elements as shown below. We can add only at the end of a list. But we can update/delete any element.
> list_data[4] <- NULL
> list_data
$`Data Frame`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

$`Vector 1`
[1] "A"      "Sample" "Vector"

$`Vector 2`
[1] 21 32 11

$`Number 1`
[1] 51.23

$`Number 2`
[1] 119.1

>

Merging Lists

You can merge many lists into one list by placing all the lists inside one list() function.
# Create two lists.
list1 <- list(1,2,3)
list2 <- list("Sun","Mon","Tue")

# Merge the two lists.
merged.list <- c(list1,list2)

# Print the merged list.
merged.list

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] "Sun"

[[5]]
[1] "Mon"

[[6]]
[1] "Tue"

Converting List to Vector

A list can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be applied after the list is converted into vectors. To do this conversion, we use the unlist() function. It takes the list as input and produces a vector.
> list1 <- list(1:5)
> list1
[[1]]
[1] 1 2 3 4 5

> 
> list2 <-list(10:14)
> list2
[[1]]
[1] 10 11 12 13 14

> 
> v1 <- unlist(list1)
> v2 <- unlist(list2)
> 
> v1
[1] 1 2 3 4 5
> v2
[1] 10 11 12 13 14
> 
> result <- v1+v2
> result
[1] 11 13 15 17 19
>
As mentioned before, Lists are mainly used for organizing data and not so much for processing. They provide an excellent tool for organising data. 

References

If you are interested in going deeper, check out this video tutorial on YouTube.
If you like books, check this out.

Comments