dummy variable - 1 hot encoding training and test data separately in R -


i need add 100 columns data.frame based on length of previous data.frame

for example, have 2 data.frames xtrain , xtest. xtrain 1000 columns, xtest has 900 columns. difference due 1-hot encoding xtrain , xtest separately.

how can add 100 missing columns (with 0s) xtest? also, order of columns in augmented xtest should same xtrain.

this have tried far:

extra = setdiff(names(xtrain), names(xtest))  (e in extra){     xtest$e <- 0 } 

but adds column e not corresponding entry in extra.