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. Due to the adaptability of multiple data types the list data structure is very useful in R programming.
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.
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 $a
For 2nd: my_list[1:3] $a
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