If I have a factor variable, say x = factor(c(1, 2, 3))
, then I can use model.matrix
function to generate a dummy matrix:
model.matrix(~x + 0)
and I will get a matrix like:
x1 x2 x3
1 1 0 0
2 0 1 0
3 0 0 1
My question is that, if I already have a large dummy matrix, how could I melt it back to a (factor) column?
In another world, is there an inverse function of model.matrix
?