Vectors and vector arithmetic
Vectors and Vector Arithmetic Vectors are ordered collections of numbers that are used in R programming for data analysis and machine learning. These ordered...
Vectors and Vector Arithmetic Vectors are ordered collections of numbers that are used in R programming for data analysis and machine learning. These ordered...
Vectors are ordered collections of numbers that are used in R programming for data analysis and machine learning. These ordered collections can be viewed as a one-dimensional array of numbers, or as a list of elements that are related by their positions.
Key Characteristics of Vectors:
They are ordered, meaning the elements are in a specific order.
They are finite, meaning they have a specific number of elements.
They are homogeneous, meaning the elements have the same data type.
Basic Operations on Vectors:
Addition: Adding two vectors adds the corresponding elements of each vector together.
Subtraction: Subtracting two vectors removes the corresponding elements of each vector from each other.
Multiplication: Multiplying two vectors results in a new vector with elements that are the results of multiplying the corresponding elements of the original vectors.
Dot product: The dot product of two vectors gives the scalar value that represents the weighted sum of the corresponding elements of the vectors.
Element extraction: Subtracting a scalar from a vector creates a new vector containing only the elements that are greater than the scalar.
Concatenation: Combining two vectors into a new vector by adding them element by element.
Example:
r
x <- c(1, 3, 5, 7, 9)
y <- c(2, 4, 6)
z <- x + y
w <- x %*% y
print(x)
print(y)
print(z)
print(w)
Output:
[1] 1 3 5 7 9
[1] 2 4 6
[1] 20 15 45
[1] 18
This shows that the vectors x, y and z are equal to each other, as they are all ordered collections of numbers that add up to the same total