Operations on Lists in R Programming
Last Updated :
25 Aug, 2020
Lists in R language, are the objects which comprise elements of diverse types like numbers, strings, logical values, vectors, list within a list and also matrix and function as its element.
A list is generated using list() function. It is basically a generic vector that contains different objects. R allows its users to perform various operations on lists which can be used to illustrate the data in different forms.
Creating a List
Lists in R can be created by placing the sequence inside the list() function.
R
Geek_list <- list ( "Geek" , "RList”, c (65, 21, 80), TRUE , 27.02, 10.3)
print (Geek_list)
|
Output:
[[1]]
[1] "Geek"
[[2]]
[1] "RList"
[[3]]
[1] 65 21 80
[[4]]
[1] TRUE
[[5]]
[1] 27.02
[[6]]
[1] 10.3
Naming the elements of a list
Name can be assigned to the elements of the list and those names can be used to access the elements.
R
Geek_list <- list ( c ( "Geeks" , "For" , "Geeks" ),
matrix ( c (1:9), nrow = 3),
list ( "Geek" , 12.3))
names (Geek_list) <- c ( "This_is_a_vector" ,
"This_is_a_Matrix" ,
"This_is_a_listwithin_the_list" )
print (Geek_list)
|
Output:
$This_is_a_vector
[1] "Geeks" "For" "Geeks"
$This_is_a_Matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"
$This_is_a_listwithin_the_list[[2]]
[1] 12.3
Accessing elements of a List
In order to access the list elements, use the index number, and in case of named list, elements can be accessed using its name also.
R
Geek_list <- list ( c ( "Geeks" , "For" , "Geeks" ),
matrix ( c (1:9), nrow = 3),
list ( "Geek" , 12.3))
names (Geek_list) <- c ( "This_is_a_vector" ,
"This_is_a_Matrix" ,
"This_is_a_listwithin_the_list" )
print (Geek_list[1])
print (Geek_list[3])
print (Geek_list$This_is_a_Matrix)
|
Output:
$This_is_a_vector
[1] "Geeks" "For" "Geeks"
$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"
$This_is_a_listwithin_the_list[[2]]
[1] 12.3
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Adding, Deleting, and Updating elements of a list
In R, a new element can be added to the list, the existing element can be deleted or updated.
R
Geek_list <- list ( c ( "Geeks" , "For" , "Geeks" ),
matrix ( c (1:9), nrow = 3),
list ( "Geek" , 12.3))
names (Geek_list) <- c ( "This_is_a_vector" ,
"This_is_a_Matrix" ,
"This_is_a_listwithin_the_list" )
Geek_list[4] <- "New element"
print (Geek_list)
Geek_list[4] <- NULL
print (Geek_list[4])
Geek_list[3] <- "updated element"
print (Geek_list[3])
|
Output:
$This_is_a_vector
[1] "Geeks" "For" "Geeks"
$This_is_a_Matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"
$This_is_a_listwithin_the_list[[2]]
[1] 12.3
[[4]]
[1] "New element"
$
NULL
$This_is_a_listwithin_the_list
[1] "updated element"
Merging elements of a List
Many lists can be merged in one list by which all the list elements are placed inside one list.
R
list1 <- list (1, 2, 3, 4, 5, 6, 7)
list2 <- list ( "Geeks" , "For" , "Geeks" )
merged_list <- c (list1, list2)
print (merged_list)
|
Output:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
[[5]]
[1] 5
[[6]]
[1] 6
[[7]]
[1] 7
[[8]]
[1] "Geeks"
[[9]]
[1] "For"
[[10]]
[1] "Geeks"
Converting a list to vector
In order to perform arithmetic operations, lists should be converted to vectors using unlist() function.
R
list1 <- list (1:5)
print (list1)
list2 <- list (11:15)
print (list2)
v1 <- unlist (list1)
v2 <- unlist (list2)
print (v1)
print (v2)
result_vector <- v1+v2
print (result_vector)
|
Output:
[[1]]
[1] 1 2 3 4 5
[[1]]
[1] 11 12 13 14 15
[1] 1 2 3 4 5
[1] 11 12 13 14 15
[1] 12 14 16 18 20
Similar Reads
Array Operations in R Programming
Arrays are the R data objects which store the data in more than two dimensions. Arrays are n-dimensional data structures. For example, if we create an array of dimensions (2, 3, 3) then it creates 3 rectangular matrices each with 2 rows and 3 columns. They are homogeneous data structures. Now, letâs
4 min read
Named List in R Programming
A list is an object in R Language which consists of heterogeneous elements. A list can even contain matrices, data frames, or functions as its elements. The list can be created using list() function in R. Named list is also created with the same function by specifying the names of the elements to ac
4 min read
Append Operation on Vectors in R Programming
In this article, let us discuss different methods to concatenate/append values to a vector in R Programming Language. Append method in RVectors in the R Programming Language is a basic objects consisting of sequences of homogeneous elements. vector can be integer, logical, double, character, complex
3 min read
Looping over Objects in R Programming
One of the biggest issues with the âforâ loop is its memory consumption and its slowness in executing a repetitive task. When it comes to dealing with a large data set and iterating over it, a for loop is not advised. In this article we will discuss How to loop over a list in R Programming Language
4 min read
Operations on Matrices in R
Matrices in R are a bunch of values, either real or complex numbers, arranged in a group of fixed number of rows and columns. Matrices are used to depict the data in a structured and well-organized format. It is necessary to enclose the elements of a matrix in parentheses or brackets. A matrix with
8 min read
File Handling in R Programming
In R Programming, handling of files such as reading and writing files can be done by using in-built functions present in R base package. In this article, let us discuss reading and writing of CSV files, creating a file, renaming a file, check the existence of the file, listing all files in the worki
4 min read
Parallel Programming In R
Parallel programming is a type of programming that involves dividing a large computational task into smaller, more manageable tasks that can be executed simultaneously. This approach can significantly speed up the execution time of complex computations and is particularly useful for data-intensive a
6 min read
Operations on Vectors in R
Vectors are the most basic data types in R. Even a single object created is also stored in the form of a vector. Vectors are nothing but arrays as defined in other languages. Vectors contain a sequence of homogeneous types of data. If mixed values are given then it auto converts the data according t
3 min read
Function Arguments in R Programming
Arguments are the parameters provided to a function to perform operations in a programming language. In R programming, we can use as many arguments as we want and are separated by a comma. There is no limit on the number of arguments in a function in R. In this article, we'll discuss different ways
4 min read
Two Dimensional List in R Programming
A list in R is basically an R object that contains within it, elements belonging to different data types, which may be numbers strings or even other lists. Basically, a list can contain other objects which may be of varying lengths. The list is defined using the list() function in R. A two-dimension
5 min read