[R] Matrix column name

Marc Schwartz marc_schwartz at comcast.net
Tue May 1 19:14:07 CEST 2007


On Tue, 2007-05-01 at 18:03 +0100, alex lam (RI) wrote:
> Dear R users,
> 
> Having searched the mail archive I think the conclusion was that it is
> not possible to have a column name when there is only one column in the
> matrix. But I thought I'd check with the more experienced users.
> 
> What I tried to do was: in a loop I pick a column, record the column
> name and remove the column from the matrix. But when there were 2
> columns left, after one column was removed, the last column name
> disappeared by default. It means that I always miss out the last column.

See R FAQ 7.5 Why do my matrices lose dimensions:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-dimensions_003f

which has some examples, along with ?Extract

To wit:

MAT <- matrix(1:12, ncol = 3)

colnames(MAT) <- LETTERS[1:3]

> MAT
     A B  C
[1,] 1 5  9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12


> MAT[, 1]
[1] 1 2 3 4


> MAT[, 1, drop = FALSE]
     A
[1,] 1
[2,] 2
[3,] 3
[4,] 4


HTH,

Marc Schwartz



More information about the R-help mailing list