How To Fix the R error in rbind(deparse.level, …) : numbers of columns of arguments do not match

When doing data science in r, the “error in rbind numbers of columns of arguments do not match” error message can occur when using rbind on two dataframes that do not have the same number of columns. To fix this problem you need to bind the dataframes by rows rather than columns. This approach even fixes the case where there are different column names.

Description of the error

The “error in rbind numbers of columns of arguments do not match” error message occurs when using the rbind function on dataframes with differences in column number. This function looks for common columns, and so it causes an error message when there is a mismatch in a column name. It produces our current error message when it is a mismatch in the number of columns and another error message when there is a mismatch in column names. It is like cbind, but each argument is a dataframe rather than a vector. When using this function, you must remember that the column names and numbers need to match exactly for this function to work.

Explanation of the error

Here is a code example that produces the “error in rbind numbers of columns of arguments do not match” error message.

> data1 = data.frame(x1 = c(1, 2, 3, 4, 5),
+ x2 = c(6, 7, 8, 9, 10),
+ x3 = c(11,12,13,14,15))
> data1
x1 x2 x3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
> data2 = data.frame(x1 = c(21, 22, 23),
+ x2 = c(31, 32, 33))
> data2
x1 x2
1 21 31
2 22 32
3 23 33
> rbind(data1, data2)
Error in rbind(deparse.level, …) :
numbers of columns of arguments do not match

As you can see here, there is a mismatch in the number of columns in the dataframes, and this causes our error message.

How to fix the error

Here is a code example that fixes the “error in rbind numbers of columns of arguments do not match” error message. We do so by using the bind_rows function.

> library(“dplyr”)
> data1 = data.frame(x1 = c(1, 2, 3, 4, 5),
+ x2 = c(6, 7, 8, 9, 10),
+ x3 = c(11,12,13,14,15))
> data1
x1 x2 x3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
> data2 = data.frame(x1 = c(21, 22, 23),
+ x2 = c(31, 32, 33))
> data2
x1 x2
1 21 31
2 22 32
3 23 33
> data_bind_rows = bind_rows(data1, data2)
> data_bind_rows
x1 x2 x3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
6 21 31 NA
7 22 32 NA
8 23 33 NA

As you can see in this example, the third column is filled out with missing values to parallel the second data frame. This is how the bind_rows function works, to get around the problem of a mismatch in columns. This is also why it can fix the other problem that occurs when you have a mismatch in column names. In both cases, it fills in the missing space with missing values allowing it to combine the two dataframes seamlessly. The problem is that this is not a base r function, but part of the dplyr package. As a result, you have to install this package before using this function. Once you have this package installed the bind_rows function will be available for use.

The “error in rbind numbers of columns of arguments do not match” error message is an easy error message to get, particularly when you are using dataframes that you have not created. If you have control over the dataframes you are working with, you can adjust them internally correcting any differences that would cause our error message. However, if this is not an option, or if you simply do not want to take the time to do it, you can install the dplyr package and then go ahead and use the bind_rows function to fix the problem. In either case, it is an easy problem to fix.

Scroll to top
Privacy Policy