Press "Enter" to skip to content

What is a list in R?

Zigya Acadmey 0

One of the most important data structures in R is List. A list is a great tool to store many kinds of objects in the order expected. We can include matrices, vectors data frames, or lists. We can imagine a list as a bag in which we want to put many different items. When we need to use an item, we open the bag and use it. A list is similar; we can store a collection of objects and use them when we need them.

Creating a List

To create a list in R we need to use list() function

> list1 <- list(comp1, comp2, comp3, ...)

The arguments to the list function are the list components. Remember, these components can be matrices, vectors, other lists, …

A single list can even contain vector, matrix, array, and also a list too.

# Let's create a vector
> vec1 <- c(1:5)

# Now let's create a matrix
> mat <- matrix(1:10, ncol = 5)

# Now let's create logical vector
> vec2 <- c(TRUE, FALSE, FALSE, TRUE, TRUE)

# Now let's create a list consisting of vector and matrix
> list2 <- list(vec1, vec2, mat)
> list2
[[1]]
[1] 1 2 3 4 5

[[2]]
[1]  TRUE FALSE FALSE  TRUE  TRUE

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

Creating a named list

We can also name the list components to avoid not knowing or remembering what the components of your list stand for.

> my_list <- list(name1 = comp1,
                name2 = comp2)
# or 

> my_list <- list(comp1, comp2)
> names(my_list) <- c("name1", "name2")

# Let's name our list which we just created
> names(list2) <- c("Num_Vector", "Logical_Vector", "Matrix")
> list2

$Num_Vector
[1] 1 2 3 4 5

$Logical_Vector
[1]  TRUE FALSE FALSE  TRUE  TRUE

$Matrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

Selecting element from list

After we built our list, we can access it quite easily. We need to use the [[index]] to select an element in a list. The value inside the double square bracket represents the position of the item in a list we want to extract. For instance, we pass 2 inside the parenthesis, R returns the second element listed.

# Access first element of list2
> list2[[1]]
[1] 1 2 3 4 5

# Access first element of the list2 with name
> list2$Num_Vector
[1] 1 2 3 4 5

# Access first element along with its name in the list2
> list2[1]
$Num_Vector
[1] 1 2 3 4 5

Manipulating elements in a List

We can add,delete, update elements in List

Update elements in a List

# Let's update the existing element at 1st index from list2
> list2[[1]] <- c(1:10)
> list2[[1]]
[1]  1  2  3  4  5  6  7  8  9 10

Delete elements in a List

# To delete element just assign NULL to it
> list2[[3]] <- NULL
> list2[[3]]
Error in list2[[3]] : subscript out of bounds

Add elements in a List

# Let's add new vector to 3rd index of the list
> list2[[3]]
Error in list2[[3]] : subscript out of bounds

> list2[[3]] <- seq(100,110, 2)
> list2
$Num_Vector
 [1]  1  2  3  4  5  6  7  8  9 10

$Logical_Vector
[1]  TRUE FALSE FALSE  TRUE  TRUE

[[3]]
[1] 100 102 104 106 108 110

How to Convert R List to Vector

A list can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be applied after the list is converted into a vector. To do this conversion, we can use the unlist() function. It takes the list as input and produces a vector.

# Create a list
> list_1 <- list(1:5)
> print(list_1)
[[1]]
[1] 1 2 3 4 5

> vec_1 <- unlist(list_1)
print(vec_1)
[1] 1 2 3 4 5

Conclusion

We have studied R List in this article. We all are aware, that lists are the object which contains elements of different types like strings, numbers, and vectors. And we also Studied how to perform operations on the list.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *