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

前端 未结 1 507
灰色年华
灰色年华 2020-11-22 12:06

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

相关标签:
1条回答
  • 2020-11-22 12:48

    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 讨论(0)
提交回复
热议问题