A list in R programming is a generic object consisting of an ordered collection of objects. Lists are one-dimensional, heterogeneous data structures. The list can be a list of vectors, a list of matrices, a list of characters, a list of functions, and so on. A list in R is created with the use of the list() function.
R allows accessing elements of an R list with the use of the index value. In R, the indexing of a list starts with 1 instead of 0.
1. Creating a List
To create a List in R you need to use the function called "list()". We want to build a list of employees with the details. So for this, we want attributes such as ID, employee name, and the number of employees.
Example:
R
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(empId, empName, numberOfEmp)
print(empList)
Output[[1]]
[1] 1 2 3 4
[[2]]
[1] "Debi" "Sandeep" "Subham" "Shiba"
[[3]]
[1] 4
2. Naming List Components
Naming list components make it easier to access them.
Example:
R
my_named_list <- list(name = "Sudheer", age = 25, city = "Delhi")
print(my_named_list)
Output$name
[1] "Sudheer"
$age
[1] 25
$city
[1] "Delhi"
3. Accessing R List Components
We can access components of an R list in two ways.
3.1. Access components by names:
All the components of a list can be named and we can use those names to access the components of the R list using the dollar command.
Example:
R
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)
cat("Accessing name components using $ command\n")
print(empList$Names)
Output$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
Accessing name components using $ command
[1] "Debi" "Sandeep" "Subham" "Shiba"
3.2. Access components by indices:
We can also access the components of the R list using indices. To access the top-level components of a R list we have to use a double slicing operator "[[ ]]" which is two square brackets and if we want to access the lower or inner-level components of a R list we have to use another square bracket "[ ]" along with the double slicing operator "[[ ]]".
Example:
R
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)
cat("Accessing name components using indices\n")
print(empList[[2]])
cat("Accessing Sandeep from name using indices\n")
print(empList[[2]][2])
cat("Accessing 4 from ID using indices\n")
print(empList[[1]][4])
Output$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
Accessing name components using indices
[1] "Debi" "Sandeep" "Subham" "Shiba"
Accessing Sandeep from na...
4. Modifying Components of a List
A R list can also be modified by accessing the components and replacing them with the ones which you want.
Example:
R
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before modifying the list\n")
print(empList)
empList$`Total Staff` = 5
empList[[1]][5] = 5
empList[[2]][5] = "Kamala"
cat("After modified the list\n")
print(empList)
OutputBefore modifying the list
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
After modified the list
$ID
[1] 1 2 3 4 5
$Names
[1] "Debi" "Sandeep" "Subham" ...
5. Concatenation of lists
Two R lists can be concatenated using the concatenation function. So, when we want to concatenate two lists we have to use the concatenation operator.
Syntax
list = c(list, list1)
list = the original list
list1 = the new list
Example:
R
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before concatenation of the new list\n")
print(empList)
empAge = c(34, 23, 18, 45)
empList = c(empName, empAge)
cat("After concatenation of the new list\n")
print(empList)
OutputBefore concatenation of the new list
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
After concatenation of the new list
[1] "Debi" "Sandeep" "Subham" "S...
6. Adding Item to List
To add an item to the end of list, we can use append() function.
R
my_numbers = c(1,5,6,3)
append(my_numbers, 45)
my_numbers
Output[1] 1 5 6 3 45
[1] 1 5 6 3
7. Deleting Components of a List
To delete components of a R list, first of all, we need to access those components and then insert a negative sign before those components. It indicates that we had to delete that component.
Example:
R
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before deletion the list is\n")
print(empList)
cat("After Deleting Total staff components\n")
print(empList[-3])
cat("After Deleting sandeep from name\n")
print(empList[[2]][-2])
OutputBefore deletion the list is
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
After Deleting Total staff components
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sand...
8. Merging list
We can merge the R list by placing all the lists into a single list.
R
lst1 <- list(1,2,3)
lst2 <- list("Sun","Mon","Tue")
new_list <- c(lst1, lst2)
print(new_list)
Output:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Sun"
[[5]]
[1] "Mon"
[[6]]
[1] "Tue"
9. Converting List to Vector
Here we are going to convert the R list to vector, for this we will create a list first and then unlist the list into the vector.
R
lst <- list(1:5)
print(lst)
vec <- unlist(lst)
print(vec)
Output[[1]]
[1] 1 2 3 4 5
[1] 1 2 3 4 5
10. R List to matrix
We will create matrices using matrix() function in R programming. Another function that will be used is unlist() function to convert the lists into a vector.
R
lst1 <- list(list(1, 2, 3),
list(4, 5, 6))
cat("The list is:\n")
print(lst1)
cat("Class:", class(lst1), "\n")
mat <- matrix(unlist(lst1), nrow = 2, byrow = TRUE)
cat("\nAfter conversion to matrix:\n")
print(mat)
cat("Class:", class(mat), "\n")
OutputThe list is:
[[1]]
[[1]][[1]]
[1] 1
[[1]][[2]]
[1] 2
[[1]][[3]]
[1] 3
[[2]]
[[2]][[1]]
[1] 4
[[2]][[2]]
[1] 5
[[2]][[3]]
[1] 6
Class: list
After conversion to matrix:
[,1] [,2] [,3]
[1,...
In this article we have covered Lists in R, we have covered list operations like creating, naming, merging and deleting a list in R language.
Also Check: