Matrix expression causes error "requires numeric/complex matrix/vector arguments"?
Asked Answered
R

5

36
ma=diag(3)+t(da)%*%da

R Code above, error message as follows:

Error in t(da) %*% da : requires numeric/complex matrix/vector arguments

da is a matrix, looks as following:

V45       V46          V47          V48         V49         V50          V51    
1    0.461727059  2.357732985 -1.536932071 -1.34425710  0.893541975 -0.0676913075 -0.86532231
2    0.253022555  1.524473647 -0.588911138 -1.65207275 -0.072255170 -0.5212951533 -1.43686625
3    0.824678362  1.497001189  0.335973892 -0.84027799  0.275289411 -0.2921928001 -0.16277595
4    0.854530787  2.258305198  0.107346531 -1.69194014 -0.841572928 -1.1153931009 -1.939461341
5    1.148286984 -0.232390389 -0.498465734 -0.45728816  0.352889082  0.9868844505 -0.68401129

Could anyone help me to figure out the error?

Rothko answered 7/4, 2014 at 8:7 Comment(5)
Can you show a more complete code snippet? What I mean is, what are the dimensions of da? Does the position of the transpose matter? For example, have you tried breaking the problem down and just doing first da %*% t(da) or t(da) %*% da ??Wolfish
@NathanielPayne yes, I tested, both not work, same error. Also, I add data to my question. It is just simple matrix.Rothko
Can you give your example in a manner that we can copy/paste it? See #5963769 how a few hints.Undershrub
I second @RomanLuštrik here. If you can please provide the command to create the data or input it along with the data, to save time, I could easily play with it and get a response back.Wolfish
Is your matrix da 5x6 as you wrote, or what? Please provide reproducible code and data as people asked.Poltroon
B
59

To get the matrix multiplication to work, you need to convert the data.frame (presumably that's what da is) to a matrix. Calculating the transpose with t() automatically does this:

t(da)%*%as.matrix(da)

But this gives a 7x7 matrix which can't be added to the 3x3 identity matrix that you're using. Do you mean something like:

ma=diag(7)+t(da)%*%as.matrix(da)

You may like to have a look at An Introduction to R if you don't feel confident about the difference between a matrix and data.frame.

Bouldon answered 7/4, 2014 at 12:12 Comment(1)
Data type also matters so in many cases a simple as.matrix is insufficient. See Matrix multiplication in R: requires numeric/complex matrix/vector arguments for example.Paladin
P
1

Since a lot of machine learning libraries use matrix multiplication, if one or more of your inputs are of class character but the algorithm expects numeric, you could experience a similar error message.

TL;DR double-check that your inputs are of class numeric. If they're not, use as.numeric() to convert character to numeric. That solved for me using the qmap package's fitQmapRQUANT() function.

Proserpina answered 18/3, 2022 at 12:37 Comment(0)
S
0

Just in addition, you get this error if you attempting to pass a data frame where a matrix should be.

Shawntashawwal answered 2/9, 2020 at 19:56 Comment(0)
H
-2

See also this solution: https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16607

I couldn't figure out what was wrong with combat, so I tried to run PCA (function prcomp) on my dataset. R kept telling me that the input is not a matrix, even though when checking the class of the input object, it did say 'matrix' and mode 'numeric'. After restarting my IDE (Architect in my case), everything was running fine with combat and with PCA.

Hostler answered 9/9, 2016 at 15:22 Comment(0)
L
-2

Matrix expression causes error “requires numeric/complex matrix/vector arguments”?

This error occurs because you are passing a String in vector and As string cannot be multiply. matrix(c('1','2','2','1'), nrow=2,ncol=2,byrow=TRUE)->> J

To correct it pass a numeric/complex arguments it works. matrix(c(1,2,2,1), nrow=2,ncol=2,byrow=TRUE)->> j

then use this t = j %*% t(j) //it will print the multiplication of matrix transpose and matrix. print(t)

Lorilee answered 2/10, 2018 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.