Skip to content
Prev 398119 / 398506 Next

Extracting portions of one vector to create others

On 9/2/2025 8:38 AM, Rui Barradas wrote:
Hello,

Here is another way with ?model.matrix.


res2 <- model.matrix(~ 0 + factor(CLASS), data = Beth)
colnames(res2) <- sub("factor\\((.*)\\)", "\\1", colnames(res2))



This will create matrix res2 with other attributes, not just dimnames. 
To get rid of those you can run


attr(res2, "assign") <- NULL
attr(res2, "contrasts") <- NULL
attributes(res2)


Hope this helps,

Rui Barradas