The R %*% operator The R %*% operator r r

The R %*% operator


It's a matrix multiplication operator!

From the documentation:

Description:

Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix).

Usage:

x %*% y

Arguments:

x, y    numeric or complex matrices or vectors


> c(1,2,3) %*% c(4,5,6)     [,1][1,]   32> c(1,2,3) * c(4,5,6)[1]  4 10 18

Like MadSeb said, it is the matrix multiplication operator. If you give it two vectors, it will coerce them to (logical) 1-row & a 1-col matrix and multiply them.

It is also the inner (or dot) product between two vectors and finds wide usage in linear algebra, computational geometry and a host of other applications.

http://en.wikipedia.org/wiki/Dot_product

BTW, the vectors have to be in the same space (same number of dimensions)

> c(1,2,3) %*% c(4,5,6,7)Error in c(1, 2, 3) %*% c(4, 5, 6, 7) : non-conformable arguments


I created a question 'What is the calculation behind the %*% operator in R?' which was marked as a duplicate of this question. The %*% operator is used to multiply two matrices. I didn't realise 'matrix multiplication' was an established algebraic method so it was useful to learn the underlying calculation, not yet described explicitly in other answers here. Passing on useful links from comments in the duplicate question

https://en.m.wikipedia.org/wiki/Matrix_multiplication#Definition

http://matrixmultiplication.xyz/

From Matrix Multiplication Wikipedia Page