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
# Creating a list.
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
# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"),
matrix(c(1:9), nrow = 3),
list("Geek", 12.3))
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector",
"This_is_a_Matrix",
"This_is_a_listwithin_the_list")
# Printing 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
# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"),
matrix(c(1:9), nrow = 3),
list("Geek", 12.3))
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector",
"This_is_a_Matrix",
"This_is_a_listwithin_the_list")
# To access the first element of the list.
print(Geek_list[1])
# To access the third element.
print(Geek_list[3])
# To access the list element using the name of the element.
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
# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"),
matrix(c(1:9), nrow = 3),
list("Geek", 12.3))
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector",
"This_is_a_Matrix",
"This_is_a_listwithin_the_list")
# To add a new element.
Geek_list[4] <- "New element"
print(Geek_list)
# To remove the last element.
Geek_list[4] <- NULL
# To print the 4th Element.
print(Geek_list[4])
# To update the 3rd Element.
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
# Firstly, create two lists.
list1 <- list(1, 2, 3, 4, 5, 6, 7)
list2 <- list("Geeks", "For", "Geeks")
# Then to merge these two lists.
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
# Firstly, create lists.
list1 <- list(1:5)
print(list1)
list2 <-list(11:15)
print(list2)
# Now, convert the lists to vectors.
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
# Now add the vectors
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
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, comple
2 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
5 min read
Operations on Matrices in R In R, matrices are two-dimensional arrays of data numeric, logical or complex organized in 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 is defined by its order
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
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