Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The R %*% operator

Tags:

r

What is this? I can't find help by using ?. (Sorry for being dumb)

> 1%*%1
     [,1]
[1,]    1
> 10%*%10
     [,1]
[1,]  100
> c(1:2)%*%c(1:2)
     [,1]
[1,]    5
like image 585
lokheart Avatar asked Feb 27 '14 06:02

lokheart


People also ask

What is the use of %*% in R?

It is called the multiplication operator. The %*% operator is a special kind of multiplication operator. It is used in the multiplication of matrices in R.

What are R operators?

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides following types of operators.

What is the && operator in R?

& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined.

What does != Mean in R?

The Inequality Operator != For example, the sentence "hello" != "goodbye" would read as: “hello” is not equal to “goodbye”. Because this statement is correct, R will output TRUE . The inequality operator can also be used for numerics, logicals, and other R objects.

What are the R operators?

R has several operators to perform tasks including arithmetic, logical and bitwise operations. In this article, you will learn about different R operators with the help of examples. R has many operators to carry out different mathematical and logical operations.

What does the%in% operator do in R?

The %in% operator in R allows you to determine whether or not an element belongs to a vector or data frame. This tutorial provides three examples of how to use this function in different scenarios.

What is arrive Maniac operator in R?

For example, some of the commonly used arrive maniac operators are addition operators represented by ‘+’ symbol for adding two vector values in R, ‘=’ or ‘<- ‘ is the assignment operator used to assign values the variable. The R operators help to develop standardized program logic. R Operators have many built-in operators.

What are arithmetic operators in C++?

Arithmetic operators are used with numeric values to perform common mathematical operations: Note: <<- is a global assigner. You will learn more about this in the Global Variable chapter. It is also possible to turn the direction of the assignment operator. Element-wise Logical AND operator.


Video Answer


2 Answers

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

like image 183
MadSeb Avatar answered Oct 23 '22 14:10

MadSeb


> 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
like image 9
jackStinger Avatar answered Oct 23 '22 14:10

jackStinger