*84*

Given vector *a* = [a_{1}, a_{2}, a_{3}] and vector *b* = [b_{1}, b_{2}, b_{3}], theÂ **dot product**Â of vector a and vector b, denoted asÂ **a Â· b**, is given by:

**a Â· b**Â = a_{1}Â * b_{1}Â + a_{2}Â * b_{2}Â + a_{3}Â * b_{3}

For example, if *a* = [2, 5, 6] and *b* = [4, 3, 2], then the dot product of *a* and *b* would be equal to:

**a Â· b =Â **2*4 + 5*3 + 6*2

**a Â· b =Â **8 + 15 + 12

**a Â· b =Â **35

In essence, theÂ dot productÂ is the sum of the products of the corresponding entries in two vectors.

**How to Calculate the Dot Product in R**

There are two ways to quickly calculate the dot product of two vectors in R:

**Method 1: Use %*%**

The following code shows how to use theÂ **%*%Â **function to calculate the dot product between two vectors in R:

#define vectors a #calculate dot product between vectors a %*% b [,1] [1,] 35

The dot product turns out to beÂ **35**.

Note that this function works for data frame columns as well:

#define data df #calculate dot product between columns 'a' and 'b' of data frame df$a %*% df$b [,1] [1,] 35

**Method 2: Use the dot() function**

We can also calculate the dot product between two vectors by using theÂ **dot()Â **function from theÂ **pracmaÂ **library:

library(pracma) #define vectors a #calculate dot product between vectors dot(a, b) [1] 35

Once again, the dot product between the two vectors turns out to beÂ **35**.

**Related:** How to Calculate a Cross Product in R

**Additional Resources**

The following tutorials explain how to calculate a dot product using other statistical software:

How to Calculate the Dot Product in Excel

How to Calculate the Dot Product in Google Sheets

How to Calculate the Dot Product on a TI-84 Calculator