List of Vectors in R
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. Syntax:
list(arg1, arg2, ..)
Example 1:
Python3
# R program to create a list of Vectors # Creating Vectors vec1 < - c( 1 , 2 , 3 ) vec2 < - c(TRUE, FALSE) # Creating a list of Vectors listt = list (vec1, vec2) # Printing List print (listt) |
Output:
[[1]] [1] 1 2 3 [[2]] [1] 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. Example 2:
Python3
# R program to create a list of Vectors # Creating Vectors vec1 < - c( 1 , 2 , 3 ) vec2 < - c(TRUE, FALSE) # Creating list of Vectors listt = list (vec1, vec2) # Printing List print (listt[[ 2 ]]) print (listt[[ 2 ]][ 2 ]) |
Output:
[1] TRUE FALSE [1] 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. Example 1:
Python3
# R program to create a list of Vectors # Creating Vectors vec1 < - c( 1 , 2 , 3 ) vec2 < - c(TRUE, FALSE) # Creating list of Vectors lst = list (vec1, vec2) # Creating a new Vector vec3 < - c( 1 + 3i ) # Adding Vector to list lst[[ 3 ]]< - vec3 # Printing List print (lst) |
Output:
[[1]] [1] 1 2 3 [[2]] [1] TRUE FALSE [[3]] [1] 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. Example 2:
Python3
# R program to create a list of Vectors # Creating Vectors vec1 < - c( 1 , 2 , 3 ) vec2 < - c(TRUE, FALSE) # Creating list of Vectors lst = list (vec1, vec2) # determine the length of list len < - length(lst) # Creating new Vector vec3 < - c( 0.5 , 2 + 2i ) # Using for loop to add elements for ( i in 1 : 2 ) { # Adding vector to list lst[[ len + i]]< - vec3 } print (lst) |
Output:
[[1]] [1] 1 2 3 [[2]] [1] TRUE FALSE [[3]] [1] 0.5+0i 2.0+2i [[4]] [1] 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. Example:
Python3
# R program to create a list of Vectors # Creating Vectors vec1 < - c( 1 , 2 , 3 ) vec2 < - c(TRUE, FALSE) # Creating list of Vectors lst = list (vec1, vec2) # Creating new Vector vec3 < - c( 1 + 3i ) # Adding Vector to list lst[[ 3 ]]< - vec3 print ( "Original List" ) print (lst) # Removing Vector from list lst[[ 2 ]]< - NULL print ( "Modified List" ) print (lst) |
Output:
[1] "Original List" [[1]] [1] 1 2 3 [[2]] [1] TRUE FALSE [[3]] [1] 1+3i [1] "Modified List" [[1]] [1] 1 2 3 [[2]] [1] 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. Example:
Python3
# R program to create a list of Vectors # Creating Vectors vec1 < - c( 1 , 2 , 3 ) vec2 < - c(TRUE, FALSE) # Creating list of Vectors lst = list (vec1, vec2) print ( "original list" ) print (lst) # Modifying List element lst[[ 2 ]]< - c( "TEACH" , "CODING" ) print ( "Modified List" ) print (lst) |
Output:
[1] "original list" [[1]] [1] 1 2 3 [[2]] [1] TRUE FALSE [1] "Modified List" [[1]] [1] 1 2 3 [[2]] [1] "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. Example:
Python3
# R program to merge two lists of Vectors # Creating 1st list list_data1 < - list (c( 1 : 3 ), c(TRUE, FALSE)) # Creating 2nd list list_data2 < - list (c( 0.1 , 3.4 )) print ( "First List" ) print (list_data1) print ( "Second List" ) print (list_data2) print ( "Merged List" ) # Merging Lists merged_list < - c(list_data1, list_data2) print (merged_list) |
Output:
[1] "First List" [[1]] [1] 1 2 3 [[2]] [1] TRUE FALSE [1] "Second List" [[1]] [1] 0.1 3.4 [1] "Merged List" [[1]] [1] 1 2 3 [[2]] [1] TRUE FALSE [[3]] [1] 0.1 3.4
Example 2:
Python3
# R program to Merge two lists # Creating 1st list list_data1 < - list (c( 1 : 3 ), c(TRUE, FALSE)) # Creating 2nd List list_data2 < - list (c( "Hi" )) print ( "First List" ) print (list_data1) print ( "Second List" ) print (list_data2) print ( "Merged List" ) # Merging lists using append function merged_list < - append(list_data1, list_data2) print (merged_list) |
Output:
[1] "First List" [[1]] [1] 1 2 3 [[2]] [1] TRUE FALSE [1] "Second List" [[1]] [1] "Hi" [1] "Merged List" [[1]] [1] 1 2 3 [[2]] [1] TRUE FALSE [[3]] [1] "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.
Please Login to comment...