Calculate feature weight vector for one-hot-encoded data frame in R
The information you need is in the assign attribute on the matrix returned by model.matrix. Something like this seems to work for me:

data &lt;- data.frame(V1 = factor(c("A", "A", "B", "C")), V2 = c(1, 3, 3, 2), V3 = c(3, 5, 3, 3))
model &lt;- model.matrix(~ . + 0, data = data)
assignments &lt;- attr(model, "assign")
counts &lt;- table(assignments)
weights &lt;- sapply(assignments, function (x) { 1 / counts[[x]] })


weights is now (0.3333333, 0.3333333, 0.3333333, 1.0000000, 1.0000000).