Re: [R] Matrix column name

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Tue, 01 May 2007 12:14:07 -0500

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



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 02 May 2007 - 02:47:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 May 2007 - 03:31:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.