In R, a list’s components can be of any mode or type. Lists are objects that consist of an ordered collection of objects.
Confused?
In simple terms, lists are vectors that can contain elements of any type.
R list can contain a string, a numeric variable, a vector, a matrix, an array, a function, and even another list.
In this R list tutorial, we will explore the lists in the R programming language. We will learn how to create them and how to name their components. We will also learn how to index them, and how to manipulate their components. Our schedule is packed, so, let’s get going!
How to Create Lists in R?
We can use the list() function to create a list.
For example:
Code:
> list1 <- list(2, "hello", c(3,5,4), 1:5, list(FALSE, c("this", "is","a","list"),c(FALSE,TRUE,TRUE,TRUE,FALSE))) > str(list1) #displays the structure of an object
Output:
$ : num 2
$ : chr “hello”
$ : num [1:3] 3 5 4
$ : int [1:5] 1 2 3 4 5
$ :List of 3
..$ : logi FALSE
..$ : chr [1:4] “this” “is” “a” “list”
..$ : logi [1:5] FALSE TRUE TRUE TRUE FALSE
Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.
Code:
> list2 <- c(c(1,2),"techvidvan",list(c(FALSE,TRUE),rep(c(1,2,3),4))) > str(list2)
Output:
$ : num 1
$ : num 2
$ : chr “techvidvan”
$ : logi [1:2] FALSE TRUE
$ : num [1:12] 1 2 3 1 2 3 1 2 3 1 …
Notice that the vector and the list inside the c() function are not separate vector and list inside list2. Instead, their components have become list2’s components. The c() function breaks down any objects with multiple elements.
The c() function can combine two or more lists into one as well.
Code:
> list3 <- c(list1,list2) #combines list1 and list2 into list3 > str(list3)
Output:
$ : num 2
$ : chr “hello”
$ : num [1:3] 3 5 4
$ : int [1:5] 1 2 3 4 5
$ :List of 3
..$ : logi FALSE
..$ : chr [1:4] “this” “is” “a” “list”
..$ : logi [1:5] FALSE TRUE TRUE TRUE FALSE
$ : num 1
$ : num 2
$ : chr “techvidvan”
$ : logi [1:2] FALSE TRUE
$ : num [1:12] 1 2 3 1 2 3 1 2 3 1 …
How to Name R List’s Components?
We can name a list’s components while creating it. This is similar to naming vector elements.
Code:
> named_list1 <- list("name"="Bob","wife name"="julie","age"= 35,"children"=3,"children age"=c(4,7,9)) > str(named_list1)
Output:
$ name : chr “Bob”
$ wife name : chr “julie”
$ age : num 35
$ children : num 3
$ children age: num [1:3] 4 7 9
We can also name a list’s components after its creation by using the names() function.
Code:
> details <- list("jim","jenny",42,2,c(14,17)) > str(details)
Output:
$ : chr “jim”
$ : chr “jenny”
$ : num 42
$ : num 2
$ : num [1:2] 14 17
Code:
> names(details) <- c("name","wife name","age","children", "children age") > str(details)
Output:
$ name : chr “jim”
$ wife name : chr “jenny”
$ age : num 42
$ children : num 2
$ children age: num [1:2] 14 17
Indexing a List
In a list, components are always numbered. We can refer to the components using those in double square brackets. So, we can refer to list1’s components as list1[[1]], list1[[2]], etc..
If there are elements within a list’s components, then we can refer to them as list1[[1]][1] and so on.
We can refer to an R list’s components in a similar fashion to vectors by using logical, integer or character vectors.
Using logical vectors:
Code:
> list2[c(F,F,T,F,T)] #index using logical vector
Output:
[1] “techvidvan”[[2]]
[1] 1 2 3 1 2 3 1 2 3 1 2 3
Using positive integers:
Code:
> list3[c(3:4)] #index using positive integer vector
Output:
[1] 3 5 4
[[2]]
[1] 1 2 3 4 5
Using negative integers:
Code:
> list1[c(-2,-3,-5)] #index using negative integers
Output:
[1] 2[[2]]
[1] 1 2 3 4 5
Using character vectors for named lists:
Code:
> named_list1[c("name","age")] #index using character vector
Output:
[1] “Bob”$age
[1] 35
How to Manipulate list in R?
We can change existing components in a list. We can also add new components and delete existing ones as well.
Modifying a component:
We can modify a component by accessing them through indexing and reassigning the new value.
Code:
> details[["name"]] <- "harry" > str(details)
Output:
$ name : chr “harry”
$ wife name : chr “jenny”
$ age : num 42
$ children : num 2
$ children age: num [1:2] 14 17
Adding a new component:
We can add a new component by assigning it to an unused index.
Code:
> list1[[6]] <- c(3,5,4,2,5,6) > str(list1)
Output:
$ : num 2
$ : chr “hello”
$ : num [1:3] 3 5 4
$ : int [1:5] 1 2 3 4 5
$ :List of 3
..$ : logi FALSE
..$ : chr [1:4] “this” “is” “a” “list”
..$ : logi [1:5] FALSE TRUE TRUE TRUE FALSE
$ : num [1:6] 3 5 4 2 5 6
Deleting a component:
We can delete a component by reassigning it to NULL.
Code:
> list1[[6]] <-NULL > str(list1)
Output:
$ : num 2
$ : chr “hello”
$ : num [1:3] 3 5 4
$ : int [1:5] 1 2 3 4 5
$ :List of 3
..$ : logi FALSE
..$ : chr [1:4] “this” “is” “a” “list”
..$ : logi [1:5] FALSE TRUE TRUE TRUE FALSE
Summary
R Lists are very versatile data structures due to the lack of any restrictions placed on them. In this R list tutorial, we learn about lists. We learned about the different ways to create them. We learned how to access their components. We also learned how to modify, add, and delete components in a list.
We hope that this article helped you understand lists in R and their workings.
If you really like our efforts please comment down below and do check our sidebar for everything on R.
Keep Learning!!