Hello, readers! In this article, we will be focusing convert vector to list and list to vector in R programming, in detail.
So, let us begin!
To begin with, vectors are the simplest form of data structure available in R. They store simple elements of the same data type in a single dimension in R programming.
Unlike vectors, lists can hold elements of varied data types in the form of multi dimensional schema.
Let us now have a look at the functions that would help us achieve the inter conversion of vectors and lists in R.
1. Conversion of List to Vector in R using unlist() function
In order to convert a vector to a list, R provides us with unlist()
function that converts the list into a vector in R.
The unlist() function dissolves the list structure fed to it as arguments i.e. it converts the multi-dimensional schema into a a single dimensional schema.
Syntax:
unlist(list, use.names=True)
- use.names: If set to True(default), it preserves the names of the headers of the data values and represents the same in the output vector.
Example 01:
In this example, we have created a list with the headers ‘roll’ and ‘age’. After which, we have applied unlist() function to convert the list into a vector.
rm(list = ls())
lst <- list(roll = c(2,4,6,8,10),
Age = c(15,6,1,12,13))
print(unlist(lst))
Output:
As seen below, the headers of the list are also displayed in the vector. This is because, the default value of use.names = True. Thus, in order to neglect the headers, we need to change it to False as shown in the next example.
roll1 roll2 roll3 roll4 roll5 Age1 Age2 Age3 Age4 Age5
2 4 6 8 10 15 6 1 12 13
Example 02:
In this example, we have set use.names = False. By this, we specific to ignore the header values and choose only the data values for the conversion.
rm(list = ls())
lst <- list(roll = c(2,4,6,8,10),
Age = c(15,6,1,12,13))
print(unlist(lst,use.names = FALSE))
Output:
2 4 6 8 10 15 6 1 12 13
2. Conversion of Vector to List using as.list() function in R
To convert a vector to list, R provides us with as.list()
function that enables us to convert the single dimensional vector structure into a list format.
Have a look at the below syntax!
as.list(object)
Yes! The as.list() function can also be used to convert other data objects such as data frames, factors, matrices, etc to a list schema.
Example 01:
In the below example, we have created a single dimensional vector using c() function. Further, using as.list() function, we convert the vector to list form.
rm(list = ls())
vectr = c(10,20,30,40,50)
lst = as.list(vectr)
print(lst)
Output:
[[1]]
[1] 10
[[2]]
[1] 20
[[3]]
[1] 30
[[4]]
[1] 40
[[5]]
[1] 50
Example 02:
In this example, we have created a 4 by 4 matrix using matrix() function. Further, we have converted the matrix into a list using as.list() function successfully!
rm(list = ls())
data <- matrix(c(1,3,5,7,9,2,6,8), 4, 4)
print("Matrix:")
print(data)
lst = as.list(data)
print("List:")
print(lst)
Output:
[1] "Matrix:"
[,1] [,2] [,3] [,4]
[1,] 1 9 1 9
[2,] 3 2 3 2
[3,] 5 6 5 6
[4,] 7 8 7 8
[1] "List:"
[[1]]
[1] 1
[[2]]
[1] 3
[[3]]
[1] 5
[[4]]
[1] 7
[[5]]
[1] 9
[[6]]
[1] 2
[[7]]
[1] 6
[[8]]
[1] 8
[[9]]
[1] 1
[[10]]
[1] 3
[[11]]
[1] 5
[[12]]
[1] 7
[[13]]
[1] 9
[[14]]
[1] 2
[[15]]
[1] 6
[[16]]
[1] 8
Conclusion
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to R programming, stay tuned with us.
Till then, Happy learning!! 🙂