简体   繁体   中英

R: Renaming when using model.matrix

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)  
colnames(A)  

model.matrix adds to the variable names the prefix "B" in the matrix A. How can I avoid this behavior (or rename it but in a very general setting, eg within a function with user supplied formula)?

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)[1], colnames(B))

Another option without using the sub would be to create a data.frame with 'y' and 'B' matrix and then use y ~.

A <- model.matrix(y~., data=data.frame(y, B))
colnames(A)
#[1] "(Intercept)" "x1"          "x2"      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM