R List – How to create, index and manipulate list components

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:

List of 5
$ : 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

R list - creating a list

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:

List of 5
$ : num 1
$ : num 2
$ : chr “techvidvan”
$ : logi [1:2] FALSE TRUE
$ : num [1:12] 1 2 3 1 2 3 1 2 3 1 …

R list creating

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:

List of 10
$ : 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 …

creating a list combine R list

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:

List of 5
$ name : chr “Bob”
$ wife name : chr “julie”
$ age : num 35
$ children : num 3
$ children age: num [1:3] 4 7 9

naming R lists

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:

List of 5
$ : 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:

List of 5
$ name : chr “jim”
$ wife name : chr “jenny”
$ age : num 42
$ children : num 2
$ children age: num [1:2] 14 17
lists - naming list names details

naming R list details

Follow TechVidvan on Google & Stay updated with latest technology trends

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]]
[1] “techvidvan”[[2]]
[1] 1 2 3 1 2 3 1 2 3 1 2 3

indexing R list using logical vector

Using positive integers:

Code:

> list3[c(3:4)]        #index using positive integer vector

Output:

[[1]]
[1] 3 5 4
[[2]]
[1] 1 2 3 4 5

indexing R list using positive integers

Using negative integers:

Code:

> list1[c(-2,-3,-5)]   #index using negative integers

Output:

[[1]]
[1] 2[[2]]
[1] 1 2 3 4 5

indexing R list using negative integers

Using character vectors for named lists:

Code:

> named_list1[c("name","age")] #index using character vector

Output:

$name
[1] “Bob”$age
[1] 35

 indexing R list using character vectors

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:

List of 5
$ name : chr “harry”
$ wife name : chr “jenny”
$ age : num 42
$ children : num 2
$ children age: num [1:2] 14 17

modify R list component

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:

List of 6
$ : 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

R list adding new component

Deleting a component:

We can delete a component by reassigning it to NULL.

Code:

> list1[[6]] <-NULL 
> str(list1)

Output:

List of 5
$ : 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

R list deleting components

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!!

Did you like our efforts? If Yes, please give TechVidvan 5 Stars on Google | Facebook

1 Response

  1. Unnikrishnan says:

    Yours is one of the most outstanding web based tutorials I have ever seen. This helps a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *