[R] Matrix column name

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 1 23:14:01 CEST 2007


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



More information about the R-help mailing list