When running the following R code:
y <- rnorm(100)
B <- matrix(rnorm(200), ncol=2)
colnames(B) <- c("x1", "x2")
A <- model.matrix(y~B)
We can use
sub to remove the first character in the column names.
colnames(A)[-1] <- sub("^.", "", colnames(A)[-1])
Or set the column names by concatenating with the ones from 'B'
colnames(A) <- c(colnames(A), colnames(B))
Another option without using the
sub would be to create a data.frame with 'y' and 'B' matrix and then use
A <- model.matrix(y~., data=data.frame(y, B)) colnames(A) # "(Intercept)" "x1" "x2"