Vector Math in R Programming Language

A vector is a primary data type in R  that contains elements of the same type. Vector has a property length through which you can count the number of elements in the vector. 

To check the vector’s data type, use typeof() function.

How to Create Vector in R

To create a Vector in R, use the c() function.

rv <- c(1, 2, 3)

In the above snippet, we have created a Vector using the c() method, which contains three elements and assigns it to the rv variable. Remember, any vector in R must-have elements of the same type.

Vector Arithmetics

Arithmetic operations of vectors can be performed on member-by-member(i.e., member-wise). For example, if you want to multiply each element by 5, you can use the following method.

# Pro.R

app.numbers <- c(1:10)
double.numbers <- app.numbers * 2
cat(double.numbers)

Output

➜ R Rscript Pro.R
2 4 6 8 10 12 14 16 18 20

In this example, we have defined a vector and then multiply the vector by 2. What R does behind the scenes because it will take each element of the vector and multiply it by 2 and return the vector.

To print the output, we have used the cat() function. The cat() function converts its arguments to character strings, concatenates them, separating them by the given sep= string, and then prints them.

Vector Math in R

Vector Math means we should be able to perform mathematical operations on vectors in R.

Let’s define two vectors and perform the addition operation.

# Pro.R

vcA <- c(11, 21, 19)
vcB <- c(6, 18, 46)

summation <- vcA + vcB
cat(summation)

Output

➜ R Rscript Pro.R
17 39 65

The output here is the vector, which is the sum of the corresponding elements of two vectors vcA and vcB.

Subtraction in R Vector

We can perform the subtraction the same way we have performed the vector addition.

vcA <- c(11, 21, 19)
vcB <- c(6, 18, 46)

subtract <- vcA - vcB
cat(subtract)

Output

➜ R Rscript Pro.R
5 3 -27

Division in R Vector

To perform division operation in R vector, use the following code.

# Pro.R

vcA <- c(10, 20, 30)
vcB <- c(2, 4, 6)

subtract <- vcA / vcB
cat(subtract)

Output

➜ R Rscript Pro.R
5 5 5

Recycling Rule

If two vectors are of unequal length, the shorter one will be recycled to match the longer vector.

For example, the following vectors vcA and vcB have different lengths, and their sum is computed by recycling values of the shorter vector vcA. Let’s understand with an example.

# Pro.R

vcA <- c(10, 20, 30)
vcB <- c(2, 4, 6, 8, 10, 12)

summation <- vcA + vcB
cat(summation)

Output

➜ R Rscript Pro.R
12 24 36 18 30 42

Here you can see that the first three values of the output are normal, but from the 4th value, it will take the  1st value of the vcA vector for addition and the 4th value of the vcB vector, and their sum is 18. (10 + 8).

For the fifth value of the output, it will take the 2nd value of the 1st vector and 5th value of the 2nd vector. The 2nd of vcA is 20, and the 5th of vcB is 10. So the addition is (10 + 20 = 30). And the same calculation for the last element of the output.

Please note that, If the first vector is smaller than the second vector, then the second vector’s length is multiple of the first vector. Otherwise, the imbalance will be created, and R will throw a warning. Let’s understand the following code.

vcA <- c(10, 20, 30)
vcB <- c(2, 4, 6, 8, 10, 12, 14)

summation <- vcA + vcB
cat(summation)

Output

➜ R Rscript Pro.R
Warning message:
In vcA + vcB :
longer object length is not a multiple of shorter object length
12 24 36 18 30 42 24

You can see that the R interpreter throws the following warning.

longer object length is not a multiple of shorter object length.

That means longer vector length is not a multiple of shorter vector length. In our case, the longer vector is vcB, whose length is 7, and the shorter vector length is vcA, whose vector length is 3.

So, the longer vector length should be one of the 3, 6, 9, 12, 15, …, and so on. It should be in multiples of 3.

That is it for Vector Math in R Programming Language.

Leave a Comment