Introduction to R List
In R List, we have various types of data structures and data types to work with like Vectors, Data Frames, Matrices, Factors, etc. In some R projects, we will have to combine various data structures in a single element. This is where List comes in the picture.
What is R List?
A-List is a one-dimensional data structure in which each element itself is a data structure. In other words, we can say that a list is a generic vector containing other objects. A-List can contain elements of the same type or a diverse type.
1. Creating A-List
A-List is created using the list () function. Creation of a list has been explained below:
For example, we want to create a list named “my_list” which contains three vectors n, s, b, and a numeric value 5.
> n = c (5, 6, 7)
> s = c ("aaa", "bbb", "ccc", "ddd", "eee")
> b = c ( FALSE, TRUE, FALSE, TRUE,FALSE)
This is the syntax to store the elements in a list.
4.5 (2,228 ratings)
List Creation Step
> my_list = list (n, s, b, 5)
Now, we have stored all these individual data structures in a list named “my_list” using list () function. The structure of the list can be examined using the str () function.
List of 4
$: num [1:3] 5 6 7
$: chr [1:5] “aaa” “bbb” “ccc” “ddd” …
$: logi [1:5] FALSE TRUE FALSE TRUE FALSE
$: num 5
Str() function output shows each element present in the list and their data type. We can find the length of the list, ie, how many elements are present in it by using length () function.
If we use: length (my_list). The output will be 4 in this case as it contains 4 elements. We can check if a given data structure is a list with the help of typeof() function.
2. Name A-List
Now we will see how we can name the components present in a list.
my_list = list (a=n, b=s, c=b, d=5)
Now, we have given a specific name to each component of the list. If we want only one component at a time, we can fetch it in Following way:
We can also fetch the list elements with syntax like
Here, It will give output as FALSE TRUE FALSE TRUE FALSE This shows that we can provide unique names to the list components So that it will be easier for us to fetch the list elements individually. We can also access the list elements with the help of indexing. For example, if we want to access the first element of the list It will be like
my_list or we want first 3 elements it will be my_list[1:3]Out will be as follows :
For 1st : my_list
For 2nd: my_list[1:3]
3. Manipulating A-List Element
We can add, delete or update the list elements. But please note that We can update any element but new elements can only be added at last.
# Adding an element.
my_list <- "New element"
# Removing the element
my_list <- NULL
# Updating the 4th Element.
my_list <- 6
We have seen that a list is a very important component of R, which makes it easier to store and manipulate different types of elements. With the help of the list, it becomes possible to store different kinds of data structures in one variable and use them as required.
This has been a guide to the R list. Here we discuss an introduction to R List. You can also go through our other suggested articles to learn more –