# Arithmetic Operation On Vector In R - Multiplication Of Vectors In R With Example

**Introduction **

R is an important programming language which is used by statisticians. It is also used in machine learning, data science, research, and many more new fields. In a previous article, we learned about vectors in R. Now, we shall learn about arithmetic operations, such as multiplication of two vectors in R. We shall see how it works in R studio.

To know more about vectors in R, you can visit the link Vectors in R.

To know more about vectors in R, you can visit the link Vectors in R.

**Vector Operation in R**

As we know vector in R is a data element so we can perform arithmetic operations on vectors, such as addition, subtraction, and multiplication. In a previous article Vector Operation In R, we have already seen how the addition of two vectors works in R. Now, we shall see how to multiply two vectors in R.

Let's start with the multiplication of two vectors in R using R Studio.

Let's start with the multiplication of two vectors in R using R Studio.

**Multiplication - Multiplication of Vectors in R**

We can multiply two or more vectors and can get the result as desired. Let's see how it works.- #Multiplication of Vector
- #Creating Vector
- firstVector <- 1:3
- firstVector
- secondVector <- 2:4
- secondVector
- #Multipling both Vector
- result <- firstVector * secondVector
- result

**Output**

[1] 2 6 12

Let's see the explanation of how it has multiplied one vector to another vector.

They are multiplied to each other by their position i.e., index. We can see that the element of the first vector which was at index 1 is multiplied by the element of the second vector which was at the index of 1. In the same way, the elements of the first vector at the index 2 and 3 were multiplied to the element of the second vector which was at the index 2 and 3 respectively.

Remember that index of the vector in R starts with 1, not 0.

This concept is simplified in the below image.

**Multiplication Rule - Commutative Property of Multiplication**

Vector multiplication in R follows commutative property of multiplication according to which when two numbers are multiplied with each other, then the result remains the same regardless of their order or sequence. We can say it by example 3 * 4 = 4 * 3 simply.

Let's see this in R by executing the above code by changing the sequence only.

- #Multiplication of Vector
- #Creating Vector
- firstVector <- 1:3
- firstVector
- secondVector <- 2:4
- secondVector
- #Multipling both Vector
- result <- secondVector * firstVector
- result

**Output**

2 6 12

We can see the result is the same as it was earlier. It has been shown in below image.

Now, we shall see different cases for multiplication of vectors in R.

**Case 1 - Multiplication of Vectors of Different Length**

When we try to multiply vectors of different length in R Studio, it gives a warning message as the longer object length is not a multiple of the shorter object length.

Let's see how it works.

First, we shall create two vectors, and after that we shall create another vector with the multiplication of these two vectors.

- #Multiplication of Vector
- #Creating Vector
- firstVector <- 1:3
- firstVector
- secondVector <- 2:5
- secondVector
- #Multipling both Vector
- result <- secondVector * firstVector
- result

**Output**

2 6 12 5

As we can see that even after warning, the result has been given after multiplication. For this, at the time of multiplication, the vector of shorter length has repeated its elements starting from index 1.

It has been shown in the below image how it has been multiplied.

**Case 2 - Multiplication of Vectors of Different DataTypes i.e., Integer and Decimal**

We can multiply vectors of data type integer and decimal. The result of multiplication will be of the data type decimal. It has been shown below.

- #Multiplication of Vector
- #Creating Vector
- firstVector <- 1:3
- firstVector
- secondVector <- rep(2.5, 3)
- secondVector
- #Multipling both Vector
- result <- secondVector * firstVector
- result

**Output**

[1] 2.5 5.0 7.5

The same has been shown in the below image in R studio.

**Case 3 - Multiplication of Vectors of Different DataTypes i.e., Integer or Decimal and Character**

We can't mutiply vector of character to other types of vectors like integer or decimal and vice versa. It gives the error as non-numeric argument to binary operator.

- #Multiplication of Vector
- #Creating Vector
- firstVector <- 1:3
- firstVector
- secondVector <- rep("Suraj", 3)
- secondVector
- #Multipling both Vector
- resultAfterMulti <- secondVector * firstVector
- resultAfterMulti

It has been show below.

**Summary****In this article, we have learned how to perform arithmetic operations on vectors, i.e., the multiplication of two vectors in R. We have seen different cases while multiplying two vectors in R also.**

I hope you have learned and enjoyed reading this article. You’re welcome to like, comment, share and leave any type of suggestion for my appreciation.