In [3]:
5


Out[3]:
5

In [5]:
y <- matrix(c(1,2,3,4,3,4),2,3)

In [6]:
y


Out[6]:
133
244

In [14]:
x <- c(1:6)

In [13]:
x


Out[13]:
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6

In [16]:
f <- function(x)
{
  x^3 + x^2 + x + 10
}

In [17]:
f(x)


Out[17]:
  1. 13
  2. 24
  3. 49
  4. 94
  5. 165
  6. 268

In [54]:
u <- c(4,3,2,1)
u


Out[54]:
  1. 4
  2. 3
  3. 2
  4. 1

In [55]:
v <- c(1,2,3,4)
v


Out[55]:
  1. 1
  2. 2
  3. 3
  4. 4

In [52]:
u*v


Out[52]:
  1. 4
  2. 6
  3. 6
  4. 4

In [53]:
u%*%v


Out[53]:
20

In [38]:
?"%*%"


Out[38]:
matmult {base}R Documentation

Matrix Multiplication

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.

Details

When a vector is promoted to a matrix, its names are not promoted to row or column names, unlike as.matrix.

This operator is S4 generic but not S3 generic. S4 methods need to be written for a function of two arguments named x and y.

Value

A double or complex matrix product. Use drop to remove dimensions which have only one level.

Note

Since R 3.2.0, promotion of a vector to a 1-row or 1-column matrix happens in even more cases, when one of the two choices allows x and y to get conformable dimensions.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

For matrix crossproducts, crossprod() and tcrossprod() are typically preferable. matrix, Arithmetic, diag.

Examples

x <- 1:4
(z <- x %*% x)    # scalar ("inner") product (1 x 1 matrix)
drop(z)             # as scalar

y <- diag(x)
z <- matrix(1:12, ncol = 3, nrow = 4)
y %*% z
y %*% x
x %*% z

[Package base version 3.2.4 ]

In [57]:
sc <- sparkR.init()
sqlContext <- sparkRSQL.init(sc)


Error in eval(expr, envir, enclos): could not find function "sparkR.init"
Error in eval(expr, envir, enclos): could not find function "sparkRSQL.init"

In [58]:
library(SparkR, lib.loc = c(file.path(Sys.getenv("SPARK_HOME"), "R", "lib")))


Attaching package: 'SparkR'

The following objects are masked from 'package:stats':

    cov, filter, lag, na.omit, predict, sd, var

The following objects are masked from 'package:base':

    colnames, colnames<-, intersect, rank, rbind, sample, subset,
    summary, table, transform


In [ ]: