R Beginner Tutorial – Arrays
R Academy Menu ggplot2 basics: learn ggplot2 in 15 minutes!R Beginner TutorialR Beginner Tutorial – Basic SyntaxR Beginner Tutorial – R ObjectsR Beginner Tutorial –
Home » R Beginner Tutorial – Lists
In previous post, we learned vectors are the most basic data structures. They are building block of other data structures. Vectors can only contain one type of elements. Lists are similar to vectors but more versatile. Lists can contain elements of all kinds of data types: numbers, character, vectors and even another lists, matrices or functions.
Let’s review how lists compare to other data structures.
As seen on the image above, lists can contain all types of elements. Vectors, matrices and arrays must contain vectors of the same type, while data frames and lists can contain vectors of different types.
We create lists using list() function. Let’s create a simple list of numbers. When printed, each element is printed in it’s own row.
> # Create a simple list.
> myList = list(1, 2, 3)
> myList
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
The great thing about lists is they can contain elements of different data types. Let’s create a list containing character, numeric, vectors and a logical values.
> # Create a list containing strings, numbers, vectors and logical values.
> myList = list("RStudio", 3, c(0, 1, 3, 5), TRUE)
> myList
[[1]]
[1] "RStudio"
[[2]]
[1] 3
[[3]]
[1] 0 1 3 5
[[4]]
[1] TRUE
> # Create a list containing a vector, a matrix and a list.
> myList = list(c("Green", "Blue", "Yellow"), matrix(c(1, 2, 3, 4, 5, 6), nrow = 3), list("Red", 0.3))
> myList
[[1]]
[1] "Green" "Blue" "Yellow"
[[2]]
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
[[3]]
[[3]][[1]]
[1] "Red"
[[3]][[2]]
[1] 0.3
To access list element, use squared brackets. For example, first element of list called myList is accessed by myList[1].
> # Access the first element of the list (a vector).
> myList[1]
[[1]]
[1] "Green" "Blue" "Yellow"
> # Access the second element (a matrix).
> myList[2]
[[1]]
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> # Access the third element (a list).
> myList[3]
[[1]]
[[1]][[1]]
[1] "Red"
[[1]][[2]]
[1] 0.3
We can also name elements of the list and access them using names.
> # Name elements in the list.
> names(myList) = c("Vector", "Matrix", "Nested list")
> # Access the list element using the name of the element.
> myList$Matrix
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
To add new element, simply use square brackets and index of the new element. For example, to add a fourth element to myList, use myList[4] = “New element”.
> # Add element at the end of the list.
> myList[4] = "New element"
> myList
$Colors
[1] "Green" "Blue" "Yellow"
$Matrix
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
$`Nested list`
$`Nested list`[[1]]
[1] "Red"
$`Nested list`[[2]]
[1] 0.3
[[4]]
[1] "New element"
To update an element, assign the new value with square brackets and index of the new element. For example, to update the third element of myList, use myList[3] = “Updated element”.
> # Update the 3rd Element.
> myList[3] = "Updated element"
> myList
$Colors
[1] "Green" "Blue" "Yellow"
$Matrix
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
$`Nested list`
[1] "Updated element"
Negative index drops an element. For example, to remove the fourth element from myList, use myList[-4].
> # Remove fourth element.
> myList = myList[-4]
> myList
$Colors
[1] "Green" "Blue" "Yellow"
$Matrix
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
$`Nested list`
$`Nested list`[[1]]
[1] "Red"
$`Nested list`[[2]]
[1] 0.3
Concatenate two lists by creating a vector or a list that contains both lists. For example, we can concatenate list1 and list2 by creating list3 = c(list1, list2).
> # Create two lists.
> list1 = list(1, 2, 3)
> list2 = list(4, 5, 6)
> list1
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
> list2
[[1]]
[1] 4
[[2]]
[1] 5
[[3]]
[1] 6
> # Concatenate two lists.
> list3 = c(list1, list2)
> list3
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
[[5]]
[1] 5
[[6]]
[1] 6
Create a sequence using the start value, colon and end value. Integer values are returned, including start and an end value.
> # Create lists.
> myList = list(1:5)
> myList
[[1]]
[1] 1 2 3 4 5
> myList = list(10:14)
> myList
[[1]]
[1] 10 11 12 13 14
Convert list to a vector using unlist() function.
> # Convert the lists to vectors.
> v1 = unlist(list1)
> v2 = unlist(list2)
> v1
[1] 1 2 3
> v2
[1] 4 5 6
Meanwhile, in a hurry? If you’re already running late, we recommend you head over to our two R crash courses:
For even more amazing tips check out our awesome ExcelOlympics YouTube channel!
1 Ceballos, Maite and Nicolás Cardiel. 2013. “Data structure.” First Steps in R. Accessed 2021-10-12.