Page 229 - Data Science class 11
P. 229
6.8.5 transforming List to Vector
You can transform a list into a vector for performing further manipulations on the elements of the vector. Once a list
is converted to a vector, all the arithmetic operations are possible on vectors.
To convert a list into a vector, the unlist() function can be used. This function takes a list as input and generates a
vector as an output.
An example of converting lists into vectors and performing addition on them.
Example
Enter the following code snippet:
# Creating two lists.
list1 <- list(2:6)
print(list1)
list2 <-list(11:15)
print(list2)
# Converting the lists to vectors.
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
# Now add the vectors
result <- v1+v2
print(result)
The following is displayed:
6.9 matrix
Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. They contain
elements of the same atomic type. Though you can create a matrix containing only characters or only logical values,
they are not very useful. We use matrices containing numeric elements to be used in mathematical calculations.
Programming with R 227

