J. Mini
J. Mini

Reputation: 1610

Change column names of numeric vector?

Take output<-matrix(sample(9),nrow=3,ncol=3). This gives me:

> output
     [,1] [,2] [,3]
[1,]    8    2    3
[2,]    4    7    6
[3,]    9    5    1

I want to change the column names to "a", "b", and "c". In other words, I want something like:

> output
       "a"  "b"  "c"
[1,]    8    2    3
[2,]    4    7    6
[3,]    9    5    1

There are many answers on this website about how to do this with a data frame, but what we have here believes itself to be an interger vector. So how can we handle it? I was surpised to find names(output)[,c(1,2,3)]<-c("a","b","c") run but apparently do nothing.

Upvotes: 0

Views: 347

Answers (1)

Ali
Ali

Reputation: 1080

You need to use colnames():

colnames(output) <- c("a","b","c")

Upvotes: 1

Related Questions