开发者

How to subset matrix to one column, maintain matrix data type, maintain row/column names?

开发者 https://www.devze.com 2023-04-03 19:20 出处:网络
When I subset a matrix to a single column, the result is of class numeric, not matrix (i.e. myMatrix[ , 5 ] to subset to the fifth 开发者_如何学运维column).Is there a compact way to subset to a single

When I subset a matrix to a single column, the result is of class numeric, not matrix (i.e. myMatrix[ , 5 ] to subset to the fifth 开发者_如何学运维column). Is there a compact way to subset to a single column, maintain the matrix format, and maintain the row/column names without doing something complicated like:

matrix( myMatrix[ , 5 ] , dimnames = list( rownames( myMatrix ) , colnames( myMatrix )[ 5 ] )


Use the drop=FALSE argument to [.

m <- matrix(1:10,5,2)
rownames(m) <- 1:5
colnames(m) <- 1:2
m[,1]             # vector
m[,1,drop=FALSE]  # matrix
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号