Matrix Multiplication Error in R: non-conformable arguments (2 Examples)

 

In this article, I’ll illustrate how to deal with the error message “non-conformable arguments” in R when performing matrix multiplication.

Table of contents:

Let’s do this…

 

Introduction of Example Data

Have a look at the exemplifying data below:

m1 <- matrix(2)                 # Create first data object
m1                              # Print first data object

 

table 1 matrix error non conformable arguments r

 

Table 1 illustrates our first data object: A matrix containing only one value.

m2 <- matrix(1:15, nrow = 5)    # Create second data object
m2                              # Print second data object

 

table 2 matrix error non conformable arguments r

 

Table 2 shows the second data object: A matrix with five rows and three columns.

 

Example 1: Reproduce the Error Message – non-conformable arguments

In this example, I’ll explain how to reproduce the error message “non-conformable arguments” in R.

Let’s assume that we want to multiply our two data objects. Then, we might try to execute the following R code:

m1 %*% m2                       # Trying to multiply data objects
# Error in m1 %*% m2 : non-conformable arguments

As you can see, the error message “non-conformable arguments” has been returned to the RStudio console.

The reason for this is that our first data object m1 is a 1×1 matrix.

Let’s see how we can solve this problem…

 

Example 2: Fix the Error Message – non-conformable arguments

This example explains how to handle the error message “non-conformable arguments”.

To do this, we have to convert our 1×1 matrix to a vector using the as.vector function:

as.vector(m1) * m2              # Converting m1 to vector

 

table 3 matrix error non conformable arguments r

 

Table 3 illustrates the result of the previous R syntax.

 

Video & Further Resources

In case you need more info on the R programming code of this tutorial, you might want to watch the following video of my YouTube channel. In the video, I’m explaining the topics of this tutorial in R:

 

The YouTube video will be added soon.

 

Furthermore, you might want to have a look at the other R tutorials on my website.

 

Summary: At this point you should have learned how to avoid the error “non-conformable arguments” in R programming. If you have additional questions, let me know in the comments section below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


12 Comments. Leave new

  • I attempted to apply lordif to examine gender differences in my measurement. My gender variable is in the first column followed by the 10 items in the measure. Total N = 1057, almost evenly split by gender. I used the following code:

    genDIF <- lordif(Neur_IRT, [ , 1, criterion = 'Chisq', alpha = 0.01

    I received message: unexpected '[' in genDIF <- lordif (Neur_IRT, ["

    Reply
    • Hey Robert,

      It seems like the specification of the arguments within the lordif function is incorrect. What do you want to specify with the [ ? Furthermore, it seems like a ) is missing at the end of this line of code.

      Regards,
      Joachim

      Reply
  • hello,my task is Nonnegative Matrix Factorization,i also meet this problem,but in my code,it is all assumption,so i not find specific matrix to solve,so this problem need how to solve?

    Reply
  • Dear Joachim,

    My Xp and betahatp are as follows(3*2 and 2*1), but I still cannot do Xp*betahatp, could you please help with this? Thank you in advance.
    Xp:
    [,1] [,2]
    [1,] 2 5
    [2,] 3 6
    [3,] 4 7
    betahatp
    [,1]
    [1,] 1.3333333
    [2,] -0.3333333
    Xp*betahatp
    Error in Xp * betahatp : non-conformable arrays

    Best regards,
    Yina

    Reply
    • Hey Yina,

      Are you looking for the following result?

      Xp <- matrix(2:7, ncol = 2)
       
      betahatp <- matrix(c(1.3333333, -0.3333333), ncol = 1)
       
      betahatp * Xp # Reproduce error
       
      as.vector(betahatp) * Xp # Fix error

      Regards,
      Joachim

      Reply
  • (my_omg) <- psych::omega(dsse_data,
    nfactors = 4,
    fm = "minres",
    key = c(1, 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, rep(1, 13)),
    poly = TRUE
    )

    hey, I'm receiving the error: Error in diag(key) %*% m : non-conformable arguments

    Reply
    • Hey Manisha,

      This is difficult to evaluate without seeing the data in dsse_data. Could you illustrate how these data looks like?

      Regards,
      Joachim

      Reply
  • hello, could you help me
    X <- model.matrix(status~WOE_usia+WOE_penghasilan+WOE_debitrasio+WOE_keluarga+
    WOE_terlambat30+WOE_terlambat60+WOE_terlambat90,
    data = datascoring.WOE)
    X=X[,-1]
    lasso lasso <- lars(x=X,y=datascoring.WOE$status,trace = TRUE)
    LASSO sequence
    Computing X'X …..
    Error in t(y) %*% x : non-conformable arguments

    Reply
  • Complete the code chunk in the template to write a function my_chol that accepts a square, positive definite matrix and returns the Cholesky Decomposition in the form of a lower triangular matrix. You may not use the built-in chol() and within your function, although you can use them to validate your answers.
    Your code does need to confirm that the input matrix M is square and positive definite.

    Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top