Q:

What's the best way to make matrix column names of the form [Q1, Q2, ...] in R?

I'm reading an Excel file into R as follows:
Table <- read_excel("mydatafile.xlsx", 1, col_names=FALSE)

Then, I convert it to a matrix:
mymatrix <- as.matrix(Table)

The columns automatically come out as X0, X1, X2, and so on up to X107. Because the columns represent answers to a test I want to make some new column names in R. I want the columns to be named Q1, Q2, Q3, Q4, and so on up to Q108.
I need to create a vector of column names so that I can go 
colnames(mymatrix) <- my_col_names

However, I'm not sure how best to do that. I realise I could easily get what I want with a loop, but I understand that looping is generally discouraged in R. I'm wondering if there's a better way to do this.

A:

You can try with paste and paste is vectorized.
 colnames(mymatrix) <- paste0('Q', 1:108)

If you don't already know the number of columns in the matrix, (as @thelatemail mentioned in the comments) a general approach would be
 colnames(mymatrix) <- paste0("Q",seq_len(ncol(mymatrix)))

You could also change the column names using regex.  Suppose if the column names in your 'mymatrix' is
 nm1 <- paste0('X',0:107)

We could use gsubfn to change the names
 library(gsubfn)
 nm2 <- gsubfn('[^0-9]+([0-9]+)$', ~sprintf('Q%d',as.numeric(x)+1), nm1)
 head(nm2)
 #[1] "Q1" "Q2" "Q3" "Q4" "Q5" "Q6"
 colnames(mymatrix) <- nm2

