Vectors are a sequence of elements belonging to the same data type. A list in R, however, comprises of elements, vectors, variables or lists which may belong to different data types. In this article, we will study how to create a list consisting of vectors as elements and how to access, append and delete these vectors to lists.
list() function in R creates a list of the specified arguments. The vectors specified as arguments in this function may have different lengths.
list(arg1, arg2, ..)
[]  1 2 3 []  TRUE FALSE
Here, in the above code, vec1 is an integer vector of length 3. vec2 is a boolean vector of length 2.
[[indx]] specifies the complete vector at the corresponding index value of the complete list.
 TRUE FALSE  FALSE
The entire vector at a position can be accessed using the corresponding position value enclosed in [[ ]] or . If we further, wish to access the elements of a particular vector, it can be specified by using the required position enclosed in .The first print statement print the entire second vector contained in the list, that is vec2. And, the second print statement prints the second element of the second vector, which is FALSE.
Adding elements to a list
Additional vectors can be added by specifying the position in the list where we wish to append the new vector. The new elements are concatenated at the end of the list. Multiple elements can also be added to a list with the use of a ‘for’ or a ‘while’ loop. These elements may be vectors, matrices, numerical or variables. The changes are made to the original list. The elements are added in O(n) time complexity where n is the length of the list.
[]  1 2 3 []  TRUE FALSE []  1+3i
In the above code, vec3 is a vector consisting of a complex number. It is added at the third position in the list.
[]  1 2 3 []  TRUE FALSE []  0.5+0i 2.0+2i []  0.5+0i 2.0+2i
Here, in the above code, a for loop is created which runs two times and adds the vector 2+2i to the list at the end.
Removing elements from a list
The vector to be deleted can be assigned to a NULL value using its corresponding position in the list. The changes are made to the original list. The vectors can be deleted at any position in the list, thereby, the size reduces by one and the elements are pushed back accordingly. The entire list can be deleted by successively forming a loop and deleting elements one by one.
 "Original List" []  1 2 3 []  TRUE FALSE []  1+3i  "Modified List" []  1 2 3 []  1+3i
Here, the second vector is deleted from the original list.
Modifying elements in a list
Elements can be modified in a similar fashion, by assigning a new vector to the desired position. Elements at any index can be changed to other vectors, functions or even matrices. Modification of an element requires O(1) time complexity.
 "original list" []  1 2 3 []  TRUE FALSE  "Modified List" []  1 2 3 []  "TEACH" "CODING"
Merging two lists
The lists comprising of vectors can be merged together to form a larger list. The lists are merged in the order in which they appear in the function as arguments. The total size of the merged list is the sum of sizes of individual lists. There are two ways to merge lists into one:
c() function or the append() function both of which take arguments as the lists to combine. The time complexity required to merge two lists is O(m) where m is the size of the list appearing first in the function.
 "First List" []  1 2 3 []  TRUE FALSE  "Second List" []  0.1 3.4  "Merged List" []  1 2 3 []  TRUE FALSE []  0.1 3.4
 "First List" []  1 2 3 []  TRUE FALSE  "Second List" []  "Hi"  "Merged List" []  1 2 3 []  TRUE FALSE []  "Hi"
List1 contains one integer vector and another boolean vector. List2 contains a single vector comprising of real numbers. Merged List is of size three as a summation of all these three vectors.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.