R Lists
A list in R 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 and a list of functions, and so on.
A list is a vector but with heterogeneous data elements. A list in R is created with the use of list() function. R allows accessing elements of a list with the use of the index value. In R, the indexing of a list starts with 1 instead of 0 like other programming languages.
Creating a List
Example:
L <- list("apple", "banana", "cherry")
# Print the list
print(L)
Output:
[[1]]
[1] "apple"
[[2]]
[1] "banana"
[[3]]
[1] "cherry"
Access Lists
You can access the list items by referring to its index number, inside brackets. The first item has index 1, the second item has index 2, and so on:
L <- list("apple", "banana", "cherry")
# Print the second element of the list L
print(L[2])
Output:
[1] "banana"
Change Item Value
To change the value of a specific item, refer to the index number:
L <- list("apple", "banana", "cherry")
# change the second element of the list L
L[2]="orange"
print(L)
Output:
[[1]]
[1]"apple"
[[2]]
[1] "orange"
[[3]]
[1] "cherry"
To find out how many items a list has, use the length() function:
L <- list("apple", "banana", "cherry")
# Find the length of the list L
print(length(L))
Output:
[1] 3
To find out if a specified item is present in a list, use the %in% operator:
L <- list("apple", "banana", "cherry")
# check apple is present in list L
print("apple" %in% L)
print("orange" %in% L)
Output:
[1] TRUE
[1] FALSE
Add List Items
To add an item to the end of the list, use the append() function:
L <- list("apple", "banana", "cherry")
# add orange to the list L
append(L,"orange")
Output:
[[1]]
[1] "apple"
[[2]]
[1] "banana"
[[3]]
[1] "cherry"
[[4]]
[1] "orange"
L <- list("apple", "banana", "cherry")
# add orange to the list L
append(L,after=2,"orange")
Output:
[[1]]
[1] "apple"
[[2]]
[1] "banana"
[[3]]
[1] "orange"
[[4]]
[1] "cherry"
Remove List Items
You can also remove list items. The following example creates a new, updated list without an "apple" item:
L <- list("apple", "banana", "cherry")
# remove apple
NL=L[-1]
print(NL)
Output:
[[1]]
[1] "banana"
[[2]]
[1] "cherry"
You can specify a range of indexes by specifying where to start and where to end the range, by using the : operator
L <- list("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
L[2:5]
Output:
[[1]]
[1] "banana"
[[2]]
[1] "cherry"
[[3]]
[1] "orange"
[[4]]
[1] "kiwi"
Loop Through a List
You can loop through the list items by using a for loop:
Example
L <- list("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
for (i in L)
print(i)
Output:
[1] "apple"
[1] "banana"
[1] "cherry"
[1] "orange"
[1] "kiwi"
[1] "melon"
[1] "mango"
There are several ways to join, or concatenate, two or more lists in R.
The most common way is to use the c() function, which combines two elements together:
Example:
list1 <- list("a", "b", "c")
list2 <- list(1,2,3)
list3 <- c(list1,list2)
print(list3)
Output:
[[1]]
[1] "a"
[[2]]
[1] "b"
[[3]]
[1] "c"
[[4]]
[1] 1
[[5]]
[1] 2
[[6]]
[1] 3
Example:
# R program to create a List
# The first attributes is a numeric vector
# containing the employee IDs which is created
# using the command here
empId = c(1, 2, 3, 4)
# The second attribute is the employee name
# which is created using this line of code here
# which is the character vector
empName = c("Debi", "Sandeep", "Subham", "Shiba")
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
print(empList)
Example:
# R program to access
# components of a list
# Creating a list by naming all its components
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)
# Accessing a top level components by indices
cat("Accessing name components using indices\n")
print(empList[[2]])
# Accessing a inner level components by indices
cat("Accessing Sandeep from name using indices\n")
print(empList[[2]][2])
# Accessing another inner level components by indices
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 name using indices
[1] "Sandeep"
Accessing 4 from ID using indices
[1] 4
Converting List to Vector
Here we are going to convert the list to vector, for this we will create a list first and then unlist the list into the vector.
# Create lists.
lst <- list(1:5)
print(lst)
print(typeof(lst))
# Convert the lists to vectors.
vec <- unlist(lst)
print(typeof(vec))
print(vec)
Output:
[1] 1 2 3 4 5
[1] "list"
[1] "integer"
[1] 1 2 3 4 5
Comments
Post a Comment