[R] Matrices with a single column

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Sep 1 17:07:12 CEST 2005


Crispin Miller wrote:

> Hi,
> I've got a quick question about what happens when indexing into matrices
> with a single column. I was wondering if anyone can help ...
>  
> For example:
> 
>>x <- matrix(1:10)
> 
> 
>>y <- cbind(x,x)
> 
> 
>>x[4:6,]
> 
> 
> [1] 4 5 6
> 
> 
>>y[4:6,]
> 
> 
>    [,1] [,2]
> 
> [1,] 4 4
> 
> [2,] 5 5
> 
> [3,] 6 6
> 
> 
>>class(x[4:6,])
> 
> 
> [1] "integer"
> 
> 
>>class(y[4:6,])
> 
> 
> [1] "matrix"
> 
> It seems that R is returning the results of indexing into a matrix with
> one column as a vector rather than a matrix?
> 
> Does anyone have a good way of preventing this from happening?

Yes, the argument "drop":

x[4:6,,drop=FALSE]

Uwe Ligges

> cheers,
> 
> crispin
>  
> --------------------------------------------------------
> 
>  
> This email is confidential and intended solely for the use o...{{dropped}}
> 
> ______________________________________________
> 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




More information about the R-help mailing list