[R] when deleting a row from a matrix gives a vector

Marc Schwartz marc_schwartz at comcast.net
Fri Nov 2 20:52:42 CET 2007


On Fri, 2007-11-02 at 20:47 +0100, Gabriel Valiente wrote:
> Deleting a row from a matrix turns it into a vector (and dim names  
> are lost) if the resulting matrix has only one row or column. For  
> instance:
> 
>  > x <- matrix(1:10, ncol=2)
>  > x <- x[1,]
> 
> turns x into
> 
> [1] 1 6
> 
> instead of
> 
>       [,1] [,2]
> [1,]    1    6
> 
> Is there any way to force the result to remain a matrix, even if it  
> has only one row or column, or even only one entry? Thanks,
> 
> Gabriel

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

HTH,

Marc Schwartz



More information about the R-help mailing list