Search code examples
rmatrix-multiplication

Correct way to perform matrix multiplication


I have the simple matrix and would like to multiply D on E but I'm getting an error:

D <- data.frame(X = c(1,1,-1,1), Y = c(1,-1,1,1), Z = c(1,1,1,-1))
E <- data.frame(X = c(-1,0,1), Y = c(-1,1,0), Z = c(1,1,1))
P <- D %*% E

Error in D %*% E : requires numeric/complex matrix/vector arguments

How do I overcome the error? It's simple 3x4 and 3x3 matrix multiplication. Thank you for your support!


Solution

  • You have to transform the data frames into matrices:

    D <- as.matrix(data.frame(X = c(1,1,-1,1), Y = c(1,-1,1,1), Z = c(1,1,1,-1)))
    E <- as.matrix(data.frame(X = c(-1,0,1), Y = c(-1,1,0), Z = c(1,1,1)))
    P <- D %*% E