[R] Simply matrix slice question

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Fri Jul 24 08:20:58 CEST 2009


yes, you need to use the 'drop' argument; have a look at the help file 
?"[" and then try this:

z <- matrix(rnorm(20), ncol = 5)
z[1, , drop = FALSE]
z[, 1, drop = FALSE]


I hope it helps.

Best,
Dimitris


Jim Nemesh wrote:
> Is there any way to force a slice of a matrix to stay a matrix? R tends 
> to convert a single row of a matrix into a vector.
> 
> Example:
> 
> z<-matrix (rnorm(20), ncol=5)
> zz<-z[1,]
> is.matrix(zz) #FALSE
> 
> I usually resort to:
> 
> zz<-matrix(z[1,], ncol=dim(z)[2], dimnames=list(rownames(z)[1], 
> colnames(z)))
> 
> But that seems horribly kludgy.
> 
> Thanks!
> 
> -Jim
> 
> ______________________________________________
> R-help at r-project.org 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list