R List – Learn what all you can do with Lists in R!
Now in this R programming DataFlair tutorial series, we will see one of the major R data types that is R list in detail. First of all, we will learn about R list, then we will discuss how to create, access and modify lists in R with the help of examples.
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 created using the list() function in R. In other words, a list is a generic vector containing other objects.
The variable x is containing copies of three vectors n, s, b and a numeric value 3.
#Author DataFlair vec <- c(1,2,3) char_vec <- c("Hadoop", "Spark", "Flink", "Mahout") logic_vec <- c(TRUE, FALSE, TRUE, FALSE) out_list <- list(vec, char_vec, logic_vec) out_list
Stay updated with latest technology trends
Join DataFlair on Telegram!!
Wait! Have you checked – Guide on R Vector Operations
How to Create Lists in R Programming
In this section of the 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.
#Author DataFlair list_data <- list("Red", "White", c(1,2,3), TRUE, 22.4) print(list_data)
How to Name List Elements in R Language
In this section, we will learn to name the R list elements with the help of an example.
Let’s create a list containing a vector, matrix, and list.
#Author DataFlair data_list <- list(c("Jan","Feb","Mar"), matrix(c(1,2,3,4,-1,9), nrow = 2),list("Red",12.3)) names(data_list) <- c("Monat", "Matrix", "Misc") print(data_list)
You must definitely learn to Create and Access R Matrix
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, list and matrix. We will use the list that we created in the previous section ‘data_list’.
In order to give names to the elements of the list:
names(data_list) <- c("Monat", "Matrix", "Misc")
Access the first element of the list.
print(data_list) #Accessing the First element
Access the third element. As it is also a list, all its elements will print.
print(data_list) #Accessing the Third element
By using the name of the element access the list elements.
print(data_list$Matrix) #Using name of access element
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))
Give names to the elements in the list.
names(data_list) <- c("Monat", "Matrix", "Misc")
Add an element at the end of the list.
data_list <- "New element" print(data_list)
Remove the last element.
data_list <- NULL print(data_list)
Update the 3rd Element.
data_list <- "updated element"
Don’t forget to explore the R Data Frame tutorial
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.
num_list <- list(1,2,3,4,5) #Author DataFlair day_list <- list("Mon","Tue","Wed", "Thurs", "Fri") merge_list <- c(num_list, day_list) merge_list
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 be applied 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.
int_list <- list(1:5) #Author DataFlair print(int_list) int_list2 <- list(10:14) print(int_list2)
Convert the lists to vectors.
vec1 <- unlist(int_list) vec2 <- unlist(int_list2) print(vec1) print(vec2)
Now add the vectors.
sum <- vec1 + vec2 print(sum)
Must Learn – R Vector Functions Tutorial
How to Generate Lists in R
We can use a colon to generate a list of numbers.
> -5:5 #Generating a list of numbers from -5 to 5
Operating on Lists in R
R allows operating on all list values at once.
> #Author DataFlair > c(1,2,3) + 4
This and the Apply function allow you to avoid most for loops.
R Predefined Lists
Lists for letters and month names are predefined:
#Author DataFlair letters LETTERS month.abb month.name
c function in R
The c function in R combines the parameter into a list and converts them to the same type.
> c("April", 4) #Author DataFlair > typeof("4")
Here 4 is converts into a string.
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.
Now, it’s the turn of R Array Function Tutorial
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.