Martin - 4 months ago 35

R Question

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, e.g. within a function with user supplied formula)?

Answer

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"
```