Calculate feature weight vector for one-hot-encoded data frame in R I have the following data frame with one categorical and two numerical columns:

    V1  V2  V3
1   A   1   3
2   A   3   5
3   B   3   3
4   C   2   3


I have turned this into the following dummy variables:

    V1.1  V1.2  V1.3 V2  V3
1   1     0     0    1   3
2   1     0     0    3   5
3   0     1     0    3   3
4   0     0     1    2   3


Now, I want to apply clustering to this latter set. I guess that I could get better results if I downweight the dummy variable columns (proportionally to their number) because with equal weights the distance based clusters will be distorted.

My question is, how could I get the following weight vector from the new data set:

0.33, 0.33, 0.33, 1, 1
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).