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. I tried this by hand:> matrix.a801 802 803 [1,] -0.0906346 0.0906346 0.0906346 [2,] -0.0804911 0.0804911 0.0804911 [3,] -0.0703796 0.0703796 0.0703796> matrix.a<-as.matrix(matrix.a[,-1]) > matrix.a802 803 [1,] 0.0906346 0.0906346 [2,] 0.0804911 0.0804911 [3,] 0.0703796 0.0703796> matrix.a<-as.matrix(matrix.a[,-1]) > matrix.a[,1] [1,] 0.0906346 [2,] 0.0804911 [3,] 0.0703796 Is there a way to force the column name to remain in such a case? Thanks, Alex> sessionInfo()R version 2.4.1 (2006-12-18) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" [7] "base">------------------------------------ Alex Lam PhD student Department of Genetics and Genomics Roslin Institute (Edinburgh) Roslin Midlothian EH25 9PS Great Britain Phone +44 131 5274471 Web http://www.roslin.ac.uk Roslin Institute is a company limited by guarantee, registered in Scotland (registered number SC157100) and a Scottish Charity (registered number SC023592). Our registered office is at Roslin, Midlothian, EH25 9PS. VAT registration number 847380013. The information contained in this e-mail (including any attachments) is confidential and is intended for the use of the addressee only. The opinions expressed within this e-mail (including any attachments) are the opinions of the sender and do not necessarily constitute those of Roslin Institute (Edinburgh) ("the Institute") unless specifically stated by a sender who is duly authorised to do so on behalf of the Institute
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]> MATA 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
You seem to be looking for matrix.a[,-1, drop = TRUE] On Tue, 1 May 2007, 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.And the matrix became a vector.> > I tried this by hand: > >> matrix.a > 801 802 803 > [1,] -0.0906346 0.0906346 0.0906346 > [2,] -0.0804911 0.0804911 0.0804911 > [3,] -0.0703796 0.0703796 0.0703796 >> matrix.a<-as.matrix(matrix.a[,-1]) >> matrix.a > 802 803 > [1,] 0.0906346 0.0906346 > [2,] 0.0804911 0.0804911 > [3,] 0.0703796 0.0703796 >> matrix.a<-as.matrix(matrix.a[,-1]) >> matrix.a > [,1] > [1,] 0.0906346 > [2,] 0.0804911 > [3,] 0.0703796 > > Is there a way to force the column name to remain in such a case? > > Thanks, > Alex > >> sessionInfo() > R version 2.4.1 (2006-12-18) > i386-pc-mingw32 > > locale: > LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United > Kingdom.1252;LC_MONETARY=English_United > Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 > > attached base packages: > [1] "stats" "graphics" "grDevices" "utils" "datasets" > "methods" > [7] "base" >> > > ------------------------------------ > Alex Lam > PhD student > Department of Genetics and Genomics > Roslin Institute (Edinburgh) > Roslin > Midlothian EH25 9PS > Great Britain > > Phone +44 131 5274471 > Web http://www.roslin.ac.uk-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595