R Lists

R List is a collection of R-objects which contains elements of different types like – strings, numbers, vectors and another list inside it. A list can also contain function or a matrix as its elements.

Creating a list

In R, to create a list we have to use list() function.

Example:

x <- list("a" = 2.4, "b" = TRUE, "c" = 1:3)

Output:

$a
[1] 2.4
$b
[1] TRUE
$c
[1] 1 2 3

Naming lists elements

We can give the name to the elements of the list. And we can also access the elements of the list using these names.

Example:

# Create a list containing a matrix, a vector and a list.
d <- list(c("Jan","Feb","Mar"), matrix(c(30,20,50,10,10,80), nrow = 2),list("green",12.3))
# Give names to the elements of the list.
names(d) <- c("Months", "Matrix", "A Inner list")
# Show the list.
print(d)

Output:

$Months
[1] "Jan" "Feb" "Mar"
$Matrix
[,1] [,2] [,3]
[1,]   30   50   10
[2,]   20   10   80
$`A Inner list`
$`A Inner list`[[1]]
[1] "green"
$`A Inner list`[[2]]
[1] 12.3

Accessing elements of the lists

We can access the elements of the list by the index of the element. Or if we are using named lists then it can also be accessed through the names.

Example:

# Accessing the first element of the list.
print(d[1])
# Accessing the thrid element. As it is also a list, all its elements will be printed.
print(d[3])
# Accessing the list element using the name of the element.
print(d$Matrix)

Output:

$Months
[1] "Jan" "Feb" "Mar"
$list
$list[[1]]
[1] "green"
$list[[2]]
[1] 12.3
        [,1] [,2] [,3]
[1,]   30   50   10
[2,]   20   10   80

Adding elements to a list

We can add elements at the end of the list.

Example:

# Create a list
x <- list("a" = 2.4, "b" = TRUE, "c" = 1:3)
# Add element at the end of the list.
x["d"] <- "New element"
x

Output:

$a
[1] 2.4
$b
[1] TRUE
$c
[1] 1 2 3
$d
[1] "New element"

Updating elements of the list

We can update any element of the list.

Example:

x <- list("a" = 2.4, "b" = TRUE, "c" = 1:3)
# Update the 2nd Element.
x[2] <- FALSE
x

Output:

$a
[1] 2.4
$b
[1] FALSE
$c
[1] 1 2 3

Deleting elements from a list

We can delete the elements of the list by assigning NULL to it.

Example:

x <- list("a" = 2.4, "b" = TRUE, "c" = 1:3)
# Update the 2nd Element.
x[2] <- NULL
x

Output:

$a
[1] 2.4
$c
[1] 1 2 3

Merging lists

We can also merge many lists into one list by placing all the lists inside one list() function.

Example:

# Create two lists.
l1 <- list(10,20,30)
l2 <- list("Nikita","Deep","Aman")
# Merge the two lists.
mergedList <- c(l1,l2)
# Print the merged list.
print(mergedList)

Output:

[[1]]
[1] 10
[[2]]
[1] 20
[[3]]
[1] 30
[[4]]
[1] "Nikita"
[[5]]
[1] "Deep"
[[6]]
[1] "Aman"

Converting lists to vectors

We can convert the list to a vector so that the elements of a vector can be used for further operations. All the arithmetic operation like addition, subtraction, multiplication, division on vectors can be applied after the list is converted into vectors. For converting the list into a vector, we have to use unlist() function. It takes a list as input and produces a vector.

Example:

# Create lists.
l1 <- list(1:5)
print(l1)
l2 <-list(11:15)
print(l2)
# Convert the lists to vectors.
v1 <- unlist(l1)
v2 <- unlist(l2)
print(v1)
print(v2)
# Now add the vectors
result <- v1+v2
print(result)

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
Reference: https://www.datamentor.io/r-programming/list/