Getting the matrix in the form of a matrix

What is the shortest way to get a row from a matrix as a matrix?

> x<-matrix(1:9,nrow=3,byrow=TRUE) > x [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 > x[1,] [1] 1 2 3 > is.vector(x[1,]) [1] TRUE 

where i would like to get

  [,1] [,2] [,3] [1,] 1 2 3 
+6
source share
2 answers

[ accepts the drop argument, which controls whether the selected subset will force (if possible) an object with a lower dimension (in this case, a simple vector). To ensure that a subset of the matrix will always be a matrix, set its drop=FALSE as follows:

 x[1,,drop=FALSE] [,1] [,2] [,3] [1,] 1 2 3 

(And for a complete set of subset rules and arguments, try help("[") .)

+17
source
 t(as.matrix(x[1,])) 

Gotta do the trick ...

+1
source

Source: https://habr.com/ru/post/944331/


All Articles