kapil
kapil

Reputation: 41

Renaming of Column names in Model.matrix

Can any one help me in renaming of column names of model.matix?

I have been trying to rename my columns:

colnames(model.matrix(reg))[1] <- c("Intercept")

but I am getting error message:

could not find function "model.matrix<-"

Here is the model.matrix object:

model.matrix.default(reg)
   xxXB(Intercept) xxXBproductPageViews xxXBqty_order xxXBLag.sales
2         1.782842             2352.032      1.804487      303.0831
4         1.782842             1569.158      1.799786      369.0831
5         1.782842             2541.884      1.806206      434.7506
6         1.782842             2932.289      1.811827      414.7104
7         1.782842             2670.780      1.798207      360.4423
8         1.782842             2694.579      1.793033      291.9571
9         1.782842             5435.900      1.791143      325.7292
10        1.782842            10727.969      1.807148      602.7453
11        1.782842            12711.044      1.826717     1130.3189
12        1.782842            10774.425      1.808426     1694.6620
13        1.782842             8597.127      1.789496     1611.1204
14        1.782842             7647.235      1.789358     1161.2223
15        1.782842             5538.071      1.798733      979.3913
16        1.782842             4240.954      1.798549      813.5173
17        1.782842             3890.973      1.787179      668.3082
18        1.782842             4086.364      1.816897      563.1715
19        1.782842             4878.903      1.815232      552.4128
20        1.782842             3999.407      1.787328      612.4691
21        1.782842             3349.887      1.875233      526.7774
22        1.782842             5394.895      1.891263      599.2761
23        1.782842             4682.374      1.817970     1290.7023
24        1.782842             3346.345      1.805635     1057.0534
25        1.782842             3106.214      1.839484      638.3726
26        1.782842             4559.091      1.878944      611.1822
attr(,"assign")
[1] 1 1 1 1

The above matrix is matrix of lm equation called reg. Now how can I change column names of model.matrix?

Upvotes: 3

Views: 20928

Answers (2)

darrelkj
darrelkj

Reputation: 142

Is model.matrix a function? Are you wanting to give the result from sending 'reg' to model.matrix() a new name? The error means that the type does not have a <- method. Look in R help for [<-.factor for more info.

If this is the case though model.matrix (which you spelled model.matix) does not have columns, its a function, you want to change the names of its return.

Try this

model.matrix <- function(x) {
      matrix(runif(4), nrow = 2, ncol = 2,
            dimnames = list(c("r1", "r2"), c("C1", "C2")))
    }

And what you are doing

colnames(model.matrix(1))[1] <- 'a' 

will not work but you can do the following

x <- model.matrix(1)
colnames(x)[1] <- 'a'

I'm just giving an argument of 1 because I am not sure what you 'reg' is.

Upvotes: -1

Thomas
Thomas

Reputation: 44525

You need to store the model.matrix result first then rename its columns.

mm <- model.matrix(reg)
colnames(mm)[1] <- c("Intercept")

More generally you could could use gsub to replace the "xxXB" part from all the names with:

colnames(mm) <- gsub("xxXB","",colnames(mm))

An alternative approach would involve the following:

`colnames<-`(model.matrix(reg),1:4)

where 1:4 is a vector of replacement names of the appropriate length. I think the first solution is easier (and more flexible), though.

Upvotes: 10

Related Questions