Converting list to vector
There is a drawback with the list, i.e., we cannot perform all the arithmetic operations on list elements. To remove this, drawback R provides unlist() function. This function converts the list into vectors. In some cases, it is required to convert a list into a vector so that we can use the elements of the vector for further manipulation.
The unlist() function takes the list as a parameter and change into a vector. Let see an example to understand how to unlist() function is used in R.
Example
# Creating lists. list1 <- list(10:20) print(list1) list2 <-list(5:14) print(list2) # Converting the lists to vectors. v1 <- unlist(list1) v2 <- unlist(list2) print(v1) print(v2) adding the vectors result <- v1+v2 print(result)
Output:
[[1]]
[1] 1 2 3 4 5[[1]]
[1] 10 11 12 13 14[1] 1 2 3 4 5
[1] 10 11 12 13 14
[1] 11 13 15 17 19