# Solving R Errors – what does non-conformable arguments mean in r

A non-conformable argument error isn’t especially uncommon. But what does non-conformable arguments mean in R? It’s not an error that really rolls off the tongue. But it sounds a lot more complex than it really is. In essence, the error simply means that you’re trying to work with multiple data points that don’t properly conform with each other’s structure. This is most commonly encountered when performing matrix multiplication. The solution is to either manipulate the basic structure that you’re working with into a compatible state or change your order of operations so that you’re essentially fitting the correct pieces together.

## How R Works With Data Structures

To understand the non-conformable error it’s important to take a brief detour into the nature of R’s datum. R is especially popular in data analysis thanks in part to the fact that the base R distribution has built-in support for advanced structures. For example, you can easily create and manipulate a complex data frame without any need for third-party libraries. In fact, many of R’s various data types have built-in support for advanced manipulation without even needing to call a special R function.

Data manipulation through R programming in general is just a lot more user-friendly than in many other languages. The entire syntax is built around the idea that you’re going to be manipulating a large number of varied element structures. So something like calculating the dot product of data structures is trivial in R while often complicated within other platforms. Likewise, R has a remarkable ability to anticipate what kind of manipulation will be needed to perform various operations on your data without the need to explicitly alert the interpreter. This predictive ability extends to concepts like fitted values as well. It’s even easy to leverage concepts like a diagonal matrix or identity matrix which essentially work as a more complex implementation of the number 1 for matrix-based calculations.

That said, even R will run into situations where it can’t anticipate what you ultimately want to do with two seemingly incompatible pieces of data. And this is the crux of a non-conformable argument error. It typically appears when you’re trying to work with matrices whose structures aren’t fully in line with each other. And the solution is in line with one of the words in the error message. To solve the problem you need to show the R interpreter how to make each structure “conform” to the other in a way that would allow the function to be completed.

## Taking a Closer Look at the Error and How It’s Triggered

The problem is most easily understood by actually working with the relevant data structures. Take a look at the following code.

ourMatrix <- matrix(1:15, nrow=3)
ourOtherMatrix <- matrix(1:12, nrow=4)
print(ourMatrix)
print(ourOtherMatrix)
dim(ourMatrix)
dim(ourOtherMatrix)
ourMatrix %*% ourOtherMatrix

We begin by creating two matrices filled with a count of one to fifteen and twelve, respectively. The ourMatrix variable is structured with three rows and ourOtherMatrix with four. We’re specifically setting these values with nrow because, as you’ll see, these values are particularly important to solving the non-conformable error. Next, we print out both of the structures in order to get a better visual representation of what we’re working with. This is followed by the use of the dim function to get the dimension of each. Note that this is going a little overboard in how one would normally get information about a structure. But for the sake of this example, it’s important to get a wider view of everything. Because when the actual multiplication occurs the R interpreter produces a non-comfortable argument error.

The error also highlights why it was so important to print out all that information about each of the variables. At first glance, the contents of the structures might seem similar but their formatting is quite distinct. But there’s an important point to take note of. The dimensions of ourMatrix are 3×5. While the dimensions of ourOtherMatrix are 4×3. The row count in ourMatrix is the same as the column count in ourOtherMatrix. Those two shared values are going to prove to be the key to solving this particular error.

## Taking a Closer Look at the Error and How It’s Triggered

Think back to the previous example. The 3×5 and 4×3 sizes were at odds with each other. But what if that order was flipped? Try the following.

Change the matrix’s declarations to (1:15, nrow=3) and (1:12, nrow=4). Then run the multiplication again as in the following line.
ourOtherMatrix %*% ourMatrix

And just like that, the error will disappear. The fix is simple enough that you might not have even noticed what’s different. But look at the position of ourMatrix and ourOtherMatrix. The new code swaps its position before the multiplication. The result wouldn’t make much sense if you were looking at it from the perspective of standard algebra. In that situation, multiplication works the same no matter what the position of the numeric value involved. But when multiplying matrices you need to keep in mind that they’re essentially containers for additional vector types. It’s the dimension of the elements being worked with that matters. In this case, we just need to ensure that the left column vector is compatible with the right row vector.

The earlier error was entirely due to the fact that the row and column values weren’t properly matched up. You can essentially think of it as an order of operations or placing a smaller plate snugly onto a larger one. While doing the reverse wouldn’t turn out very well.

If you’re working with new structures rather than existing ones you can also just plan ahead and create them to be compatible from the start. With the earlier example, that could be done with the following changes.

ourMatrix <- matrix(1:12, nrow=3)
ourOtherMatrix <- matrix(1:12, nrow=4)
print(ourMatrix)
print(ourOtherMatrix)
dim(ourMatrix)
dim(ourOtherMatrix)
ourMatrix %*% ourOtherMatrix

As you can see from the dim statements, ourMaxtrix and ourOtherMatrix now have a structure of 3×4 and 4×3. The columns in ourMatrix are compatible with the rows in ourOtherMatrix without needing to swap their order around within the final multiplication.

Finally, you can usually modify existing structures fairly easily in R. Take a look at the following.

ourMatrix <- matrix(1:15, nrow=3)
ourOtherMatrix <- matrix(1:12, nrow=4)
ourMatrix <- matrix(ourMatrix, nrow=4, byrow = TRUE)
ourMatrix %*% ourOtherMatrix

In this example, we reshape ourMatrix with the data preserved by reading the contents in row by row. Note that if byrow hadn’t been set to true it would have defaulted to the false state and operated in a similar way with columns. These options are part of the reason why R won’t perform this or similar operations automatically. By giving you the error it’s essentially telling you that there are subjective choices to be made. But once ourMatrix is reshaped and reassigned back into the originating variable it can now be multiplied with ourOtherMatrix without needing to change the order of operations.

Scroll to top