r - Renaming of Column names in Model.matrix -
can 1 me in renaming of column names of model.matix?
i have been trying rename columns:
colnames(model.matrix(reg))[1] <- c("intercept") but getting error message:
could not find function "model.matrix<-" here 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 matrix of lm equation called reg. how can change column names of model.matrix?
you need store model.matrix result first rename columns.
mm <- model.matrix(reg) colnames(mm)[1] <- c("intercept") more could use gsub replace "xxxb" part names with:
colnames(mm) <- gsub("xxxb","",colnames(mm)) an alternative approach involve following:
`colnames<-`(model.matrix(reg),1:4) where 1:4 vector of replacement names of appropriate length. think first solution easier (and more flexible), though.
Comments
Post a Comment