Introduction to R List
R list is a type of data structure that supports multiple data types. R language has a built-in function called list(), which is used to create the list variable and assign various types of values to it. Data values in the list are called components, and the components can be accessed using list indexing in R languages. R list is a generic data structure that supports both the primitive data structures like integer, logical, double, character, and other data structures like vectors as its components. The list data structure is instrumental in R programming due to the adaptability of multiple data types.
What is an 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. The creation of a list has been explained below:
For example, we want to create a “my_list “list containing 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.
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 the list () function. Next, 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, i.e., how many elements are present in it, by using the length () function.
Suppose 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 the 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 the 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 the 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 an essential component of R, making it easier to store and manipulate different types of elements. With the list’s help, 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 –