R List | How to Create, Access, Manipulate, Merge Lists in R?

1. R List – Objective

In our previous R tutorial, we have discussed what is R Data Types in detail. Now in this R programming tutorial, we will see one of the R data Types i.e. R List in detail. First of all, we will learn what is R List, then we will also discuss how to Create a list in R, how to Access R list, how to modify lists in R with the help of examples.

R List | How to Create, Access, Manipulate, Merge Lists in R?

R List | How to Create, Access, Manipulate, Merge Lists in R?

2. What is R List?

R List is the object which Contains elements of different types – like strings, numbers, vectors and another list inside it. R list can also contain a matrix or a function as its elements. The List is been created using list() Function in R. In other words, a list is a generic vector containing other objects.

For Example:

The variable x is containing copies of three vectors n, s, b and a numeric value 3.

n = c(2, 3, 5)
s =  c(“aa”,  “bb”,  “cc”,  “dd”, “ee” )
b = c(TRUE,  FALSE,  TRUE,  FALSE,  FALSE )
x = list( n, s, b, 3)    # x contains copies of n, s, b

3. How to Create Lists in R Programming?

In this section of R List tutorial, we will create R List with the help of an example.

Let’s create a list containing string, numbers, vectors and logical values.

For Example:

List_data <- list("Green", "Yellow", c(5,6,7), TRUE, 51.2)
print(list_data)

When we execute the above code, it produces the following result-

[[1]]
[1] “Green”
[[2]]
[1] “Yellow”
[[3]]
[1] 5, 6, 7
[[4]]
[1] TRUE
[[5]]
[1] 51.2

4. How to Name List Elements in R Language?

In this section, we will discuss to name the R List elements with the help of an example.

Let’s create a list containing a vector, a matrix, and a list.

For Example:

list_data <- list(c("Feb","Mar","Apr"), matrix(c(3,9,5,1,-2,8), nrow = 2),   list("green",12.3))

Give names to the elements in the list.

names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

Show the list.

print(list_data) 

When we execute the above code, it produces the following

Result-

$1<sup>st</sup> Quarter’[1] “Feb”, “Mar”, “Apr”
$A Matrix
[,1] [,2] [,3]
[1,]      3     5   -2
[2,]      9     1    8
$A_Inner_list
$A_Inner_list [[1]]
[1] “Green”
$A_Inner_list [[2]]
[1] “12.3”

5. How to Access R List Elements?

Let’s now understand how to access Lists elements in R programming?

Create an R list containing a vector, a list and a matrix. list_data <- list(c(“Feb”,”Mar”,”Apr)) list(“white”,13.4)), matrix(c(3,9,5,1,-2,8), nrow = 2)

For Example:

Give names to the elements in the list.

Names(list_data) <- c(“1<sup>st </sup>Quarter”, “A Matrix”, “A Inner list”)

Access the first element of the list.

print(list_data[1])

Access the third element. As it also a list, all its elements will print.

Print(list_data[3])

By using the name of the element access the list elements.

Print(list_data$A Matrix)

It will produce the following result after executing the above code-

$”1st Quarter” [1] “Feb”, Mar”, Apr”

$A_Inner_list

$A_Inner_list[[1]]

[1] “White”

$A_Inner_list[[2]]

[1] 13.4

$ “A Matrix” [1]

[1]   [2]   [3]

[1]     3     5    -2

[2]     9     1     8

6. How to Manipulate List elements in R Programming?

Let’s now discuss how to manipulate the R List elements with the help of an example.

Create a list containing a vector, a matrix and a list.list_data <- list(c(“Feb”,”Mar”,”Apr”), matrix(c(3,9,5,1,-2,8), nrow = 2),list(“green”,12.3))

For Example:

Give names to the elements in the list.

names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

Add an element at the end of the list.

list_data[4] <- "New element"print(list_data[4])

Remove the last element.

list_data[4] <- NULL # Print the 4th Element.print(list_data[4])

Update the 3rd Element.

list_data[3] <- "updated element"print(list_data[3])

When we execute the above code, it produces the following result.

[[1]]

[1] "New element"

$NULL

$`A Inner list`

[1] "updated element"

7. How to Merge Lists in R Programming language?

We can merge many lists into one list by placing all the lists element inside one list() function.

For Example:

Create two lists.

list1 <- list(1,2,3)list2 <- list("Mon","Tue","Wed")

Merge the two lists.

list <- c(list1,list2) # Print the merged list.print(merged.list)

When we execute the above code, it produces the following result.

[[1]]

[1] 1

[[2]]

[1] 2

[[3]]

[1] 3

[[4]]

[1] "Mon"

[[5]]

[1] "Tue"

[[6]]

[1] "Wed"

8. How to Convert R 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 apply after the list is converted into vector. To do this conversion, we can use the unlist() function. It takes the list as input and produces a vector.

For example:

Create lists.

list1 <- list(2:6)
print(list1)
list2 <-list(11:15)
print(list2)

Convert the lists to vectors.

v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)

Now add the vectors

result <- v1+v2
print(result)

When we execute the above code, it produces the following result −

[[1]]
[1] 2 3 4 5 6
[[1]]
[1]11 12 13 14 15
[1] 2 3 4 5 6
[1]11 12 13 14 15
[1] 13 15 17 19 21

9. How to Generate Lists in R?

We can use a colon to generate a list of numbers.

For example:

-3:3
[1] -3 -2 -1 0 1 2 3

10. Operating on lists in R

R allows to Operate on all list values at once.

For example:

c(1,3,5) + 4

This and the Apply function allow you to avoid most for loops.

[1] 5, 7, 9

11. R Predefined Lists

Lists for letters and month names are predefined:

letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o"
[16] "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O"
[16] "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
month.abb
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct"
[11] "Nov" "Dec"
month.name
[1] "January"   "February"  "March"     "April"     "May"
[6] "June"      "July"      "August"    "September" "October"
[11] "November"  "December"

R Quiz

12. c function in R

The c Function in Rcombines the parameter into a list and converts them to the same type.

For example:

c("test",4)
[1] "test" "4"
typeof("4")
[1] "character"

Here 4 is converts into a string.

So, this was all in R list tutorial. Hope you like our explanation.

13. Conclusion – R List

Hence, we have studied the R list in the above-mentioned information. We all are aware, that lists are the object which contains elements of different types like strings, numbers, and vectors. Thus it is necessary to learn how to apply different operations on list elements.
If you like this post on R list, or you have any query to understand Lists in R. So, please drop me a comment below.

See Also-

Reference for R 

Leave a Reply

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