トレーニング セットとテスト セット用に 2 つのスパース マトリックスがあり、それぞれにない列を削除して、両方の列を同じにする必要があります。現時点ではループを使用していますが、より効率的な方法があると確信しています。
# take out features in training set that are not in test
i<-0
for(feature in testmatrix@Dimnames[2][[1]]){
i<-i+1
if(!(feature %in% trainmatrix@Dimnames[2][[1]])){
removerows<-c(removerows, i)
}
}
testmatrix<-testmatrix[,-removerows]
# and vice versa...