Can we combine two data frames with similar column names but different order directly in R?




I want to combine my test and the training datasets so that I can create new variables in both of them together. I use the command


but get and error saying

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

So I create dummy variables which are missing in the test dataset and then use the same command again but get an error saying that the order of variables are different.

This problem does not arise while combining datasets in python as it takes NaNs in place of the missing variables and combines them. Can we not do something similar in R? Or the only way is to create the same variables in the same order in both the datasets and then combine them?



Hello @pravin,

I am not sure if I understood you correctly.According to what I understood from your ques I did:

df1 <- data.frame(A = 1,B = "x")
df2 <- data.frame(B = "y",A = 2)
df3 <- rbind(df1,df2) 

And did not receive any error.
Is this what you were trying to do?