rbind error: numbers of columns of arguments do not match

Rbind error numbers of columns of arguments do not match

If you are having this rbind error “numbers of columns of arguments do not match”, there might be a solution to this problem. It is caused by different column names in data frames. The good news is that there is a quick solution to that.

Here are two data frames. The second one has a column that the first does not.

df1 <- data.frame(
  agent = as.character(c("David", "David", "David", "Kate", "Kate", "Alma", "Alma")),
  manager = as.character(c("Lisa", "Monica", "Karl", "Kianna", "Luna", "Kaylin", "Georgia"))
)

df2 <- data.frame(
  agent = as.character(c("David", "David", "David", "Kate", "Kate", "Alma", "Alma")),
  manager = as.character(c("Lisa", "Monica", "Karl", "Kianna", "Luna", "Kaylin", "Georgia")),
  team = as.character(c("Sales", "Sales", "Sales", "Billing", "Sales", "Sales", "Sales"))
)

After appending them by using rbind, I’m getting an error “numbers of columns of arguments do not match”.

rbind(df1, df2)
#Error in rbind(deparse.level, ...) : 
#  numbers of columns of arguments do not match

The solution of rbind error

If it is not a problem with your data, and you want to append those data frames, then try to use bind_rows from the dplyr package.

bind_rows(df1, df2)
#   agent manager    team
#1  David    Lisa      NA
#2  David  Monica      NA
#3  David    Karl      NA
#4   Kate  Kianna      NA
#5   Kate    Luna      NA
#6   Alma  Kaylin      NA
#7   Alma Georgia      NA
#8  David    Lisa   Sales
#9  David  Monica   Sales
#10 David    Karl   Sales
#11  Kate  Kianna Billing
#12  Kate    Luna   Sales
#13  Alma  Kaylin   Sales
#14  Alma Georgia   Sales

As you can see, the problem is solved. For the data frame that does not contain a specific column appears NA values.

Check out my favorite RStudio tips and tricks. For example, how to quickly view a data frame from R script.

If you are new to R but experienced in Excel, then it might be useful to take a look at the ideas on how to switch from Excel to R.




Leave a comment

Your email address will not be published. Required fields are marked *

Exit mobile version