Matrix Multiplication Error in R: non-conformable arguments (2 Examples)
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 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 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 illustrates the result of the previous R syntax.
Video & Further Resources
In case you need further information 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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
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.