0

I need to add about 100 extra columns to data.frame based on the length of a previous data.frame

For example, I have two data.frames Xtrain and Xtest. Xtrain as 1000 columns, but Xtest has only 900 columns. This difference is due to 1-hot encoding the Xtrain and Xtest separately.

How can I add those 100 missing columns (with all 0s) to Xtest? Also, the order of columns in the augmented Xtest should be same as Xtrain.

This is what I have tried so far:

extra = setdiff(names(Xtrain), names(Xtest))

for (e in extra){
    Xtest$e <- 0
}

But this adds a column e not the corresponding entry in extra.

coatless
  • 20,011
  • 13
  • 69
  • 84
Sonu Mishra
  • 1,659
  • 4
  • 26
  • 45

0 Answers0