How to turn a vector into a matrix in R?

前端 未结 2 1728
臣服心动
臣服心动 2020-12-08 18:27

I have a vector with 49 numeric values. I want to have a 7x7 numeric matrix instead.

Is there some sort of convenient automatic conversion statement I can use, or d

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 18:57

    Just use matrix:

    matrix(vec,nrow = 7,ncol = 7)
    

    One advantage of using matrix rather than simply altering the dimension attribute as Gavin points out, is that you can specify whether the matrix is filled by row or column using the byrow argument in matrix.

提交回复
热议问题