Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

I am trying to do some feature engineering on test and train data. I am well versed with python but new to R.

#Row binding train & test set for feature engineering
train_test = rbind(train, test)

It seems that my train and test data have different number of columns. How to resolve this so that the only columns which are common in both dataframes stay?

Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match
like image 936
noob Avatar asked Apr 09 '20 12:04

noob


2 Answers

I would find out what the column names are for both data frames, take their intersection (common names), and selecting those columns from both data frames:

train_names <- colnames(train)
test_names <- colnames(test)
common_names <- intersect(train_names, test_names)

train_test <- rbind(train[common_names], test[common_names])
like image 52
Bas Avatar answered Oct 13 '22 01:10

Bas


Find the common columns:

common_cols <- intersect(colnames(train), colnames(test))

Now perform the rbind

train_test=rbind(subset(train, select = common_cols), 
  subset(test, select = common_cols))
like image 43
noob Avatar answered Oct 12 '22 23:10

noob